1 /* $NetBSD: evthread_pthread.c,v 1.1.1.1 2013/04/11 16:43:25 christos Exp $ */
3 * Copyright 2009-2012 Niels Provos and Nick Mathewson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "event2/event-config.h"
28 #include <sys/cdefs.h>
29 __RCSID("$NetBSD: evthread_pthread.c,v 1.1.1.1 2013/04/11 16:43:25 christos Exp $");
31 /* With glibc we need to define this to get PTHREAD_MUTEX_RECURSIVE. */
36 #include "event2/thread.h"
40 #include "mm-internal.h"
41 #include "evthread-internal.h"
43 static pthread_mutexattr_t attr_recursive
;
46 evthread_posix_lock_alloc(unsigned locktype
)
48 pthread_mutexattr_t
*attr
= NULL
;
49 pthread_mutex_t
*lock
= mm_malloc(sizeof(pthread_mutex_t
));
52 if (locktype
& EVTHREAD_LOCKTYPE_RECURSIVE
)
53 attr
= &attr_recursive
;
54 if (pthread_mutex_init(lock
, attr
)) {
62 evthread_posix_lock_free(void *_lock
, unsigned locktype
)
64 pthread_mutex_t
*lock
= _lock
;
65 pthread_mutex_destroy(lock
);
70 evthread_posix_lock(unsigned mode
, void *_lock
)
72 pthread_mutex_t
*lock
= _lock
;
73 if (mode
& EVTHREAD_TRY
)
74 return pthread_mutex_trylock(lock
);
76 return pthread_mutex_lock(lock
);
80 evthread_posix_unlock(unsigned mode
, void *_lock
)
82 pthread_mutex_t
*lock
= _lock
;
83 return pthread_mutex_unlock(lock
);
87 evthread_posix_get_id(void)
91 #if _EVENT_SIZEOF_PTHREAD_T > _EVENT_SIZEOF_LONG
97 #if _EVENT_SIZEOF_PTHREAD_T < _EVENT_SIZEOF_LONG
98 memset(&r
, 0, sizeof(r
));
100 r
.thr
= pthread_self();
101 return (unsigned long)r
.id
;
105 evthread_posix_cond_alloc(unsigned condflags
)
107 pthread_cond_t
*cond
= mm_malloc(sizeof(pthread_cond_t
));
110 if (pthread_cond_init(cond
, NULL
)) {
118 evthread_posix_cond_free(void *_cond
)
120 pthread_cond_t
*cond
= _cond
;
121 pthread_cond_destroy(cond
);
126 evthread_posix_cond_signal(void *_cond
, int broadcast
)
128 pthread_cond_t
*cond
= _cond
;
131 r
= pthread_cond_broadcast(cond
);
133 r
= pthread_cond_signal(cond
);
138 evthread_posix_cond_wait(void *_cond
, void *_lock
, const struct timeval
*tv
)
141 pthread_cond_t
*cond
= _cond
;
142 pthread_mutex_t
*lock
= _lock
;
145 struct timeval now
, abstime
;
147 evutil_gettimeofday(&now
, NULL
);
148 evutil_timeradd(&now
, tv
, &abstime
);
149 ts
.tv_sec
= abstime
.tv_sec
;
150 ts
.tv_nsec
= abstime
.tv_usec
*1000;
151 r
= pthread_cond_timedwait(cond
, lock
, &ts
);
159 r
= pthread_cond_wait(cond
, lock
);
165 evthread_use_pthreads(void)
167 struct evthread_lock_callbacks cbs
= {
168 EVTHREAD_LOCK_API_VERSION
,
169 EVTHREAD_LOCKTYPE_RECURSIVE
,
170 evthread_posix_lock_alloc
,
171 evthread_posix_lock_free
,
173 evthread_posix_unlock
175 struct evthread_condition_callbacks cond_cbs
= {
176 EVTHREAD_CONDITION_API_VERSION
,
177 evthread_posix_cond_alloc
,
178 evthread_posix_cond_free
,
179 evthread_posix_cond_signal
,
180 evthread_posix_cond_wait
182 /* Set ourselves up to get recursive locks. */
183 if (pthread_mutexattr_init(&attr_recursive
))
185 if (pthread_mutexattr_settype(&attr_recursive
, PTHREAD_MUTEX_RECURSIVE
))
188 evthread_set_lock_callbacks(&cbs
);
189 evthread_set_condition_callbacks(&cond_cbs
);
190 evthread_set_id_callback(evthread_posix_get_id
);