2 * The UCW Library -- Threading Helpers
4 * (c) 2006--2010 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 /*** Library lock ***/
33 static pthread_mutex_t ucwlib_master_mutex;
38 pthread_mutex_lock(&ucwlib_master_mutex);
44 pthread_mutex_unlock(&ucwlib_master_mutex);
47 /*** Thread identifiers ***/
52 static int tid_counter;
55 #ifdef CONFIG_USE_GETTID
59 /* The syscall might be unimplemented */
68 /*** Thread context ***/
70 static void CONSTRUCTOR
71 ucwlib_threads_init_master(void)
73 pthread_mutex_init(&ucwlib_master_mutex, NULL);
78 __thread struct ucwlib_context ucwlib_context;
81 ucwlib_thread_id(struct ucwlib_context *c)
84 c->_thread_id = ucwlib_tid();
90 static pthread_key_t ucwlib_context_key;
93 ucwlib_free_thread_context(void *p)
98 static void CONSTRUCTOR
99 ucwlib_threads_init(void)
101 if (pthread_key_create(&ucwlib_context_key, ucwlib_free_thread_context) < 0)
102 die("Cannot create pthread_key: %m");
105 struct ucwlib_context *
106 ucwlib_thread_context(void)
108 struct ucwlib_context *c = pthread_getspecific(ucwlib_context_key);
111 c = xmalloc_zero(sizeof(*c));
112 c->_thread_id = ucwlib_tid();
113 pthread_setspecific(ucwlib_context_key, c);
118 #endif /* CONFIG_UCW_TLS */
120 #else /* !CONFIG_UCW_THREADS */
122 struct ucwlib_context default_ucwlib_context;
132 msg(L_INFO, "tid=%d", ucwlib_thread_id(ucwlib_thread_context()));