1*2b15cb3dSCy Schubert /*
2*2b15cb3dSCy Schubert * Copyright 2009-2012 Niels Provos and Nick Mathewson
3*2b15cb3dSCy Schubert *
4*2b15cb3dSCy Schubert * Redistribution and use in source and binary forms, with or without
5*2b15cb3dSCy Schubert * modification, are permitted provided that the following conditions
6*2b15cb3dSCy Schubert * are met:
7*2b15cb3dSCy Schubert * 1. Redistributions of source code must retain the above copyright
8*2b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer.
9*2b15cb3dSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright
10*2b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer in the
11*2b15cb3dSCy Schubert * documentation and/or other materials provided with the distribution.
12*2b15cb3dSCy Schubert * 3. The name of the author may not be used to endorse or promote products
13*2b15cb3dSCy Schubert * derived from this software without specific prior written permission.
14*2b15cb3dSCy Schubert *
15*2b15cb3dSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*2b15cb3dSCy Schubert * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*2b15cb3dSCy Schubert * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*2b15cb3dSCy Schubert * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*2b15cb3dSCy Schubert * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*2b15cb3dSCy Schubert * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*2b15cb3dSCy Schubert * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*2b15cb3dSCy Schubert * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*2b15cb3dSCy Schubert * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*2b15cb3dSCy Schubert * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*2b15cb3dSCy Schubert */
26*2b15cb3dSCy Schubert #include "event2/event-config.h"
27*2b15cb3dSCy Schubert #include "evconfig-private.h"
28*2b15cb3dSCy Schubert
29*2b15cb3dSCy Schubert /* With glibc we need to define _GNU_SOURCE to get PTHREAD_MUTEX_RECURSIVE.
30*2b15cb3dSCy Schubert * This comes from evconfig-private.h
31*2b15cb3dSCy Schubert */
32*2b15cb3dSCy Schubert #include <pthread.h>
33*2b15cb3dSCy Schubert
34*2b15cb3dSCy Schubert struct event_base;
35*2b15cb3dSCy Schubert #include "event2/thread.h"
36*2b15cb3dSCy Schubert
37*2b15cb3dSCy Schubert #include <stdlib.h>
38*2b15cb3dSCy Schubert #include <string.h>
39*2b15cb3dSCy Schubert #include "mm-internal.h"
40*2b15cb3dSCy Schubert #include "evthread-internal.h"
41*2b15cb3dSCy Schubert
42*2b15cb3dSCy Schubert static pthread_mutexattr_t attr_recursive;
43*2b15cb3dSCy Schubert
44*2b15cb3dSCy Schubert static void *
evthread_posix_lock_alloc(unsigned locktype)45*2b15cb3dSCy Schubert evthread_posix_lock_alloc(unsigned locktype)
46*2b15cb3dSCy Schubert {
47*2b15cb3dSCy Schubert pthread_mutexattr_t *attr = NULL;
48*2b15cb3dSCy Schubert pthread_mutex_t *lock = mm_malloc(sizeof(pthread_mutex_t));
49*2b15cb3dSCy Schubert if (!lock)
50*2b15cb3dSCy Schubert return NULL;
51*2b15cb3dSCy Schubert if (locktype & EVTHREAD_LOCKTYPE_RECURSIVE)
52*2b15cb3dSCy Schubert attr = &attr_recursive;
53*2b15cb3dSCy Schubert if (pthread_mutex_init(lock, attr)) {
54*2b15cb3dSCy Schubert mm_free(lock);
55*2b15cb3dSCy Schubert return NULL;
56*2b15cb3dSCy Schubert }
57*2b15cb3dSCy Schubert return lock;
58*2b15cb3dSCy Schubert }
59*2b15cb3dSCy Schubert
60*2b15cb3dSCy Schubert static void
evthread_posix_lock_free(void * lock_,unsigned locktype)61*2b15cb3dSCy Schubert evthread_posix_lock_free(void *lock_, unsigned locktype)
62*2b15cb3dSCy Schubert {
63*2b15cb3dSCy Schubert pthread_mutex_t *lock = lock_;
64*2b15cb3dSCy Schubert pthread_mutex_destroy(lock);
65*2b15cb3dSCy Schubert mm_free(lock);
66*2b15cb3dSCy Schubert }
67*2b15cb3dSCy Schubert
68*2b15cb3dSCy Schubert static int
evthread_posix_lock(unsigned mode,void * lock_)69*2b15cb3dSCy Schubert evthread_posix_lock(unsigned mode, void *lock_)
70*2b15cb3dSCy Schubert {
71*2b15cb3dSCy Schubert pthread_mutex_t *lock = lock_;
72*2b15cb3dSCy Schubert if (mode & EVTHREAD_TRY)
73*2b15cb3dSCy Schubert return pthread_mutex_trylock(lock);
74*2b15cb3dSCy Schubert else
75*2b15cb3dSCy Schubert return pthread_mutex_lock(lock);
76*2b15cb3dSCy Schubert }
77*2b15cb3dSCy Schubert
78*2b15cb3dSCy Schubert static int
evthread_posix_unlock(unsigned mode,void * lock_)79*2b15cb3dSCy Schubert evthread_posix_unlock(unsigned mode, void *lock_)
80*2b15cb3dSCy Schubert {
81*2b15cb3dSCy Schubert pthread_mutex_t *lock = lock_;
82*2b15cb3dSCy Schubert return pthread_mutex_unlock(lock);
83*2b15cb3dSCy Schubert }
84*2b15cb3dSCy Schubert
85*2b15cb3dSCy Schubert static unsigned long
evthread_posix_get_id(void)86*2b15cb3dSCy Schubert evthread_posix_get_id(void)
87*2b15cb3dSCy Schubert {
88*2b15cb3dSCy Schubert union {
89*2b15cb3dSCy Schubert pthread_t thr;
90*2b15cb3dSCy Schubert #if EVENT__SIZEOF_PTHREAD_T > EVENT__SIZEOF_LONG
91*2b15cb3dSCy Schubert ev_uint64_t id;
92*2b15cb3dSCy Schubert #else
93*2b15cb3dSCy Schubert unsigned long id;
94*2b15cb3dSCy Schubert #endif
95*2b15cb3dSCy Schubert } r;
96*2b15cb3dSCy Schubert #if EVENT__SIZEOF_PTHREAD_T < EVENT__SIZEOF_LONG
97*2b15cb3dSCy Schubert memset(&r, 0, sizeof(r));
98*2b15cb3dSCy Schubert #endif
99*2b15cb3dSCy Schubert r.thr = pthread_self();
100*2b15cb3dSCy Schubert return (unsigned long)r.id;
101*2b15cb3dSCy Schubert }
102*2b15cb3dSCy Schubert
103*2b15cb3dSCy Schubert static void *
evthread_posix_cond_alloc(unsigned condflags)104*2b15cb3dSCy Schubert evthread_posix_cond_alloc(unsigned condflags)
105*2b15cb3dSCy Schubert {
106*2b15cb3dSCy Schubert pthread_cond_t *cond = mm_malloc(sizeof(pthread_cond_t));
107*2b15cb3dSCy Schubert if (!cond)
108*2b15cb3dSCy Schubert return NULL;
109*2b15cb3dSCy Schubert if (pthread_cond_init(cond, NULL)) {
110*2b15cb3dSCy Schubert mm_free(cond);
111*2b15cb3dSCy Schubert return NULL;
112*2b15cb3dSCy Schubert }
113*2b15cb3dSCy Schubert return cond;
114*2b15cb3dSCy Schubert }
115*2b15cb3dSCy Schubert
116*2b15cb3dSCy Schubert static void
evthread_posix_cond_free(void * cond_)117*2b15cb3dSCy Schubert evthread_posix_cond_free(void *cond_)
118*2b15cb3dSCy Schubert {
119*2b15cb3dSCy Schubert pthread_cond_t *cond = cond_;
120*2b15cb3dSCy Schubert pthread_cond_destroy(cond);
121*2b15cb3dSCy Schubert mm_free(cond);
122*2b15cb3dSCy Schubert }
123*2b15cb3dSCy Schubert
124*2b15cb3dSCy Schubert static int
evthread_posix_cond_signal(void * cond_,int broadcast)125*2b15cb3dSCy Schubert evthread_posix_cond_signal(void *cond_, int broadcast)
126*2b15cb3dSCy Schubert {
127*2b15cb3dSCy Schubert pthread_cond_t *cond = cond_;
128*2b15cb3dSCy Schubert int r;
129*2b15cb3dSCy Schubert if (broadcast)
130*2b15cb3dSCy Schubert r = pthread_cond_broadcast(cond);
131*2b15cb3dSCy Schubert else
132*2b15cb3dSCy Schubert r = pthread_cond_signal(cond);
133*2b15cb3dSCy Schubert return r ? -1 : 0;
134*2b15cb3dSCy Schubert }
135*2b15cb3dSCy Schubert
136*2b15cb3dSCy Schubert static int
evthread_posix_cond_wait(void * cond_,void * lock_,const struct timeval * tv)137*2b15cb3dSCy Schubert evthread_posix_cond_wait(void *cond_, void *lock_, const struct timeval *tv)
138*2b15cb3dSCy Schubert {
139*2b15cb3dSCy Schubert int r;
140*2b15cb3dSCy Schubert pthread_cond_t *cond = cond_;
141*2b15cb3dSCy Schubert pthread_mutex_t *lock = lock_;
142*2b15cb3dSCy Schubert
143*2b15cb3dSCy Schubert if (tv) {
144*2b15cb3dSCy Schubert struct timeval now, abstime;
145*2b15cb3dSCy Schubert struct timespec ts;
146*2b15cb3dSCy Schubert evutil_gettimeofday(&now, NULL);
147*2b15cb3dSCy Schubert evutil_timeradd(&now, tv, &abstime);
148*2b15cb3dSCy Schubert ts.tv_sec = abstime.tv_sec;
149*2b15cb3dSCy Schubert ts.tv_nsec = abstime.tv_usec*1000;
150*2b15cb3dSCy Schubert r = pthread_cond_timedwait(cond, lock, &ts);
151*2b15cb3dSCy Schubert if (r == ETIMEDOUT)
152*2b15cb3dSCy Schubert return 1;
153*2b15cb3dSCy Schubert else if (r)
154*2b15cb3dSCy Schubert return -1;
155*2b15cb3dSCy Schubert else
156*2b15cb3dSCy Schubert return 0;
157*2b15cb3dSCy Schubert } else {
158*2b15cb3dSCy Schubert r = pthread_cond_wait(cond, lock);
159*2b15cb3dSCy Schubert return r ? -1 : 0;
160*2b15cb3dSCy Schubert }
161*2b15cb3dSCy Schubert }
162*2b15cb3dSCy Schubert
163*2b15cb3dSCy Schubert int
evthread_use_pthreads(void)164*2b15cb3dSCy Schubert evthread_use_pthreads(void)
165*2b15cb3dSCy Schubert {
166*2b15cb3dSCy Schubert struct evthread_lock_callbacks cbs = {
167*2b15cb3dSCy Schubert EVTHREAD_LOCK_API_VERSION,
168*2b15cb3dSCy Schubert EVTHREAD_LOCKTYPE_RECURSIVE,
169*2b15cb3dSCy Schubert evthread_posix_lock_alloc,
170*2b15cb3dSCy Schubert evthread_posix_lock_free,
171*2b15cb3dSCy Schubert evthread_posix_lock,
172*2b15cb3dSCy Schubert evthread_posix_unlock
173*2b15cb3dSCy Schubert };
174*2b15cb3dSCy Schubert struct evthread_condition_callbacks cond_cbs = {
175*2b15cb3dSCy Schubert EVTHREAD_CONDITION_API_VERSION,
176*2b15cb3dSCy Schubert evthread_posix_cond_alloc,
177*2b15cb3dSCy Schubert evthread_posix_cond_free,
178*2b15cb3dSCy Schubert evthread_posix_cond_signal,
179*2b15cb3dSCy Schubert evthread_posix_cond_wait
180*2b15cb3dSCy Schubert };
181*2b15cb3dSCy Schubert /* Set ourselves up to get recursive locks. */
182*2b15cb3dSCy Schubert if (pthread_mutexattr_init(&attr_recursive))
183*2b15cb3dSCy Schubert return -1;
184*2b15cb3dSCy Schubert if (pthread_mutexattr_settype(&attr_recursive, PTHREAD_MUTEX_RECURSIVE))
185*2b15cb3dSCy Schubert return -1;
186*2b15cb3dSCy Schubert
187*2b15cb3dSCy Schubert evthread_set_lock_callbacks(&cbs);
188*2b15cb3dSCy Schubert evthread_set_condition_callbacks(&cond_cbs);
189*2b15cb3dSCy Schubert evthread_set_id_callback(evthread_posix_get_id);
190*2b15cb3dSCy Schubert return 0;
191*2b15cb3dSCy Schubert }
192