X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fovs-thread.c;h=c1560703bab9600ee53053804d0f352c98295e28;hb=591cb419cf3694e0ae66a95973e73c61bad9e03d;hp=c8b2c15999439a37b530f4ce62556c20d86b6167;hpb=97be153858b4cd175cbe7862b8e1624bf22ab98a;p=sliver-openvswitch.git diff --git a/lib/ovs-thread.c b/lib/ovs-thread.c index c8b2c1599..c1560703b 100644 --- a/lib/ovs-thread.c +++ b/lib/ovs-thread.c @@ -114,6 +114,8 @@ UNLOCK_FUNCTION(rwlock, destroy); } \ } +XPTHREAD_FUNC1(pthread_mutex_lock, pthread_mutex_t *); +XPTHREAD_FUNC1(pthread_mutex_unlock, pthread_mutex_t *); XPTHREAD_FUNC1(pthread_mutexattr_init, pthread_mutexattr_t *); XPTHREAD_FUNC1(pthread_mutexattr_destroy, pthread_mutexattr_t *); XPTHREAD_FUNC2(pthread_mutexattr_settype, pthread_mutexattr_t *, int); @@ -124,8 +126,11 @@ XPTHREAD_FUNC1(pthread_cond_destroy, pthread_cond_t *); XPTHREAD_FUNC1(pthread_cond_signal, pthread_cond_t *); XPTHREAD_FUNC1(pthread_cond_broadcast, pthread_cond_t *); +XPTHREAD_FUNC2(pthread_join, pthread_t, void **); + typedef void destructor_func(void *); XPTHREAD_FUNC2(pthread_key_create, pthread_key_t *, destructor_func *); +XPTHREAD_FUNC2(pthread_setspecific, pthread_key_t, const void *); void ovs_mutex_init(const struct ovs_mutex *l_, int type) @@ -166,18 +171,49 @@ ovs_mutex_cond_wait(pthread_cond_t *cond, const struct ovs_mutex *mutex_) ovs_abort(error, "pthread_cond_wait failed"); } } + +DEFINE_EXTERN_PER_THREAD_DATA(ovsthread_id, 0); + +struct ovsthread_aux { + void *(*start)(void *); + void *arg; +}; + +static void * +ovsthread_wrapper(void *aux_) +{ + static atomic_uint next_id = ATOMIC_VAR_INIT(1); + + struct ovsthread_aux *auxp = aux_; + struct ovsthread_aux aux; + unsigned int id; + + atomic_add(&next_id, 1, &id); + *ovsthread_id_get() = id; + + aux = *auxp; + free(auxp); + + return aux.start(aux.arg); +} void xpthread_create(pthread_t *threadp, pthread_attr_t *attr, void *(*start)(void *), void *arg) { + struct ovsthread_aux *aux; pthread_t thread; int error; forbid_forking("multiple threads exist"); multithreaded = true; - error = pthread_create(threadp ? threadp : &thread, attr, start, arg); + aux = xmalloc(sizeof *aux); + aux->start = start; + aux->arg = arg; + + error = pthread_create(threadp ? threadp : &thread, attr, + ovsthread_wrapper, aux); if (error) { ovs_abort(error, "pthread_create failed"); }