Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2009-2012 Niels Provos and Nick Mathewson |
michael@0 | 3 | * |
michael@0 | 4 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | * modification, are permitted provided that the following conditions |
michael@0 | 6 | * are met: |
michael@0 | 7 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 8 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 9 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 10 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 11 | * documentation and/or other materials provided with the distribution. |
michael@0 | 12 | * 3. The name of the author may not be used to endorse or promote products |
michael@0 | 13 | * derived from this software without specific prior written permission. |
michael@0 | 14 | * |
michael@0 | 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
michael@0 | 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
michael@0 | 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
michael@0 | 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
michael@0 | 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
michael@0 | 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
michael@0 | 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 25 | */ |
michael@0 | 26 | #include "event2/event-config.h" |
michael@0 | 27 | |
michael@0 | 28 | /* With glibc we need to define this to get PTHREAD_MUTEX_RECURSIVE. */ |
michael@0 | 29 | #define _GNU_SOURCE |
michael@0 | 30 | #include <pthread.h> |
michael@0 | 31 | |
michael@0 | 32 | struct event_base; |
michael@0 | 33 | #include "event2/thread.h" |
michael@0 | 34 | |
michael@0 | 35 | #include <stdlib.h> |
michael@0 | 36 | #include <string.h> |
michael@0 | 37 | #include "mm-internal.h" |
michael@0 | 38 | #include "evthread-internal.h" |
michael@0 | 39 | |
michael@0 | 40 | static pthread_mutexattr_t attr_recursive; |
michael@0 | 41 | |
michael@0 | 42 | static void * |
michael@0 | 43 | evthread_posix_lock_alloc(unsigned locktype) |
michael@0 | 44 | { |
michael@0 | 45 | pthread_mutexattr_t *attr = NULL; |
michael@0 | 46 | pthread_mutex_t *lock = mm_malloc(sizeof(pthread_mutex_t)); |
michael@0 | 47 | if (!lock) |
michael@0 | 48 | return NULL; |
michael@0 | 49 | if (locktype & EVTHREAD_LOCKTYPE_RECURSIVE) |
michael@0 | 50 | attr = &attr_recursive; |
michael@0 | 51 | if (pthread_mutex_init(lock, attr)) { |
michael@0 | 52 | mm_free(lock); |
michael@0 | 53 | return NULL; |
michael@0 | 54 | } |
michael@0 | 55 | return lock; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | static void |
michael@0 | 59 | evthread_posix_lock_free(void *_lock, unsigned locktype) |
michael@0 | 60 | { |
michael@0 | 61 | pthread_mutex_t *lock = _lock; |
michael@0 | 62 | pthread_mutex_destroy(lock); |
michael@0 | 63 | mm_free(lock); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | static int |
michael@0 | 67 | evthread_posix_lock(unsigned mode, void *_lock) |
michael@0 | 68 | { |
michael@0 | 69 | pthread_mutex_t *lock = _lock; |
michael@0 | 70 | if (mode & EVTHREAD_TRY) |
michael@0 | 71 | return pthread_mutex_trylock(lock); |
michael@0 | 72 | else |
michael@0 | 73 | return pthread_mutex_lock(lock); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | static int |
michael@0 | 77 | evthread_posix_unlock(unsigned mode, void *_lock) |
michael@0 | 78 | { |
michael@0 | 79 | pthread_mutex_t *lock = _lock; |
michael@0 | 80 | return pthread_mutex_unlock(lock); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | static unsigned long |
michael@0 | 84 | evthread_posix_get_id(void) |
michael@0 | 85 | { |
michael@0 | 86 | union { |
michael@0 | 87 | pthread_t thr; |
michael@0 | 88 | #if _EVENT_SIZEOF_PTHREAD_T > _EVENT_SIZEOF_LONG |
michael@0 | 89 | ev_uint64_t id; |
michael@0 | 90 | #else |
michael@0 | 91 | unsigned long id; |
michael@0 | 92 | #endif |
michael@0 | 93 | } r; |
michael@0 | 94 | #if _EVENT_SIZEOF_PTHREAD_T < _EVENT_SIZEOF_LONG |
michael@0 | 95 | memset(&r, 0, sizeof(r)); |
michael@0 | 96 | #endif |
michael@0 | 97 | r.thr = pthread_self(); |
michael@0 | 98 | return (unsigned long)r.id; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | static void * |
michael@0 | 102 | evthread_posix_cond_alloc(unsigned condflags) |
michael@0 | 103 | { |
michael@0 | 104 | pthread_cond_t *cond = mm_malloc(sizeof(pthread_cond_t)); |
michael@0 | 105 | if (!cond) |
michael@0 | 106 | return NULL; |
michael@0 | 107 | if (pthread_cond_init(cond, NULL)) { |
michael@0 | 108 | mm_free(cond); |
michael@0 | 109 | return NULL; |
michael@0 | 110 | } |
michael@0 | 111 | return cond; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | static void |
michael@0 | 115 | evthread_posix_cond_free(void *_cond) |
michael@0 | 116 | { |
michael@0 | 117 | pthread_cond_t *cond = _cond; |
michael@0 | 118 | pthread_cond_destroy(cond); |
michael@0 | 119 | mm_free(cond); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | static int |
michael@0 | 123 | evthread_posix_cond_signal(void *_cond, int broadcast) |
michael@0 | 124 | { |
michael@0 | 125 | pthread_cond_t *cond = _cond; |
michael@0 | 126 | int r; |
michael@0 | 127 | if (broadcast) |
michael@0 | 128 | r = pthread_cond_broadcast(cond); |
michael@0 | 129 | else |
michael@0 | 130 | r = pthread_cond_signal(cond); |
michael@0 | 131 | return r ? -1 : 0; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | static int |
michael@0 | 135 | evthread_posix_cond_wait(void *_cond, void *_lock, const struct timeval *tv) |
michael@0 | 136 | { |
michael@0 | 137 | int r; |
michael@0 | 138 | pthread_cond_t *cond = _cond; |
michael@0 | 139 | pthread_mutex_t *lock = _lock; |
michael@0 | 140 | |
michael@0 | 141 | if (tv) { |
michael@0 | 142 | struct timeval now, abstime; |
michael@0 | 143 | struct timespec ts; |
michael@0 | 144 | evutil_gettimeofday(&now, NULL); |
michael@0 | 145 | evutil_timeradd(&now, tv, &abstime); |
michael@0 | 146 | ts.tv_sec = abstime.tv_sec; |
michael@0 | 147 | ts.tv_nsec = abstime.tv_usec*1000; |
michael@0 | 148 | r = pthread_cond_timedwait(cond, lock, &ts); |
michael@0 | 149 | if (r == ETIMEDOUT) |
michael@0 | 150 | return 1; |
michael@0 | 151 | else if (r) |
michael@0 | 152 | return -1; |
michael@0 | 153 | else |
michael@0 | 154 | return 0; |
michael@0 | 155 | } else { |
michael@0 | 156 | r = pthread_cond_wait(cond, lock); |
michael@0 | 157 | return r ? -1 : 0; |
michael@0 | 158 | } |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | int |
michael@0 | 162 | evthread_use_pthreads(void) |
michael@0 | 163 | { |
michael@0 | 164 | struct evthread_lock_callbacks cbs = { |
michael@0 | 165 | EVTHREAD_LOCK_API_VERSION, |
michael@0 | 166 | EVTHREAD_LOCKTYPE_RECURSIVE, |
michael@0 | 167 | evthread_posix_lock_alloc, |
michael@0 | 168 | evthread_posix_lock_free, |
michael@0 | 169 | evthread_posix_lock, |
michael@0 | 170 | evthread_posix_unlock |
michael@0 | 171 | }; |
michael@0 | 172 | struct evthread_condition_callbacks cond_cbs = { |
michael@0 | 173 | EVTHREAD_CONDITION_API_VERSION, |
michael@0 | 174 | evthread_posix_cond_alloc, |
michael@0 | 175 | evthread_posix_cond_free, |
michael@0 | 176 | evthread_posix_cond_signal, |
michael@0 | 177 | evthread_posix_cond_wait |
michael@0 | 178 | }; |
michael@0 | 179 | /* Set ourselves up to get recursive locks. */ |
michael@0 | 180 | if (pthread_mutexattr_init(&attr_recursive)) |
michael@0 | 181 | return -1; |
michael@0 | 182 | if (pthread_mutexattr_settype(&attr_recursive, PTHREAD_MUTEX_RECURSIVE)) |
michael@0 | 183 | return -1; |
michael@0 | 184 | |
michael@0 | 185 | evthread_set_lock_callbacks(&cbs); |
michael@0 | 186 | evthread_set_condition_callbacks(&cond_cbs); |
michael@0 | 187 | evthread_set_id_callback(evthread_posix_get_id); |
michael@0 | 188 | return 0; |
michael@0 | 189 | } |