1#include "BufferPool.h"
2
3#include <stdio.h>
4#include <string.h>
5
6
7#define BLOCK_SIZE 2048
8#define NUM_THREADS 10
9#define NUM_LOOPS 50
10
11
12int32 gID;
13
14
15int32
16allocator(void *_pool)
17{
18	BufferPool &pool = *(BufferPool *)_pool;
19	int32 id = atomic_add(&gID, 1);
20
21	for (int32 i = 0; i < NUM_LOOPS; i++) {
22		snooze(50000);
23
24		printf("%ld. Get buffer...\n", id);
25		status_t status;
26		void *buffer;
27		if ((status = pool.GetBuffer(&buffer)) != B_OK) {
28			printf("\t%ld. Couldn't get buffer: %s\n", id, strerror(status));
29			continue;
30		}
31		printf("\t%ld. got buffer\n", id);
32		snooze(50000);
33		pool.PutBuffer(buffer);
34		printf("\t%ld. released buffer\n", id);
35	}
36	return 0;
37}
38
39
40int
41main(int argc, char **argv)
42{
43	BufferPool pool;
44	thread_id thread[NUM_THREADS];
45
46	if (pool.RequestBuffers(BLOCK_SIZE) == B_OK) {
47		for (int i = 0; i < NUM_THREADS; i++) {
48			thread[i] = spawn_thread(allocator, "", B_NORMAL_PRIORITY, (void *)&pool);
49			if (thread[i] < B_OK)
50				fprintf(stderr, "Couldn't spawn thread %d\n", i);
51			resume_thread(thread[i]);
52		}
53
54		for (int32 i = 0; i < NUM_THREADS; i++) {
55			status_t status;
56			wait_for_thread(thread[i], &status);
57		}
58
59		pool.ReleaseBuffers();
60	}
61
62	return 0;
63}
64