1 2 /* 3 * Copyright (C) Igor Sysoev 4 * Copyright (C) NGINX, Inc. 5 */ 6 7 #ifndef _NXT_UNIX_THREAD_H_INCLUDED_ 8 #define _NXT_UNIX_THREAD_H_INCLUDED_ 9 10 11 /* 12 * Thread Specific Data 13 * 14 * The interface unifies two TSD implementations: the __thread storage 15 * class and pthread specific data. It works also in non-threaded mode. 16 * The interface is optimized for the __thread storage class and non-threaded 17 * mode, since the __thread storage is faster and is supported in modern 18 * versions of Linux, FreeBSD, Solaris, and MacOSX. Pthread specific data 19 * is considered as a fallback option. 20 * 21 * The underlining interfaces are different: pthread data must be allocated 22 * by hand and may be accessed only by using pointers whereas __thread data 23 * allocation is transparent and it is accessed directly. 24 * 25 * pthread_getspecific() is usually faster than pthread_setspecific() 26 * (much faster on MacOSX), so there is no nxt_thread_set_data() interface 27 * for this reason. It is better to store frequently alterable thread 28 * log pointer in nxt_thread_t, but not in a dedicated key. 29 */ 30 31 #if (NXT_HAVE_THREAD_STORAGE_CLASS) 32 33 #define nxt_thread_extern_data(type, tsd) \ 34 NXT_EXPORT extern __thread type tsd 35 36 #define nxt_thread_declare_data(type, tsd) \ 37 __thread type tsd 38 39 #define nxt_thread_init_data(tsd) 40 41 #define nxt_thread_get_data(tsd) \ 42 &tsd 43 44 45 #else /* NXT_HAVE_PTHREAD_SPECIFIC_DATA */ 46 47 /* 48 * nxt_thread_get_data() is used as 49 * p = nxt_thread_get_data(tsd), 50 * but the tsd address is actually required. This could be resolved by macro 51 * #define nxt_thread_get_data(tsd) nxt_thread_get_data_addr(&tsd) 52 * or by definition nxt_thread_specific_data_t as an array. 53 * 54 * On Linux and Solaris pthread_key_t is unsigned integer. 55 * On FreeBSD, NetBSD, OpenBSD, and HP-UX pthread_key_t is integer. 56 * On MacOSX and AIX pthread_key_t is unsigned long integer. 57 * On Cygwin pthread_key_t is pointer to void. 58 */ 59 60 typedef struct { 61 nxt_atomic_t key; 62 size_t size; 63 } nxt_thread_specific_data_t[1]; 64 65 66 #define nxt_thread_extern_data(type, tsd) \ 67 NXT_EXPORT extern nxt_thread_specific_data_t tsd 68 69 #define nxt_thread_declare_data(type, tsd) \ 70 nxt_thread_specific_data_t tsd = { { (nxt_atomic_int_t) -1, sizeof(type) } } 71 72 NXT_EXPORT void nxt_thread_init_data(nxt_thread_specific_data_t tsd); 73 74 #define nxt_thread_get_data(tsd) \ 75 pthread_getspecific((pthread_key_t) tsd->key) 76 77 #endif 78 79 80 typedef void (*nxt_thread_start_t)(void *data); 81 82 typedef struct { 83 nxt_thread_start_t start; 84 nxt_event_engine_t *engine; 85 nxt_work_t work; 86 } nxt_thread_link_t; 87 88 89 NXT_EXPORT nxt_int_t nxt_thread_create(nxt_thread_handle_t *handle, 90 nxt_thread_link_t *link); 91 NXT_EXPORT nxt_thread_t *nxt_thread_init(void); 92 NXT_EXPORT void nxt_thread_exit(nxt_thread_t *thr); 93 NXT_EXPORT void nxt_thread_cancel(nxt_thread_handle_t handle); 94 NXT_EXPORT void nxt_thread_wait(nxt_thread_handle_t handle); 95 96 97 #define nxt_thread_handle() \ 98 pthread_self() 99 100 101 typedef pthread_mutex_t nxt_thread_mutex_t; 102 103 NXT_EXPORT nxt_int_t nxt_thread_mutex_create(nxt_thread_mutex_t *mtx); 104 NXT_EXPORT void nxt_thread_mutex_destroy(nxt_thread_mutex_t *mtx); 105 NXT_EXPORT nxt_int_t nxt_thread_mutex_lock(nxt_thread_mutex_t *mtx); 106 NXT_EXPORT nxt_bool_t nxt_thread_mutex_trylock(nxt_thread_mutex_t *mtx); 107 NXT_EXPORT nxt_int_t nxt_thread_mutex_unlock(nxt_thread_mutex_t *mtx); 108 109 110 typedef pthread_cond_t nxt_thread_cond_t; 111 112 NXT_EXPORT nxt_int_t nxt_thread_cond_create(nxt_thread_cond_t *cond); 113 NXT_EXPORT void nxt_thread_cond_destroy(nxt_thread_cond_t *cond); 114 NXT_EXPORT nxt_int_t nxt_thread_cond_signal(nxt_thread_cond_t *cond); 115 NXT_EXPORT nxt_err_t nxt_thread_cond_wait(nxt_thread_cond_t *cond, 116 nxt_thread_mutex_t *mtx, nxt_nsec_t timeout); 117 118 119 #if (NXT_HAVE_PTHREAD_YIELD) 120 #define nxt_thread_yield() \ 121 pthread_yield() 122 123 #elif (NXT_HAVE_PTHREAD_YIELD_NP) 124 #define nxt_thread_yield() \ 125 pthread_yield_np() 126 127 #else 128 #define nxt_thread_yield() \ 129 nxt_sched_yield() 130 131 #endif 132 133 134 struct nxt_thread_s { 135 nxt_log_t *log; 136 nxt_log_t main_log; 137 138 nxt_task_t *task; 139 140 nxt_tid_t tid; 141 nxt_thread_handle_t handle; 142 nxt_thread_link_t *link; 143 nxt_thread_pool_t *thread_pool; 144 145 nxt_thread_time_t time; 146 147 nxt_runtime_t *runtime; 148 nxt_event_engine_t *engine; 149 void *data; 150 151 #if 0 152 /* 153 * Although pointer to a current fiber should be a property of 154 * engine->fibers, its placement here eliminates 2 memory accesses. 155 */ 156 nxt_fiber_t *fiber; 157 #endif 158 159 nxt_random_t random; 160 }; 161 162 163 #endif /* _NXT_UNIX_THREAD_H_INCLUDED_ */ 164