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
17 static pthread_key_t ucwlib_context_key;
18 static pthread_mutex_t ucwlib_master_mutex;
21 ucwlib_free_thread_context(void *p)
26 static void CONSTRUCTOR
27 ucwlib_threads_init(void)
29 if (pthread_key_create(&ucwlib_context_key, ucwlib_free_thread_context) < 0)
30 die("Cannot create pthread_key: %m");
31 pthread_mutex_init(&ucwlib_master_mutex, NULL);
37 static int tid_counter;
40 int tid = ++tid_counter;
45 struct ucwlib_context *
46 ucwlib_thread_context(void)
48 struct ucwlib_context *c = pthread_getspecific(ucwlib_context_key);
51 c = xmalloc_zero(sizeof(*c));
52 c->thread_id = ucwlib_tid();
53 pthread_setspecific(ucwlib_context_key, c);
61 pthread_mutex_lock(&ucwlib_master_mutex);
67 pthread_mutex_unlock(&ucwlib_master_mutex);
72 struct ucwlib_context *
73 ucwlib_thread_context(void)
75 static struct ucwlib_context ucwlib_context;
76 return &ucwlib_context;
97 log(L_INFO, "tid=%d", ucwlib_thread_context()->thread_id);