2 * Copyright 2009-2012 Niels Provos and Nick Mathewson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "event2/event-config.h"
28 /* With glibc we need to define this to get PTHREAD_MUTEX_RECURSIVE. */
33 #include "event2/thread.h"
37 #include "mm-internal.h"
38 #include "evthread-internal.h"
40 static pthread_mutexattr_t attr_recursive
;
43 evthread_posix_lock_alloc(unsigned locktype
)
45 pthread_mutexattr_t
*attr
= NULL
;
46 pthread_mutex_t
*lock
= mm_malloc(sizeof(pthread_mutex_t
));
49 if (locktype
& EVTHREAD_LOCKTYPE_RECURSIVE
)
50 attr
= &attr_recursive
;
51 if (pthread_mutex_init(lock
, attr
)) {
59 evthread_posix_lock_free(void *_lock
, unsigned locktype
)
61 pthread_mutex_t
*lock
= _lock
;
62 pthread_mutex_destroy(lock
);
67 evthread_posix_lock(unsigned mode
, void *_lock
)
69 pthread_mutex_t
*lock
= _lock
;
70 if (mode
& EVTHREAD_TRY
)
71 return pthread_mutex_trylock(lock
);
73 return pthread_mutex_lock(lock
);
77 evthread_posix_unlock(unsigned mode
, void *_lock
)
79 pthread_mutex_t
*lock
= _lock
;
80 return pthread_mutex_unlock(lock
);
84 evthread_posix_get_id(void)
88 #if _EVENT_SIZEOF_PTHREAD_T > _EVENT_SIZEOF_LONG
94 #if _EVENT_SIZEOF_PTHREAD_T < _EVENT_SIZEOF_LONG
95 memset(&r
, 0, sizeof(r
));
97 r
.thr
= pthread_self();
98 return (unsigned long)r
.id
;
102 evthread_posix_cond_alloc(unsigned condflags
)
104 pthread_cond_t
*cond
= mm_malloc(sizeof(pthread_cond_t
));
107 if (pthread_cond_init(cond
, NULL
)) {
115 evthread_posix_cond_free(void *_cond
)
117 pthread_cond_t
*cond
= _cond
;
118 pthread_cond_destroy(cond
);
123 evthread_posix_cond_signal(void *_cond
, int broadcast
)
125 pthread_cond_t
*cond
= _cond
;
128 r
= pthread_cond_broadcast(cond
);
130 r
= pthread_cond_signal(cond
);
135 evthread_posix_cond_wait(void *_cond
, void *_lock
, const struct timeval
*tv
)
138 pthread_cond_t
*cond
= _cond
;
139 pthread_mutex_t
*lock
= _lock
;
142 struct timeval now
, abstime
;
144 evutil_gettimeofday(&now
, NULL
);
145 evutil_timeradd(&now
, tv
, &abstime
);
146 ts
.tv_sec
= abstime
.tv_sec
;
147 ts
.tv_nsec
= abstime
.tv_usec
*1000;
148 r
= pthread_cond_timedwait(cond
, lock
, &ts
);
156 r
= pthread_cond_wait(cond
, lock
);
162 evthread_use_pthreads(void)
164 struct evthread_lock_callbacks cbs
= {
165 EVTHREAD_LOCK_API_VERSION
,
166 EVTHREAD_LOCKTYPE_RECURSIVE
,
167 evthread_posix_lock_alloc
,
168 evthread_posix_lock_free
,
170 evthread_posix_unlock
172 struct evthread_condition_callbacks cond_cbs
= {
173 EVTHREAD_CONDITION_API_VERSION
,
174 evthread_posix_cond_alloc
,
175 evthread_posix_cond_free
,
176 evthread_posix_cond_signal
,
177 evthread_posix_cond_wait
179 /* Set ourselves up to get recursive locks. */
180 if (pthread_mutexattr_init(&attr_recursive
))
182 if (pthread_mutexattr_settype(&attr_recursive
, PTHREAD_MUTEX_RECURSIVE
))
185 evthread_set_lock_callbacks(&cbs
);
186 evthread_set_condition_callbacks(&cond_cbs
);
187 evthread_set_id_callback(evthread_posix_get_id
);