summaryrefslogtreecommitdiff
path: root/testing/embedding
diff options
context:
space:
mode:
authorArmin Rigo <arigo@tunes.org>2016-01-16 16:59:02 +0100
committerArmin Rigo <arigo@tunes.org>2016-01-16 16:59:02 +0100
commit6f821d64c3de69b68ce43e026fd222ce21efea9c (patch)
tree6e3f31a050a7104e47c1c97e53d752f4722a807b /testing/embedding
parentcfc6af0ab5e6c4932b02d872bad80e6943ede1e3 (diff)
downloadcffi-6f821d64c3de69b68ce43e026fd222ce21efea9c.tar.gz
Use mutex/condition variables instead of semaphores (for os/x)
Diffstat (limited to 'testing/embedding')
-rw-r--r--testing/embedding/perf-test.c24
-rw-r--r--testing/embedding/thread-test.h48
2 files changed, 55 insertions, 17 deletions
diff --git a/testing/embedding/perf-test.c b/testing/embedding/perf-test.c
index 2931186..2195bf5 100644
--- a/testing/embedding/perf-test.c
+++ b/testing/embedding/perf-test.c
@@ -3,8 +3,9 @@
#include <sys/time.h>
#ifdef PTEST_USE_THREAD
# include <pthread.h>
-# include <semaphore.h>
-static sem_t done;
+static pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
+static int remaining;
#endif
@@ -54,8 +55,11 @@ static void *start_routine(void *arg)
printf("time per call: %.3g\n", t);
#ifdef PTEST_USE_THREAD
- int status = sem_post(&done);
- assert(status == 0);
+ pthread_mutex_lock(&mutex1);
+ remaining -= 1;
+ if (!remaining)
+ pthread_cond_signal(&cond1);
+ pthread_mutex_unlock(&mutex1);
#endif
return arg;
@@ -68,19 +72,19 @@ int main(void)
start_routine(0);
#else
pthread_t th;
- int i, status = sem_init(&done, 0, 0);
- assert(status == 0);
+ int i, status;
add1(0, 0); /* this is the main thread */
+ remaining = PTEST_USE_THREAD;
for (i = 0; i < PTEST_USE_THREAD; i++) {
status = pthread_create(&th, NULL, start_routine, NULL);
assert(status == 0);
}
- for (i = 0; i < PTEST_USE_THREAD; i++) {
- status = sem_wait(&done);
- assert(status == 0);
- }
+ pthread_mutex_lock(&mutex1);
+ while (remaining)
+ pthread_cond_wait(&cond1, &mutex1);
+ pthread_mutex_unlock(&mutex1);
#endif
return 0;
}
diff --git a/testing/embedding/thread-test.h b/testing/embedding/thread-test.h
index 33a7e48..f66cf70 100644
--- a/testing/embedding/thread-test.h
+++ b/testing/embedding/thread-test.h
@@ -4,7 +4,41 @@
#include <pthread.h>
-#include <semaphore.h>
+
+/* don't include <semaphore.h>, it is not available on OS/X */
+
+typedef struct {
+ pthread_mutex_t mutex1;
+ pthread_cond_t cond1;
+ unsigned int value;
+} sem_t;
+
+static int sem_init(sem_t *sem, int pshared, unsigned int value)
+{
+ assert(pshared == 0);
+ sem->value = value;
+ return (pthread_mutex_init(&sem->mutex1, NULL) ||
+ pthread_cond_init(&sem->cond1, NULL));
+}
+
+static int sem_post(sem_t *sem)
+{
+ pthread_mutex_lock(&sem->mutex1);
+ sem->value += 1;
+ pthread_cond_signal(&sem->cond1);
+ pthread_mutex_unlock(&sem->mutex1);
+ return 0;
+}
+
+static int sem_wait(sem_t *sem)
+{
+ pthread_mutex_lock(&sem->mutex1);
+ while (sem->value == 0)
+ pthread_cond_wait(&sem->cond1, &sem->mutex1);
+ sem->value -= 1;
+ pthread_mutex_unlock(&sem->mutex1);
+ return 0;
+}
/************************************************************/
@@ -22,7 +56,7 @@
typedef HANDLE sem_t;
typedef HANDLE pthread_t;
-int sem_init(sem_t *sem, int pshared, unsigned int value)
+static int sem_init(sem_t *sem, int pshared, unsigned int value)
{
assert(pshared == 0);
assert(value == 0);
@@ -30,26 +64,26 @@ int sem_init(sem_t *sem, int pshared, unsigned int value)
return *sem ? 0 : -1;
}
-int sem_post(sem_t *sem)
+static int sem_post(sem_t *sem)
{
return ReleaseSemaphore(*sem, 1, NULL) ? 0 : -1;
}
-int sem_wait(sem_t *sem)
+static int sem_wait(sem_t *sem)
{
WaitForSingleObject(*sem, INFINITE);
return 0;
}
-DWORD WINAPI myThreadProc(LPVOID lpParameter)
+static DWORD WINAPI myThreadProc(LPVOID lpParameter)
{
void *(* start_routine)(void *) = (void *(*)(void *))lpParameter;
start_routine(NULL);
return 0;
}
-int pthread_create(pthread_t *thread, void *attr,
- void *start_routine(void *), void *arg)
+static int pthread_create(pthread_t *thread, void *attr,
+ void *start_routine(void *), void *arg)
{
assert(arg == NULL);
*thread = CreateThread(NULL, 0, myThreadProc, start_routine, 0, NULL);