From: Bart Van Assche Date: Thu, 23 Jul 2009 18:22:00 +0000 (+0000) Subject: Ported circular_buffer test to Darwin. X-Git-Tag: svn/VALGRIND_3_5_0~280 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=572a6b91d536a1bd5504d35c0bbc9dcab6319a6f;p=thirdparty%2Fvalgrind.git Ported circular_buffer test to Darwin. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@10568 --- diff --git a/drd/tests/circular_buffer.c b/drd/tests/circular_buffer.c index 943c101410..9a7238cb43 100644 --- a/drd/tests/circular_buffer.c +++ b/drd/tests/circular_buffer.c @@ -24,15 +24,17 @@ has built-in functions for atomic memory access. #define BUFFER_MAX (2) +#define DATA_SEMAPHORE_NAME "cb-data-semaphore" +#define FREE_SEMAPHORE_NAME "cb-free-semaphore" typedef int data_t; typedef struct { /* Counting semaphore representing the number of data items in the buffer. */ - sem_t data; + sem_t* data; /* Counting semaphore representing the number of free elements. */ - sem_t free; + sem_t* free; /* Position where a new elements should be written. */ int in; /* Position from where an element can be removed. */ @@ -54,10 +56,34 @@ int fetch_and_add(int* p, int i) return __sync_fetch_and_add(p, i); } -void buffer_init(buffer_t * b) +static sem_t* create_semaphore(const char* const name, const int value) { - sem_init(&b->data, 0, 0); - sem_init(&b->free, 0, BUFFER_MAX); +#ifdef __APPLE__ + sem_t* p = sem_open(name, O_CREAT, 0600, value); + return p; +#else + sem_t* p = malloc(sizeof(*p)); + if (p) + sem_init(p, 0, value); + return p; +#endif +} + +static void destroy_semaphore(const char* const name, sem_t* p) +{ +#ifdef __APPLE__ + sem_close(p); + sem_unlink(name); +#else + sem_destroy(p); + free(p); +#endif +} + +static void buffer_init(buffer_t * b) +{ + b->data = create_semaphore(DATA_SEMAPHORE_NAME, 0); + b->free = create_semaphore(FREE_SEMAPHORE_NAME, BUFFER_MAX); pthread_mutex_init(&b->mutex_in, NULL); pthread_mutex_init(&b->mutex_out, NULL); @@ -66,10 +92,10 @@ void buffer_init(buffer_t * b) b->out = 0; } -void buffer_recv(buffer_t* b, data_t* d) +static void buffer_recv(buffer_t* b, data_t* d) { int out; - sem_wait(&b->data); + sem_wait(b->data); if (use_locking) pthread_mutex_lock(&b->mutex_out); out = fetch_and_add(&b->out, 1); @@ -86,13 +112,13 @@ void buffer_recv(buffer_t* b, data_t* d) printf("received %d from buffer[%d]\n", *d, out); fflush(stdout); } - sem_post(&b->free); + sem_post(b->free); } -void buffer_send(buffer_t* b, data_t* d) +static void buffer_send(buffer_t* b, data_t* d) { int in; - sem_wait(&b->free); + sem_wait(b->free); if (use_locking) pthread_mutex_lock(&b->mutex_in); in = fetch_and_add(&b->in, 1); @@ -109,21 +135,21 @@ void buffer_send(buffer_t* b, data_t* d) printf("sent %d to buffer[%d]\n", *d, in); fflush(stdout); } - sem_post(&b->data); + sem_post(b->data); } -void buffer_destroy(buffer_t* b) +static void buffer_destroy(buffer_t* b) { - sem_destroy(&b->data); - sem_destroy(&b->free); + destroy_semaphore(DATA_SEMAPHORE_NAME, b->data); + destroy_semaphore(FREE_SEMAPHORE_NAME, b->free); pthread_mutex_destroy(&b->mutex_in); pthread_mutex_destroy(&b->mutex_out); } -buffer_t b; +static buffer_t b; -void producer(int* id) +static void producer(int* id) { buffer_send(&b, id); pthread_exit(NULL); @@ -131,7 +157,7 @@ void producer(int* id) #define MAXSLEEP (100 * 1000) -void consumer(int* id) +static void consumer(int* id) { int d; usleep(rand() % MAXSLEEP);