MiniDLNA update: 1.0.19.1 to 1.0.20
[tomato.git] / release / src / router / libevent / evthread_pthread.c
blob55495a6ca3a4de11381c296fc3d3258d389a6315
1 /*
2 * Copyright 2009-2010 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
6 * are met:
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. */
29 #define _GNU_SOURCE
30 #include <pthread.h>
32 struct event_base;
33 #include "event2/thread.h"
35 #include <stdlib.h>
36 #include <string.h>
37 #include "mm-internal.h"
38 #include "evthread-internal.h"
40 static pthread_mutexattr_t attr_recursive;
42 static void *
43 evthread_posix_lock_alloc(unsigned locktype)
45 pthread_mutexattr_t *attr = NULL;
46 pthread_mutex_t *lock = mm_malloc(sizeof(pthread_mutex_t));
47 if (!lock)
48 return NULL;
49 if (locktype & EVTHREAD_LOCKTYPE_RECURSIVE)
50 attr = &attr_recursive;
51 if (pthread_mutex_init(lock, attr)) {
52 mm_free(lock);
53 return NULL;
55 return lock;
58 static void
59 evthread_posix_lock_free(void *_lock, unsigned locktype)
61 pthread_mutex_t *lock = _lock;
62 pthread_mutex_destroy(lock);
63 mm_free(lock);
66 static int
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);
72 else
73 return pthread_mutex_lock(lock);
76 static int
77 evthread_posix_unlock(unsigned mode, void *_lock)
79 pthread_mutex_t *lock = _lock;
80 return pthread_mutex_unlock(lock);
83 static unsigned long
84 evthread_posix_get_id(void)
86 union {
87 pthread_t thr;
88 #if _EVENT_SIZEOF_PTHREAD_T > _EVENT_SIZEOF_LONG
89 ev_uint64_t id;
90 #else
91 unsigned long id;
92 #endif
93 } r;
94 #if _EVENT_SIZEOF_PTHREAD_T < _EVENT_SIZEOF_LONG
95 memset(&r, 0, sizeof(r));
96 #endif
97 r.thr = pthread_self();
98 return (unsigned long)r.id;
101 static void *
102 evthread_posix_cond_alloc(unsigned condflags)
104 pthread_cond_t *cond = mm_malloc(sizeof(pthread_cond_t));
105 if (!cond)
106 return NULL;
107 if (pthread_cond_init(cond, NULL)) {
108 mm_free(cond);
109 return NULL;
111 return cond;
114 static void
115 evthread_posix_cond_free(void *_cond)
117 pthread_cond_t *cond = _cond;
118 pthread_cond_destroy(cond);
119 mm_free(cond);
122 static int
123 evthread_posix_cond_signal(void *_cond, int broadcast)
125 pthread_cond_t *cond = _cond;
126 int r;
127 if (broadcast)
128 r = pthread_cond_broadcast(cond);
129 else
130 r = pthread_cond_signal(cond);
131 return r ? -1 : 0;
134 static int
135 evthread_posix_cond_wait(void *_cond, void *_lock, const struct timeval *tv)
137 int r;
138 pthread_cond_t *cond = _cond;
139 pthread_mutex_t *lock = _lock;
141 if (tv) {
142 struct timeval now, abstime;
143 struct timespec ts;
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);
149 if (r == ETIMEDOUT)
150 return 1;
151 else if (r)
152 return -1;
153 else
154 return 0;
155 } else {
156 r = pthread_cond_wait(cond, lock);
157 return r ? -1 : 0;
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,
169 evthread_posix_lock,
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))
181 return -1;
182 if (pthread_mutexattr_settype(&attr_recursive, PTHREAD_MUTEX_RECURSIVE))
183 return -1;
185 evthread_set_lock_callbacks(&cbs);
186 evthread_set_condition_callbacks(&cond_cbs);
187 evthread_set_id_callback(evthread_posix_get_id);
188 return 0;