[pypy-commit] cffi default: Use mutex/condition variables instead of semaphores (for os/x)
arigo
pypy.commits at gmail.com
Sat Jan 16 10:59:15 EST 2016
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r2601:08bf0039b46e
Date: 2016-01-16 16:59 +0100
http://bitbucket.org/cffi/cffi/changeset/08bf0039b46e/
Log: Use mutex/condition variables instead of semaphores (for os/x)
diff --git a/testing/embedding/perf-test.c b/testing/embedding/perf-test.c
--- 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 @@
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 @@
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
--- 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 @@
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);
More information about the pypy-commit
mailing list