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