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 "ucw/threads.h"
13 #ifdef CONFIG_UCW_THREADS
18 #include <sys/types.h>
19 #include <sys/syscall.h>
25 return syscall(__NR_gettid);
27 #define CONFIG_USE_GETTID
31 static pthread_key_t ucwlib_context_key;
32 static pthread_mutex_t ucwlib_master_mutex;
35 ucwlib_free_thread_context(void *p)
40 static void CONSTRUCTOR
41 ucwlib_threads_init(void)
43 if (pthread_key_create(&ucwlib_context_key, ucwlib_free_thread_context) < 0)
44 die("Cannot create pthread_key: %m");
45 pthread_mutex_init(&ucwlib_master_mutex, NULL);
51 static int tid_counter;
54 #ifdef CONFIG_USE_GETTID
58 /* The syscall might be unimplemented */
67 struct ucwlib_context *
68 ucwlib_thread_context(void)
70 struct ucwlib_context *c = pthread_getspecific(ucwlib_context_key);
73 c = xmalloc_zero(sizeof(*c));
74 c->thread_id = ucwlib_tid();
75 pthread_setspecific(ucwlib_context_key, c);
83 pthread_mutex_lock(&ucwlib_master_mutex);
89 pthread_mutex_unlock(&ucwlib_master_mutex);
94 struct ucwlib_context *
95 ucwlib_thread_context(void)
97 static struct ucwlib_context ucwlib_context;
98 return &ucwlib_context;
119 msg(L_INFO, "tid=%d", ucwlib_thread_context()->thread_id);