2 * The UCW Library -- Threading Helpers
4 * (c) 2006 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include "lib/threads.h"
13 #ifdef CONFIG_UCW_THREADS
18 #include <sys/types.h>
19 #include <linux/unistd.h>
21 static _syscall0(pid_t, gettid)
22 #define CONFIG_USE_GETTID
26 static pthread_key_t ucwlib_context_key;
27 static pthread_mutex_t ucwlib_master_mutex;
30 ucwlib_free_thread_context(void *p)
35 static void CONSTRUCTOR
36 ucwlib_threads_init(void)
38 if (pthread_key_create(&ucwlib_context_key, ucwlib_free_thread_context) < 0)
39 die("Cannot create pthread_key: %m");
40 pthread_mutex_init(&ucwlib_master_mutex, NULL);
46 static int tid_counter;
49 #ifdef CONFIG_USE_GETTID
53 /* The syscall might be unimplemented */
62 struct ucwlib_context *
63 ucwlib_thread_context(void)
65 struct ucwlib_context *c = pthread_getspecific(ucwlib_context_key);
68 c = xmalloc_zero(sizeof(*c));
69 c->thread_id = ucwlib_tid();
70 pthread_setspecific(ucwlib_context_key, c);
78 pthread_mutex_lock(&ucwlib_master_mutex);
84 pthread_mutex_unlock(&ucwlib_master_mutex);
89 struct ucwlib_context *
90 ucw_thread_context(void)
92 static struct ucwlib_context ucwlib_context;
93 return &ucwlib_context;
114 log(L_INFO, "tid=%d", ucwlib_thread_context()->thread_id);