svn commit: r215076 - in user/davidxu/libthr: include lib/libthr
lib/libthr/thread sys/sys
David Xu
davidxu at FreeBSD.org
Wed Nov 10 04:37:10 UTC 2010
Author: davidxu
Date: Wed Nov 10 04:37:09 2010
New Revision: 215076
URL: http://svn.freebsd.org/changeset/base/215076
Log:
Convert pthread_barrier_t from pointer to structure.
Modified:
user/davidxu/libthr/include/pthread.h
user/davidxu/libthr/lib/libthr/pthread.map
user/davidxu/libthr/lib/libthr/thread/thr_barrier.c
user/davidxu/libthr/lib/libthr/thread/thr_private.h
user/davidxu/libthr/sys/sys/_pthreadtypes.h
Modified: user/davidxu/libthr/include/pthread.h
==============================================================================
--- user/davidxu/libthr/include/pthread.h Wed Nov 10 04:35:37 2010 (r215075)
+++ user/davidxu/libthr/include/pthread.h Wed Nov 10 04:37:09 2010 (r215076)
@@ -195,6 +195,14 @@ struct pthread_rwlock {
__uint32_t __blocked_writers;
};
+struct pthread_barrier {
+ pthread_mutex_t __lock;
+ pthread_cond_t __cond;
+ __uint64_t __cycle;
+ __uint32_t __count;
+ __uint32_t __waiters;
+};
+
/*
* Thread function prototype definitions:
*/
Modified: user/davidxu/libthr/lib/libthr/pthread.map
==============================================================================
--- user/davidxu/libthr/lib/libthr/pthread.map Wed Nov 10 04:35:37 2010 (r215075)
+++ user/davidxu/libthr/lib/libthr/pthread.map Wed Nov 10 04:37:09 2010 (r215076)
@@ -23,9 +23,6 @@ FBSD_1.0 {
poll;
pselect;
pthread_atfork;
- pthread_barrier_destroy;
- pthread_barrier_init;
- pthread_barrier_wait;
pthread_barrierattr_destroy;
pthread_barrierattr_getpshared;
pthread_barrierattr_init;
@@ -376,6 +373,9 @@ FBSD_1.1 {
FBSD_1.2 {
openat;
+ pthread_barrier_destroy;
+ pthread_barrier_init;
+ pthread_barrier_wait;
pthread_cond_broadcast;
pthread_cond_destroy;
pthread_cond_init;
Modified: user/davidxu/libthr/lib/libthr/thread/thr_barrier.c
==============================================================================
--- user/davidxu/libthr/lib/libthr/thread/thr_barrier.c Wed Nov 10 04:35:37 2010 (r215075)
+++ user/davidxu/libthr/lib/libthr/thread/thr_barrier.c Wed Nov 10 04:37:09 2010 (r215076)
@@ -29,6 +29,7 @@
#include "namespace.h"
#include <errno.h>
#include <stdlib.h>
+#include <string.h>
#include <pthread.h>
#include "un-namespace.h"
@@ -38,76 +39,111 @@ __weak_reference(_pthread_barrier_init,
__weak_reference(_pthread_barrier_wait, pthread_barrier_wait);
__weak_reference(_pthread_barrier_destroy, pthread_barrier_destroy);
+typedef struct pthread_barrier *pthread_barrier_old_t;
+int _pthread_barrier_destroy_1_0(pthread_barrier_old_t *);
+int _pthread_barrier_wait_1_0(pthread_barrier_old_t *);
+int _pthread_barrier_init_1_0(pthread_barrier_old_t *,
+ const pthread_barrierattr_t *, unsigned);
+
int
-_pthread_barrier_destroy(pthread_barrier_t *barrier)
+_pthread_barrier_destroy(pthread_barrier_t *barp)
{
- pthread_barrier_t bar;
+ (void)_pthread_cond_destroy(&barp->__cond);
+ (void)_pthread_mutex_destroy(&barp->__lock);
+ memset(barp, -1, sizeof(*barp));
+ return (0);
+}
- if (barrier == NULL || *barrier == NULL)
+int
+_pthread_barrier_init(pthread_barrier_t *barp,
+ const pthread_barrierattr_t *attr, unsigned count)
+{
+ if (count == 0)
return (EINVAL);
- bar = *barrier;
- if (bar->b_waiters > 0)
- return (EBUSY);
- *barrier = NULL;
- free(bar);
+ _pthread_mutex_init(&barp->__lock, NULL);
+ _pthread_cond_init(&barp->__cond, NULL);
+ if (attr != NULL && *attr != NULL) {
+ if ((*attr)->pshared == PTHREAD_PROCESS_SHARED) {
+ barp->__lock.__lockflags |= USYNC_PROCESS_SHARED;
+ barp->__cond.__flags |= USYNC_PROCESS_SHARED;
+ } else if ((*attr)->pshared != PTHREAD_PROCESS_PRIVATE) {
+ return (EINVAL);
+ }
+ }
+ barp->__cycle = 0;
+ barp->__waiters = 0;
+ barp->__count = count;
return (0);
}
int
-_pthread_barrier_init(pthread_barrier_t *barrier,
- const pthread_barrierattr_t *attr, unsigned count)
+_pthread_barrier_wait(pthread_barrier_t *barp)
{
- pthread_barrier_t bar;
+ uint64_t cycle;
+ int error;
- (void)attr;
+ _pthread_mutex_lock(&barp->__lock);
+ if (++barp->__waiters == barp->__count) {
+ /* Current thread is lastest thread. */
+ barp->__waiters = 0;
+ barp->__cycle++;
+ _pthread_cond_broadcast(&barp->__cond);
+ _pthread_mutex_unlock(&barp->__lock);
+ error = PTHREAD_BARRIER_SERIAL_THREAD;
+ } else {
+ cycle = barp->__cycle;
+ do {
+ _pthread_cond_wait(&barp->__cond, &barp->__lock);
+ /* test cycle to avoid bogus wakeup */
+ } while (cycle == barp->__cycle);
+ _pthread_mutex_unlock(&barp->__lock);
+ error = 0;
+ }
+ return (error);
+}
- if (barrier == NULL || count <= 0)
- return (EINVAL);
+int
+_pthread_barrier_destroy_1_0(pthread_barrier_old_t *barpp)
+{
+ struct pthread_barrier *barp;
- bar = malloc(sizeof(struct pthread_barrier));
- if (bar == NULL)
- return (ENOMEM);
+ if ((barp = *barpp) == NULL)
+ return (EINVAL);
+ _pthread_barrier_destroy(barp);
+ free(barp);
+ return (0);
+}
- _thr_umutex_init(&bar->b_lock);
- _thr_ucond_init(&bar->b_cv);
- bar->b_cycle = 0;
- bar->b_waiters = 0;
- bar->b_count = count;
- *barrier = bar;
+int
+_pthread_barrier_init_1_0(pthread_barrier_old_t *barpp,
+ const pthread_barrierattr_t *attr, unsigned count)
+{
+ struct pthread_barrier *barp;
+ int error;
+ barp = malloc(sizeof(struct pthread_barrier));
+ if (barp == NULL)
+ return (ENOMEM);
+ error = _pthread_barrier_init(barp, attr, count);
+ if (error) {
+ free(barp);
+ return (error);
+ }
+ *barpp = barp;
return (0);
}
int
-_pthread_barrier_wait(pthread_barrier_t *barrier)
+_pthread_barrier_wait_1_0(pthread_barrier_old_t *barpp)
{
- struct pthread *curthread = _get_curthread();
- pthread_barrier_t bar;
- int64_t cycle;
- int ret;
+ struct pthread_barrier *barp;
- if (barrier == NULL || *barrier == NULL)
+ if ((barp = *barpp) == NULL)
return (EINVAL);
-
- bar = *barrier;
- THR_UMUTEX_LOCK(curthread, &bar->b_lock);
- if (++bar->b_waiters == bar->b_count) {
- /* Current thread is lastest thread */
- bar->b_waiters = 0;
- bar->b_cycle++;
- _thr_ucond_broadcast(&bar->b_cv);
- THR_UMUTEX_UNLOCK(curthread, &bar->b_lock);
- ret = PTHREAD_BARRIER_SERIAL_THREAD;
- } else {
- cycle = bar->b_cycle;
- do {
- _thr_ucond_wait(&bar->b_cv, &bar->b_lock, NULL, 0);
- THR_UMUTEX_LOCK(curthread, &bar->b_lock);
- /* test cycle to avoid bogus wakeup */
- } while (cycle == bar->b_cycle);
- THR_UMUTEX_UNLOCK(curthread, &bar->b_lock);
- ret = 0;
- }
- return (ret);
+ return _pthread_barrier_wait(barp);
}
+
+FB10_COMPAT(_pthread_barrier_destroy_1_0, pthread_barrier_destroy);
+FB10_COMPAT(_pthread_barrier_init_1_0, pthread_barrier_init);
+FB10_COMPAT(_pthread_barrier_wait_1_0, pthread_barrier_wait);
Modified: user/davidxu/libthr/lib/libthr/thread/thr_private.h
==============================================================================
--- user/davidxu/libthr/lib/libthr/thread/thr_private.h Wed Nov 10 04:35:37 2010 (r215075)
+++ user/davidxu/libthr/lib/libthr/thread/thr_private.h Wed Nov 10 04:37:09 2010 (r215076)
@@ -163,14 +163,6 @@ struct pthread_cond_attr {
int c_clockid;
};
-struct pthread_barrier {
- struct umutex b_lock;
- struct ucond b_cv;
- volatile int64_t b_cycle;
- volatile int b_count;
- volatile int b_waiters;
-};
-
struct pthread_barrierattr {
int pshared;
};
@@ -180,13 +172,6 @@ struct pthread_spinlock {
};
/*
- * Flags for condition variables.
- */
-#define COND_FLAGS_PRIVATE 0x01
-#define COND_FLAGS_INITED 0x02
-#define COND_FLAGS_BUSY 0x04
-
-/*
* Cleanup definitions.
*/
struct pthread_cleanup {
Modified: user/davidxu/libthr/sys/sys/_pthreadtypes.h
==============================================================================
--- user/davidxu/libthr/sys/sys/_pthreadtypes.h Wed Nov 10 04:35:37 2010 (r215075)
+++ user/davidxu/libthr/sys/sys/_pthreadtypes.h Wed Nov 10 04:37:09 2010 (r215076)
@@ -74,7 +74,7 @@ typedef int pthread_key_t;
typedef struct pthread_once pthread_once_t;
typedef struct pthread_rwlock pthread_rwlock_t;
typedef struct pthread_rwlockattr *pthread_rwlockattr_t;
-typedef struct pthread_barrier *pthread_barrier_t;
+typedef struct pthread_barrier pthread_barrier_t;
typedef struct pthread_barrierattr *pthread_barrierattr_t;
typedef struct pthread_spinlock *pthread_spinlock_t;
More information about the svn-src-user
mailing list