|
From: <sv...@va...> - 2009-07-23 18:22:11
|
Author: bart
Date: 2009-07-23 19:22:00 +0100 (Thu, 23 Jul 2009)
New Revision: 10568
Log:
Ported circular_buffer test to Darwin.
Modified:
trunk/drd/tests/circular_buffer.c
Modified: trunk/drd/tests/circular_buffer.c
===================================================================
--- trunk/drd/tests/circular_buffer.c 2009-07-23 18:10:55 UTC (rev 10567)
+++ trunk/drd/tests/circular_buffer.c 2009-07-23 18:22:00 UTC (rev 10568)
@@ -24,15 +24,17 @@
#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,11 +56,35 @@
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 @@
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 @@
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 @@
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 @@
#define MAXSLEEP (100 * 1000)
-void consumer(int* id)
+static void consumer(int* id)
{
int d;
usleep(rand() % MAXSLEEP);
|