Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 192 lines (175 sloc) 4.948 kb
b85b710 Update copyright statements to reflect the facts that:
Nick Mathewson authored
1 /*
17efc1c Update all our copyright notices to say "2010"
Nick Mathewson authored
2 * Copyright 2009-2010 Niels Provos and Nick Mathewson
b85b710 Update copyright statements to reflect the facts that:
Nick Mathewson authored
3 *
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.
14 *
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.
25 */
ec347b9 Move event-config.h to include/event2
Nick Mathewson authored
26 #include "event2/event-config.h"
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
27
3b26541 @kev009 Remove internal usage of _GNU_SOURCE
kev009 authored
28 /* With glibc we need to define this to get PTHREAD_MUTEX_RECURSIVE.
29 * _GNU_SOURCE, now from event-private
30 */
31 #include "event-private.h"
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
32 #include <pthread.h>
ec35eb5 Make threading functions global, like the mm_ functions. Use the libeve...
Nick Mathewson authored
33
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
34 struct event_base;
fbe64f2 Use relative includes instead of system includes consistently.
Evan Jones authored
35 #include "event2/thread.h"
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
36
9bf124b Build correctly with mm replacement turned off.
Nick Mathewson authored
37 #include <stdlib.h>
fbaf077 Fix bugs in posix thread-id calculation when sizeof(pthread_t) != sizeof...
Nick Mathewson authored
38 #include <string.h>
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
39 #include "mm-internal.h"
d4977b5 Add a condition variable backend, with implementations for pthreads and ...
Nick Mathewson authored
40 #include "evthread-internal.h"
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
41
ec35eb5 Make threading functions global, like the mm_ functions. Use the libeve...
Nick Mathewson authored
42 static pthread_mutexattr_t attr_recursive;
43
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
44 static void *
347952f Revise the locking API: deprecate the old locking callbacks and add tryl...
Nick Mathewson authored
45 evthread_posix_lock_alloc(unsigned locktype)
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
46 {
347952f Revise the locking API: deprecate the old locking callbacks and add tryl...
Nick Mathewson authored
47 pthread_mutexattr_t *attr = NULL;
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
48 pthread_mutex_t *lock = mm_malloc(sizeof(pthread_mutex_t));
49 if (!lock)
50 return NULL;
347952f Revise the locking API: deprecate the old locking callbacks and add tryl...
Nick Mathewson authored
51 if (locktype & EVTHREAD_LOCKTYPE_RECURSIVE)
52 attr = &attr_recursive;
53 if (pthread_mutex_init(lock, attr)) {
ec35eb5 Make threading functions global, like the mm_ functions. Use the libeve...
Nick Mathewson authored
54 mm_free(lock);
55 return NULL;
56 }
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
57 return lock;
58 }
59
60 static void
347952f Revise the locking API: deprecate the old locking callbacks and add tryl...
Nick Mathewson authored
61 evthread_posix_lock_free(void *_lock, unsigned locktype)
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
62 {
63 pthread_mutex_t *lock = _lock;
64 pthread_mutex_destroy(lock);
65 mm_free(lock);
66 }
67
347952f Revise the locking API: deprecate the old locking callbacks and add tryl...
Nick Mathewson authored
68 static int
69 evthread_posix_lock(unsigned mode, void *_lock)
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
70 {
71 pthread_mutex_t *lock = _lock;
347952f Revise the locking API: deprecate the old locking callbacks and add tryl...
Nick Mathewson authored
72 if (mode & EVTHREAD_TRY)
73 return pthread_mutex_trylock(lock);
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
74 else
347952f Revise the locking API: deprecate the old locking callbacks and add tryl...
Nick Mathewson authored
75 return pthread_mutex_lock(lock);
76 }
77
78 static int
79 evthread_posix_unlock(unsigned mode, void *_lock)
80 {
81 pthread_mutex_t *lock = _lock;
82 return pthread_mutex_unlock(lock);
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
83 }
84
85 static unsigned long
86 evthread_posix_get_id(void)
87 {
88 union {
89 pthread_t thr;
fbaf077 Fix bugs in posix thread-id calculation when sizeof(pthread_t) != sizeof...
Nick Mathewson authored
90 #if _EVENT_SIZEOF_PTHREAD_T > _EVENT_SIZEOF_LONG
91 ev_uint64_t id;
92 #else
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
93 unsigned long id;
fbaf077 Fix bugs in posix thread-id calculation when sizeof(pthread_t) != sizeof...
Nick Mathewson authored
94 #endif
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
95 } r;
fbaf077 Fix bugs in posix thread-id calculation when sizeof(pthread_t) != sizeof...
Nick Mathewson authored
96 #if _EVENT_SIZEOF_PTHREAD_T < _EVENT_SIZEOF_LONG
97 memset(&r, 0, sizeof(r));
98 #endif
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
99 r.thr = pthread_self();
fbaf077 Fix bugs in posix thread-id calculation when sizeof(pthread_t) != sizeof...
Nick Mathewson authored
100 return (unsigned long)r.id;
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
101 }
102
d4977b5 Add a condition variable backend, with implementations for pthreads and ...
Nick Mathewson authored
103 static void *
104 evthread_posix_cond_alloc(unsigned condflags)
105 {
106 pthread_cond_t *cond = mm_malloc(sizeof(pthread_cond_t));
107 if (!cond)
108 return NULL;
109 if (pthread_cond_init(cond, NULL)) {
110 mm_free(cond);
111 return NULL;
112 }
113 return cond;
114 }
115
116 static void
117 evthread_posix_cond_free(void *_cond)
118 {
119 pthread_cond_t *cond = _cond;
120 pthread_cond_destroy(cond);
121 mm_free(cond);
122 }
123
124 static int
125 evthread_posix_cond_signal(void *_cond, int broadcast)
126 {
127 pthread_cond_t *cond = _cond;
128 int r;
129 if (broadcast)
130 r = pthread_cond_broadcast(cond);
131 else
132 r = pthread_cond_signal(cond);
133 return r ? -1 : 0;
134 }
135
136 static int
137 evthread_posix_cond_wait(void *_cond, void *_lock, const struct timeval *tv)
138 {
139 int r;
140 pthread_cond_t *cond = _cond;
141 pthread_mutex_t *lock = _lock;
142
143 if (tv) {
144 struct timeval now, abstime;
145 struct timespec ts;
146 evutil_gettimeofday(&now, NULL);
147 evutil_timeradd(&now, tv, &abstime);
148 ts.tv_sec = abstime.tv_sec;
149 ts.tv_nsec = abstime.tv_usec*1000;
150 r = pthread_cond_timedwait(cond, lock, &ts);
151 if (r == ETIMEDOUT)
152 return 1;
153 else if (r)
154 return -1;
155 else
156 return 0;
157 } else {
158 r = pthread_cond_wait(cond, lock);
159 return r ? -1 : 0;
160 }
161 }
162
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
163 int
ec35eb5 Make threading functions global, like the mm_ functions. Use the libeve...
Nick Mathewson authored
164 evthread_use_pthreads(void)
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
165 {
347952f Revise the locking API: deprecate the old locking callbacks and add tryl...
Nick Mathewson authored
166 struct evthread_lock_callbacks cbs = {
167 EVTHREAD_LOCK_API_VERSION,
168 EVTHREAD_LOCKTYPE_RECURSIVE,
169 evthread_posix_lock_alloc,
170 evthread_posix_lock_free,
171 evthread_posix_lock,
172 evthread_posix_unlock
173 };
d4977b5 Add a condition variable backend, with implementations for pthreads and ...
Nick Mathewson authored
174 struct evthread_condition_callbacks cond_cbs = {
175 EVTHREAD_CONDITION_API_VERSION,
176 evthread_posix_cond_alloc,
177 evthread_posix_cond_free,
178 evthread_posix_cond_signal,
179 evthread_posix_cond_wait
180 };
ec35eb5 Make threading functions global, like the mm_ functions. Use the libeve...
Nick Mathewson authored
181 /* Set ourselves up to get recursive locks. */
4ba6eda Make evthread_use_pthreads() actually return 0 on success.
Nick Mathewson authored
182 if (pthread_mutexattr_init(&attr_recursive))
183 return -1;
184 if (pthread_mutexattr_settype(&attr_recursive, PTHREAD_MUTEX_RECURSIVE))
185 return -1;
ec35eb5 Make threading functions global, like the mm_ functions. Use the libeve...
Nick Mathewson authored
186
347952f Revise the locking API: deprecate the old locking callbacks and add tryl...
Nick Mathewson authored
187 evthread_set_lock_callbacks(&cbs);
d4977b5 Add a condition variable backend, with implementations for pthreads and ...
Nick Mathewson authored
188 evthread_set_condition_callbacks(&cond_cbs);
ec35eb5 Make threading functions global, like the mm_ functions. Use the libeve...
Nick Mathewson authored
189 evthread_set_id_callback(evthread_posix_get_id);
4ba6eda Make evthread_use_pthreads() actually return 0 on success.
Nick Mathewson authored
190 return 0;
309fc7c New functions to provide sane threading callbacks with pthreads and win3...
Nick Mathewson authored
191 }
Something went wrong with that request. Please try again.