1 /* 2 * Copyright 2009-2012 Niels Provos and Nick Mathewson 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 */ 26 #include "event2/event-config.h" 27 28 /* With glibc we need to define this to get PTHREAD_MUTEX_RECURSIVE. */ 29 #define _GNU_SOURCE 30 #include <pthread.h> 31 32 struct event_base; 33 #include "event2/thread.h" 34 35 #include <stdlib.h> 36 #include <string.h> 37 #include "mm-internal.h" 38 #include "evthread-internal.h" 39 40 static pthread_mutexattr_t attr_recursive; 41 42 static void * 43 evthread_posix_lock_alloc(unsigned locktype) 44 { 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; 54 } 55 return lock; 56 } 57 58 static void 59 evthread_posix_lock_free(void *_lock, unsigned locktype) 60 { 61 pthread_mutex_t *lock = _lock; 62 pthread_mutex_destroy(lock); 63 mm_free(lock); 64 } 65 66 static int 67 evthread_posix_lock(unsigned mode, void *_lock) 68 { 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); 74 } 75 76 static int 77 evthread_posix_unlock(unsigned mode, void *_lock) 78 { 79 pthread_mutex_t *lock = _lock; 80 return pthread_mutex_unlock(lock); 81 } 82 83 static unsigned long 84 evthread_posix_get_id(void) 85 { 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; 99 } 100 101 static void * 102 evthread_posix_cond_alloc(unsigned condflags) 103 { 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; 110 } 111 return cond; 112 } 113 114 static void 115 evthread_posix_cond_free(void *_cond) 116 { 117 pthread_cond_t *cond = _cond; 118 pthread_cond_destroy(cond); 119 mm_free(cond); 120 } 121 122 static int 123 evthread_posix_cond_signal(void *_cond, int broadcast) 124 { 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; 132 } 133 134 static int 135 evthread_posix_cond_wait(void *_cond, void *_lock, const struct timeval *tv) 136 { 137 int r; 138 pthread_cond_t *cond = _cond; 139 pthread_mutex_t *lock = _lock; 140 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; 158 } 159 } 160 161 int 162 evthread_use_pthreads(void) 163 { 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 171 }; 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 178 }; 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; 184 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; 189 } 190