xref: /libevent-2.1.12/evthread_pthread.c (revision 946b5841)
1b85b710cSNick Mathewson /*
2e49e2891SNick Mathewson  * Copyright 2009-2012 Niels Provos and Nick Mathewson
3b85b710cSNick Mathewson  *
4b85b710cSNick Mathewson  * Redistribution and use in source and binary forms, with or without
5b85b710cSNick Mathewson  * modification, are permitted provided that the following conditions
6b85b710cSNick Mathewson  * are met:
7b85b710cSNick Mathewson  * 1. Redistributions of source code must retain the above copyright
8b85b710cSNick Mathewson  *    notice, this list of conditions and the following disclaimer.
9b85b710cSNick Mathewson  * 2. Redistributions in binary form must reproduce the above copyright
10b85b710cSNick Mathewson  *    notice, this list of conditions and the following disclaimer in the
11b85b710cSNick Mathewson  *    documentation and/or other materials provided with the distribution.
12b85b710cSNick Mathewson  * 3. The name of the author may not be used to endorse or promote products
13b85b710cSNick Mathewson  *    derived from this software without specific prior written permission.
14b85b710cSNick Mathewson  *
15b85b710cSNick Mathewson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16b85b710cSNick Mathewson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17b85b710cSNick Mathewson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18b85b710cSNick Mathewson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19b85b710cSNick Mathewson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20b85b710cSNick Mathewson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21b85b710cSNick Mathewson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22b85b710cSNick Mathewson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23b85b710cSNick Mathewson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24b85b710cSNick Mathewson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25b85b710cSNick Mathewson  */
26ec347b92SNick Mathewson #include "event2/event-config.h"
279b27b307SKevin Bowling #include "evconfig-private.h"
280915ca0aSKevin Bowling 
290915ca0aSKevin Bowling /* With glibc we need to define _GNU_SOURCE to get PTHREAD_MUTEX_RECURSIVE.
300915ca0aSKevin Bowling  * This comes from evconfig-private.h
310915ca0aSKevin Bowling  */
32309fc7c4SNick Mathewson #include <pthread.h>
33ec35eb55SNick Mathewson 
34309fc7c4SNick Mathewson struct event_base;
35fbe64f21SEvan Jones #include "event2/thread.h"
36309fc7c4SNick Mathewson 
379bf124bfSNick Mathewson #include <stdlib.h>
38fbaf0770SNick Mathewson #include <string.h>
39309fc7c4SNick Mathewson #include "mm-internal.h"
40d4977b52SNick Mathewson #include "evthread-internal.h"
41309fc7c4SNick Mathewson 
42ec35eb55SNick Mathewson static pthread_mutexattr_t attr_recursive;
43ec35eb55SNick Mathewson 
44309fc7c4SNick Mathewson static void *
evthread_posix_lock_alloc(unsigned locktype)45347952ffSNick Mathewson evthread_posix_lock_alloc(unsigned locktype)
46309fc7c4SNick Mathewson {
47347952ffSNick Mathewson 	pthread_mutexattr_t *attr = NULL;
48309fc7c4SNick Mathewson 	pthread_mutex_t *lock = mm_malloc(sizeof(pthread_mutex_t));
49309fc7c4SNick Mathewson 	if (!lock)
50309fc7c4SNick Mathewson 		return NULL;
51347952ffSNick Mathewson 	if (locktype & EVTHREAD_LOCKTYPE_RECURSIVE)
52347952ffSNick Mathewson 		attr = &attr_recursive;
53347952ffSNick Mathewson 	if (pthread_mutex_init(lock, attr)) {
54ec35eb55SNick Mathewson 		mm_free(lock);
55ec35eb55SNick Mathewson 		return NULL;
56ec35eb55SNick Mathewson 	}
57309fc7c4SNick Mathewson 	return lock;
58309fc7c4SNick Mathewson }
59309fc7c4SNick Mathewson 
60309fc7c4SNick Mathewson static void
evthread_posix_lock_free(void * lock_,unsigned locktype)61*946b5841SNick Mathewson evthread_posix_lock_free(void *lock_, unsigned locktype)
62309fc7c4SNick Mathewson {
63*946b5841SNick Mathewson 	pthread_mutex_t *lock = lock_;
64309fc7c4SNick Mathewson 	pthread_mutex_destroy(lock);
65309fc7c4SNick Mathewson 	mm_free(lock);
66309fc7c4SNick Mathewson }
67309fc7c4SNick Mathewson 
68347952ffSNick Mathewson static int
evthread_posix_lock(unsigned mode,void * lock_)69*946b5841SNick Mathewson evthread_posix_lock(unsigned mode, void *lock_)
70309fc7c4SNick Mathewson {
71*946b5841SNick Mathewson 	pthread_mutex_t *lock = lock_;
72347952ffSNick Mathewson 	if (mode & EVTHREAD_TRY)
73347952ffSNick Mathewson 		return pthread_mutex_trylock(lock);
74309fc7c4SNick Mathewson 	else
75347952ffSNick Mathewson 		return pthread_mutex_lock(lock);
76347952ffSNick Mathewson }
77347952ffSNick Mathewson 
78347952ffSNick Mathewson static int
evthread_posix_unlock(unsigned mode,void * lock_)79*946b5841SNick Mathewson evthread_posix_unlock(unsigned mode, void *lock_)
80347952ffSNick Mathewson {
81*946b5841SNick Mathewson 	pthread_mutex_t *lock = lock_;
82347952ffSNick Mathewson 	return pthread_mutex_unlock(lock);
83309fc7c4SNick Mathewson }
84309fc7c4SNick Mathewson 
85309fc7c4SNick Mathewson static unsigned long
evthread_posix_get_id(void)86309fc7c4SNick Mathewson evthread_posix_get_id(void)
87309fc7c4SNick Mathewson {
88309fc7c4SNick Mathewson 	union {
89309fc7c4SNick Mathewson 		pthread_t thr;
9068120d9bSNick Mathewson #if EVENT__SIZEOF_PTHREAD_T > EVENT__SIZEOF_LONG
91fbaf0770SNick Mathewson 		ev_uint64_t id;
92fbaf0770SNick Mathewson #else
93309fc7c4SNick Mathewson 		unsigned long id;
94fbaf0770SNick Mathewson #endif
95309fc7c4SNick Mathewson 	} r;
9668120d9bSNick Mathewson #if EVENT__SIZEOF_PTHREAD_T < EVENT__SIZEOF_LONG
97fbaf0770SNick Mathewson 	memset(&r, 0, sizeof(r));
98fbaf0770SNick Mathewson #endif
99309fc7c4SNick Mathewson 	r.thr = pthread_self();
100fbaf0770SNick Mathewson 	return (unsigned long)r.id;
101309fc7c4SNick Mathewson }
102309fc7c4SNick Mathewson 
103d4977b52SNick Mathewson static void *
evthread_posix_cond_alloc(unsigned condflags)104d4977b52SNick Mathewson evthread_posix_cond_alloc(unsigned condflags)
105d4977b52SNick Mathewson {
106d4977b52SNick Mathewson 	pthread_cond_t *cond = mm_malloc(sizeof(pthread_cond_t));
107d4977b52SNick Mathewson 	if (!cond)
108d4977b52SNick Mathewson 		return NULL;
109d4977b52SNick Mathewson 	if (pthread_cond_init(cond, NULL)) {
110d4977b52SNick Mathewson 		mm_free(cond);
111d4977b52SNick Mathewson 		return NULL;
112d4977b52SNick Mathewson 	}
113d4977b52SNick Mathewson 	return cond;
114d4977b52SNick Mathewson }
115d4977b52SNick Mathewson 
116d4977b52SNick Mathewson static void
evthread_posix_cond_free(void * cond_)117*946b5841SNick Mathewson evthread_posix_cond_free(void *cond_)
118d4977b52SNick Mathewson {
119*946b5841SNick Mathewson 	pthread_cond_t *cond = cond_;
120d4977b52SNick Mathewson 	pthread_cond_destroy(cond);
121d4977b52SNick Mathewson 	mm_free(cond);
122d4977b52SNick Mathewson }
123d4977b52SNick Mathewson 
124d4977b52SNick Mathewson static int
evthread_posix_cond_signal(void * cond_,int broadcast)125*946b5841SNick Mathewson evthread_posix_cond_signal(void *cond_, int broadcast)
126d4977b52SNick Mathewson {
127*946b5841SNick Mathewson 	pthread_cond_t *cond = cond_;
128d4977b52SNick Mathewson 	int r;
129d4977b52SNick Mathewson 	if (broadcast)
130d4977b52SNick Mathewson 		r = pthread_cond_broadcast(cond);
131d4977b52SNick Mathewson 	else
132d4977b52SNick Mathewson 		r = pthread_cond_signal(cond);
133d4977b52SNick Mathewson 	return r ? -1 : 0;
134d4977b52SNick Mathewson }
135d4977b52SNick Mathewson 
136d4977b52SNick Mathewson static int
evthread_posix_cond_wait(void * cond_,void * lock_,const struct timeval * tv)137*946b5841SNick Mathewson evthread_posix_cond_wait(void *cond_, void *lock_, const struct timeval *tv)
138d4977b52SNick Mathewson {
139d4977b52SNick Mathewson 	int r;
140*946b5841SNick Mathewson 	pthread_cond_t *cond = cond_;
141*946b5841SNick Mathewson 	pthread_mutex_t *lock = lock_;
142d4977b52SNick Mathewson 
143d4977b52SNick Mathewson 	if (tv) {
144d4977b52SNick Mathewson 		struct timeval now, abstime;
145d4977b52SNick Mathewson 		struct timespec ts;
146d4977b52SNick Mathewson 		evutil_gettimeofday(&now, NULL);
147d4977b52SNick Mathewson 		evutil_timeradd(&now, tv, &abstime);
148d4977b52SNick Mathewson 		ts.tv_sec = abstime.tv_sec;
149d4977b52SNick Mathewson 		ts.tv_nsec = abstime.tv_usec*1000;
150d4977b52SNick Mathewson 		r = pthread_cond_timedwait(cond, lock, &ts);
151d4977b52SNick Mathewson 		if (r == ETIMEDOUT)
152d4977b52SNick Mathewson 			return 1;
153d4977b52SNick Mathewson 		else if (r)
154d4977b52SNick Mathewson 			return -1;
155d4977b52SNick Mathewson 		else
156d4977b52SNick Mathewson 			return 0;
157d4977b52SNick Mathewson 	} else {
158d4977b52SNick Mathewson 		r = pthread_cond_wait(cond, lock);
159d4977b52SNick Mathewson 		return r ? -1 : 0;
160d4977b52SNick Mathewson 	}
161d4977b52SNick Mathewson }
162d4977b52SNick Mathewson 
163309fc7c4SNick Mathewson int
evthread_use_pthreads(void)164ec35eb55SNick Mathewson evthread_use_pthreads(void)
165309fc7c4SNick Mathewson {
166347952ffSNick Mathewson 	struct evthread_lock_callbacks cbs = {
167347952ffSNick Mathewson 		EVTHREAD_LOCK_API_VERSION,
168347952ffSNick Mathewson 		EVTHREAD_LOCKTYPE_RECURSIVE,
169347952ffSNick Mathewson 		evthread_posix_lock_alloc,
170347952ffSNick Mathewson 		evthread_posix_lock_free,
171347952ffSNick Mathewson 		evthread_posix_lock,
172347952ffSNick Mathewson 		evthread_posix_unlock
173347952ffSNick Mathewson 	};
174d4977b52SNick Mathewson 	struct evthread_condition_callbacks cond_cbs = {
175d4977b52SNick Mathewson 		EVTHREAD_CONDITION_API_VERSION,
176d4977b52SNick Mathewson 		evthread_posix_cond_alloc,
177d4977b52SNick Mathewson 		evthread_posix_cond_free,
178d4977b52SNick Mathewson 		evthread_posix_cond_signal,
179d4977b52SNick Mathewson 		evthread_posix_cond_wait
180d4977b52SNick Mathewson 	};
181ec35eb55SNick Mathewson 	/* Set ourselves up to get recursive locks. */
1824ba6eda4SNick Mathewson 	if (pthread_mutexattr_init(&attr_recursive))
1834ba6eda4SNick Mathewson 		return -1;
1844ba6eda4SNick Mathewson 	if (pthread_mutexattr_settype(&attr_recursive, PTHREAD_MUTEX_RECURSIVE))
1854ba6eda4SNick Mathewson 		return -1;
186ec35eb55SNick Mathewson 
187347952ffSNick Mathewson 	evthread_set_lock_callbacks(&cbs);
188d4977b52SNick Mathewson 	evthread_set_condition_callbacks(&cond_cbs);
189ec35eb55SNick Mathewson 	evthread_set_id_callback(evthread_posix_get_id);
1904ba6eda4SNick Mathewson 	return 0;
191309fc7c4SNick Mathewson }
192