19616fa5dSAxel Dörfler #include "BufferPool.h"
29616fa5dSAxel Dörfler
39616fa5dSAxel Dörfler #include <stdio.h>
49616fa5dSAxel Dörfler #include <string.h>
59616fa5dSAxel Dörfler
69616fa5dSAxel Dörfler
79616fa5dSAxel Dörfler #define BLOCK_SIZE 2048
89616fa5dSAxel Dörfler #define NUM_THREADS 10
99616fa5dSAxel Dörfler #define NUM_LOOPS 50
109616fa5dSAxel Dörfler
119616fa5dSAxel Dörfler
129616fa5dSAxel Dörfler int32 gID;
139616fa5dSAxel Dörfler
149616fa5dSAxel Dörfler
159616fa5dSAxel Dörfler int32
allocator(void * _pool)169616fa5dSAxel Dörfler allocator(void *_pool)
179616fa5dSAxel Dörfler {
189616fa5dSAxel Dörfler BufferPool &pool = *(BufferPool *)_pool;
199616fa5dSAxel Dörfler int32 id = atomic_add(&gID, 1);
209616fa5dSAxel Dörfler
219616fa5dSAxel Dörfler for (int32 i = 0; i < NUM_LOOPS; i++) {
229616fa5dSAxel Dörfler snooze(50000);
239616fa5dSAxel Dörfler
249616fa5dSAxel Dörfler printf("%ld. Get buffer...\n", id);
259616fa5dSAxel Dörfler status_t status;
269616fa5dSAxel Dörfler void *buffer;
279616fa5dSAxel Dörfler if ((status = pool.GetBuffer(&buffer)) != B_OK) {
289616fa5dSAxel Dörfler printf("\t%ld. Couldn't get buffer: %s\n", id, strerror(status));
299616fa5dSAxel Dörfler continue;
309616fa5dSAxel Dörfler }
319616fa5dSAxel Dörfler printf("\t%ld. got buffer\n", id);
329616fa5dSAxel Dörfler snooze(50000);
339616fa5dSAxel Dörfler pool.PutBuffer(buffer);
349616fa5dSAxel Dörfler printf("\t%ld. released buffer\n", id);
359616fa5dSAxel Dörfler }
36*7410cb93SAxel Dörfler return 0;
379616fa5dSAxel Dörfler }
389616fa5dSAxel Dörfler
399616fa5dSAxel Dörfler
409616fa5dSAxel Dörfler int
main(int argc,char ** argv)419616fa5dSAxel Dörfler main(int argc, char **argv)
429616fa5dSAxel Dörfler {
439616fa5dSAxel Dörfler BufferPool pool;
449616fa5dSAxel Dörfler thread_id thread[NUM_THREADS];
459616fa5dSAxel Dörfler
469616fa5dSAxel Dörfler if (pool.RequestBuffers(BLOCK_SIZE) == B_OK) {
479616fa5dSAxel Dörfler for (int i = 0; i < NUM_THREADS; i++) {
489616fa5dSAxel Dörfler thread[i] = spawn_thread(allocator, "", B_NORMAL_PRIORITY, (void *)&pool);
499616fa5dSAxel Dörfler if (thread[i] < B_OK)
50*7410cb93SAxel Dörfler fprintf(stderr, "Couldn't spawn thread %d\n", i);
519616fa5dSAxel Dörfler resume_thread(thread[i]);
529616fa5dSAxel Dörfler }
539616fa5dSAxel Dörfler
549616fa5dSAxel Dörfler for (int32 i = 0; i < NUM_THREADS; i++) {
559616fa5dSAxel Dörfler status_t status;
569616fa5dSAxel Dörfler wait_for_thread(thread[i], &status);
579616fa5dSAxel Dörfler }
589616fa5dSAxel Dörfler
599616fa5dSAxel Dörfler pool.ReleaseBuffers();
609616fa5dSAxel Dörfler }
619616fa5dSAxel Dörfler
629616fa5dSAxel Dörfler return 0;
639616fa5dSAxel Dörfler }
64