This source file includes following definitions.
- evthread_posix_lock_alloc
- evthread_posix_lock_free
- evthread_posix_lock
- evthread_posix_unlock
- evthread_posix_get_id
- evthread_posix_cond_alloc
- evthread_posix_cond_free
- evthread_posix_cond_signal
- evthread_posix_cond_wait
- evthread_use_pthreads
   1 
   2 
   3 
   4 
   5 
   6 
   7 
   8 
   9 
  10 
  11 
  12 
  13 
  14 
  15 
  16 
  17 
  18 
  19 
  20 
  21 
  22 
  23 
  24 
  25 
  26 #include "event2/event-config.h"
  27 
  28 
  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         
 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 }