1 /* ath.c - A Thread-safeness library.
2 * Copyright (C) 2002, 2003, 2004, 2011 Free Software Foundation, Inc.
4 * This file is part of Libgcrypt.
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #if USE_POSIX_THREADS_WEAK
36 /* On an ELF system it is easy to use pthreads using weak references.
37 Take care not to test the address of a weak referenced function we
38 actually use; some GCC versions have a bug were &foo != NULL is
39 always evaluated to true in PIC mode. USING_PTHREAD_AS_DEFAULT is
40 used by ath_install to detect the default usage of pthread. */
41 #if USE_POSIX_THREADS_WEAK
42 # pragma weak pthread_cancel
43 # pragma weak pthread_mutex_init
44 # pragma weak pthread_mutex_lock
45 # pragma weak pthread_mutex_unlock
46 # pragma weak pthread_mutex_destroy
49 /* For the dummy interface. The MUTEX_NOTINIT value is used to check
50 that a mutex has been initialized. */
51 #define MUTEX_NOTINIT ((ath_mutex_t) 0)
52 #define MUTEX_UNLOCKED ((ath_mutex_t) 1)
53 #define MUTEX_LOCKED ((ath_mutex_t) 2)
54 #define MUTEX_DESTROYED ((ath_mutex_t) 3)
57 /* Return the thread type from the option field. */
58 #define GET_OPTION(a) ((a) & 0xff)
62 enum ath_thread_model {
63 ath_model_undefined = 0,
64 ath_model_none, /* No thread support. */
65 ath_model_pthreads_weak, /* POSIX threads using weak symbols. */
66 ath_model_pthreads, /* POSIX threads directly linked. */
67 ath_model_w32 /* Microsoft Windows threads. */
71 /* The thread model in use. */
72 static enum ath_thread_model thread_model;
75 /* Initialize the ath subsystem. This is called as part of the
76 Libgcrypt initialization. It's purpose is to initialize the
77 locking system. It returns 0 on sucess or an ERRNO value on error.
78 In the latter case it is not defined whether ERRNO was changed.
80 Note: This should be called as early as possible because it is not
81 always possible to detect the thread model to use while already
82 running multi threaded. */
89 return 0; /* Already initialized - no error. */
93 #if USE_POSIX_THREADS_WEAK
94 else if (pthread_cancel)
96 thread_model = ath_model_pthreads_weak;
101 /* Assume a single threaded application. */
102 thread_model = ath_model_none;
109 /* Return the used thread model as string for display purposes an if
110 R_MODEL is not null store its internal number at R_MODEL. */
112 ath_get_model (int *r_model)
115 *r_model = thread_model;
116 switch (thread_model)
118 case ath_model_undefined: return "undefined";
119 case ath_model_none: return "none";
120 case ath_model_pthreads_weak: return "pthread(weak)";
121 case ath_model_pthreads: return "pthread";
122 case ath_model_w32: return "w32";
128 /* This function was used in old Libgcrypt versions (via
129 GCRYCTL_SET_THREAD_CBS) to register the thread callback functions.
130 It is not anymore required. However to allow existing code to
131 continue to work, we keep this function and check that no user
132 defined callbacks are used and that the requested thread system
133 matches the one Libgcrypt is using. */
135 ath_install (struct ath_ops *ath_ops)
137 unsigned int thread_option;
139 /* Check if the requested thread option is compatible to the
140 thread option we are already committed to. */
141 thread_option = ath_ops? GET_OPTION (ath_ops->option) : 0;
143 /* Return an error if the requested thread model does not match the
147 #if USE_POSIX_THREADS_WEAK
148 else if (thread_model == ath_model_pthreads_weak)
150 if (thread_option == ATH_THREAD_OPTION_PTHREAD)
151 return 0; /* Okay - compatible. */
153 #endif /*USE_POSIX_THREADS_WEAK*/
154 else if (thread_option == ATH_THREAD_OPTION_DEFAULT)
155 return 0; /* No thread support requested. */
157 return GPG_ERR_NOT_SUPPORTED;
161 /* Initialize a new mutex. This function returns 0 on success or an
162 system error code (i.e. an ERRNO value). ERRNO may or may not be
165 ath_mutex_init (ath_mutex_t *lock)
169 switch (thread_model)
172 *lock = MUTEX_UNLOCKED;
176 #if USE_POSIX_THREADS_WEAK
177 case ath_model_pthreads_weak:
179 pthread_mutex_t *plck;
181 plck = malloc (sizeof *plck);
183 err = errno? errno : ENOMEM;
186 err = pthread_mutex_init (plck, NULL);
194 #endif /*USE_POSIX_THREADS_WEAK*/
205 /* Destroy a mutex. This function is a NOP if LOCK is NULL. If the
206 mutex is still locked it can't be destroyed and the function
207 returns EBUSY. ERRNO may or may not be changed on error. */
209 ath_mutex_destroy (ath_mutex_t *lock)
216 switch (thread_model)
219 if (*lock != MUTEX_UNLOCKED)
223 *lock = MUTEX_DESTROYED;
228 #if USE_POSIX_THREADS_WEAK
229 case ath_model_pthreads_weak:
231 pthread_mutex_t *plck = (pthread_mutex_t*) (*lock);
233 err = pthread_mutex_destroy (plck);
241 #endif /*USE_POSIX_THREADS_WEAK*/
252 /* Lock the mutex LOCK. On success the function returns 0; on error
253 an error code. ERRNO may or may not be changed on error. */
255 ath_mutex_lock (ath_mutex_t *lock)
259 switch (thread_model)
262 if (*lock == MUTEX_NOTINIT)
264 else if (*lock == MUTEX_UNLOCKED)
266 *lock = MUTEX_LOCKED;
273 #if USE_POSIX_THREADS_WEAK
274 case ath_model_pthreads_weak:
275 err = pthread_mutex_lock ((pthread_mutex_t*)(*lock));
277 #endif /*USE_POSIX_THREADS_WEAK*/
287 /* Unlock the mutex LOCK. On success the function returns 0; on error
288 an error code. ERRNO may or may not be changed on error. */
290 ath_mutex_unlock (ath_mutex_t *lock)
294 switch (thread_model)
297 if (*lock == MUTEX_NOTINIT)
299 else if (*lock == MUTEX_LOCKED)
301 *lock = MUTEX_UNLOCKED;
308 #if USE_POSIX_THREADS_WEAK
309 case ath_model_pthreads_weak:
310 err = pthread_mutex_unlock ((pthread_mutex_t*)(*lock));
312 #endif /*USE_POSIX_THREADS_WEAK*/