1 /* HTHREADS.H (c) Copyright Roger Bowler, 1999-2009 */ 2 /* Hercules Threading Macros and Functions */ 3 4 5 #ifndef _HTHREADS_H 6 #define _HTHREADS_H 7 8 #include "hercules.h" 9 10 #if defined(OPTION_FTHREADS) 11 12 /////////////////////////////////////////////////////////////////////// 13 // FTHREADS 14 /////////////////////////////////////////////////////////////////////// 15 16 #include "fthreads.h" 17 18 typedef fthread_t TID; 19 typedef fthread_mutex_t LOCK; 20 typedef fthread_cond_t COND; 21 typedef fthread_attr_t ATTR; 22 23 #define create_thread(ptid,pat,fn,arg,nm) fthread_create((ptid),(pat),(PFT_THREAD_FUNC)&(fn),(arg),nm) 24 #define join_thread(tid,pcode) fthread_join((tid),(pcode)) 25 #define initialize_lock(plk) fthread_mutex_init((plk),NULL) 26 #define destroy_lock(plk) fthread_mutex_destroy((plk)) 27 #define obtain_lock(plk) fthread_mutex_lock((plk)) 28 #define try_obtain_lock(plk) fthread_mutex_trylock((plk)) 29 #define test_lock(plk) \ 30 (fthread_mutex_trylock((plk)) ? 1 : fthread_mutex_unlock((plk)) ) 31 #define release_lock(plk) fthread_mutex_unlock((plk)) 32 #define initialize_condition(pcond) fthread_cond_init((pcond)) 33 #define destroy_condition(pcond) fthread_cond_destroy((pcond)) 34 #define signal_condition(pcond) fthread_cond_signal((pcond)) 35 #define broadcast_condition(pcond) fthread_cond_broadcast((pcond)) 36 #define wait_condition(pcond,plk) fthread_cond_wait((pcond),(plk)) 37 #define timed_wait_condition(pcond,plk,tm) fthread_cond_timedwait((pcond),(plk),(tm)) 38 #define initialize_detach_attr(pat) fthread_attr_init((pat)); \ 39 fthread_attr_setstacksize((pat),1048576); \ 40 fthread_attr_setdetachstate((pat),FTHREAD_CREATE_DETACHED) 41 #define initialize_join_attr(pat) fthread_attr_init((pat)); \ 42 fthread_attr_setstacksize((pat),1048576); \ 43 fthread_attr_setdetachstate((pat),FTHREAD_CREATE_JOINABLE) 44 #define detach_thread(tid) fthread_detach((tid)) 45 #define signal_thread(tid,signo) fthread_kill((tid),(signo)) 46 #define thread_id() fthread_self() 47 #define exit_thread(exitvar_ptr) fthread_exit((exitvar_ptr)) 48 #define equal_threads(tid1,tid2) fthread_equal((tid1),(tid2)) 49 50 #else // !defined(OPTION_FTHREADS) 51 52 /////////////////////////////////////////////////////////////////////// 53 // PTHREADS 54 /////////////////////////////////////////////////////////////////////// 55 56 #include <pthread.h> 57 58 typedef pthread_t TID; 59 typedef pthread_mutex_t LOCK; 60 typedef pthread_cond_t COND; 61 typedef pthread_attr_t ATTR; 62 #define initialize_lock(plk) \ 63 pthread_mutex_init((plk),NULL) 64 #define destroy_lock(plk) \ 65 pthread_mutex_destroy((plk)) 66 #define obtain_lock(plk) \ 67 pthread_mutex_lock((plk)) 68 #define try_obtain_lock(plk) \ 69 pthread_mutex_trylock((plk)) 70 #define release_lock(plk) \ 71 pthread_mutex_unlock((plk)) 72 #define test_lock(plk) \ 73 (pthread_mutex_trylock((plk)) ? 1 : pthread_mutex_unlock((plk)) ) 74 #define initialize_condition(pcond) \ 75 pthread_cond_init((pcond),NULL) 76 #define destroy_condition(pcond) \ 77 pthread_cond_destroy((pcond)) 78 #define signal_condition(pcond) \ 79 pthread_cond_signal((pcond)) 80 #define broadcast_condition(pcond) \ 81 pthread_cond_broadcast((pcond)) 82 #define wait_condition(pcond,plk) \ 83 pthread_cond_wait((pcond),(plk)) 84 #define timed_wait_condition(pcond,plk,timeout) \ 85 pthread_cond_timedwait((pcond),(plk),(timeout)) 86 #define initialize_detach_attr(pat) \ 87 pthread_attr_init((pat)); \ 88 pthread_attr_setstacksize((pat),1048576); \ 89 pthread_attr_setdetachstate((pat),PTHREAD_CREATE_DETACHED) 90 #define initialize_join_attr(pat) \ 91 pthread_attr_init((pat)); \ 92 pthread_attr_setstacksize((pat),1048576); \ 93 pthread_attr_setdetachstate((pat),PTHREAD_CREATE_JOINABLE) 94 #define join_thread(tid,pcode) \ 95 pthread_join((tid),(pcode)) 96 #define detach_thread(tid) \ 97 pthread_detach((tid)) 98 typedef void*THREAD_FUNC(void*); 99 #define create_thread(ptid,pat,fn,arg,nm) \ 100 pthread_create(ptid,pat,(THREAD_FUNC*)&(fn),arg) 101 #define exit_thread(_code) \ 102 pthread_exit((_code)) 103 #define signal_thread(tid,signo) \ 104 pthread_kill((tid),(signo)) 105 #define thread_id() \ 106 pthread_self() 107 #define equal_threads(tid1,tid2) \ 108 pthread_equal(tid1,tid2) 109 110 #endif // defined(OPTION_FTHREADS) 111 112 /////////////////////////////////////////////////////////////////////// 113 // 'Thread' tracing... 114 /////////////////////////////////////////////////////////////////////// 115 116 #ifdef OPTION_PTTRACE 117 118 #include "pttrace.h" 119 120 #undef initialize_lock 121 #define initialize_lock(plk) \ 122 ptt_pthread_mutex_init((plk),NULL,PTT_LOC) 123 #undef obtain_lock 124 #define obtain_lock(plk) \ 125 ptt_pthread_mutex_lock((plk),PTT_LOC) 126 #undef try_obtain_lock 127 #define try_obtain_lock(plk) \ 128 ptt_pthread_mutex_trylock((plk),PTT_LOC) 129 #undef test_lock 130 #define test_lock(plk) \ 131 (ptt_pthread_mutex_trylock ((plk),PTT_LOC) ? 1 : \ 132 ptt_pthread_mutex_unlock ((plk),PTT_LOC)) 133 #undef release_lock 134 #define release_lock(plk) \ 135 ptt_pthread_mutex_unlock((plk),PTT_LOC) 136 #undef initialize_condition 137 #define initialize_condition(pcond) \ 138 ptt_pthread_cond_init((pcond),NULL,PTT_LOC) 139 #undef signal_condition 140 #define signal_condition(pcond) \ 141 ptt_pthread_cond_signal((pcond),PTT_LOC) 142 #undef broadcast_condition 143 #define broadcast_condition(pcond) \ 144 ptt_pthread_cond_broadcast((pcond),PTT_LOC) 145 #undef wait_condition 146 #define wait_condition(pcond,plk) \ 147 ptt_pthread_cond_wait((pcond),(plk),PTT_LOC) 148 #undef timed_wait_condition 149 #define timed_wait_condition(pcond,plk,timeout) \ 150 ptt_pthread_cond_timedwait((pcond),(plk),(timeout),PTT_LOC) 151 #undef create_thread 152 #if defined(OPTION_FTHREADS) 153 #define create_thread(ptid,pat,fn,arg,nm) \ 154 ptt_pthread_create((ptid),(pat),(PFT_THREAD_FUNC)&(fn),(arg),(nm),PTT_LOC) 155 #else 156 #define create_thread(ptid,pat,fn,arg,nm) \ 157 ptt_pthread_create(ptid,pat,(THREAD_FUNC*)&(fn),arg,(nm),PTT_LOC) 158 #endif 159 #undef join_thread 160 #define join_thread(tid,pcode) \ 161 ptt_pthread_join((tid),(pcode),PTT_LOC) 162 #undef detach_thread 163 #define detach_thread(tid) \ 164 ptt_pthread_detach((tid),PTT_LOC) 165 #undef signal_thread 166 #define signal_thread(tid,signo) \ 167 ptt_pthread_kill((tid),(signo),PTT_LOC) 168 169 #endif // OPTION_PTTRACE 170 171 /////////////////////////////////////////////////////////////////////// 172 // (Misc) 173 /////////////////////////////////////////////////////////////////////// 174 175 /* Pattern for displaying the thread_id */ 176 #define TIDPAT "%8.8lX" 177 178 /*-------------------------------------------------------------------*/ 179 /* Pipe signaling support... */ 180 /*-------------------------------------------------------------------*/ 181 182 #if defined( OPTION_WAKEUP_SELECT_VIA_PIPE ) 183 184 #define RECV_PIPE_SIGNAL( rfd, lock, flag ) \ 185 do { \ 186 int f; int saved_errno=get_HSO_errno(); BYTE c=0; \ 187 obtain_lock(&(lock)); \ 188 if ((f=(flag))>=1) (flag)=0; \ 189 release_lock(&(lock)); \ 190 if (f>=1) \ 191 VERIFY(read_pipe((rfd),&c,1)==1); \ 192 set_HSO_errno(saved_errno); \ 193 } while (0) 194 195 #define SEND_PIPE_SIGNAL( wfd, lock, flag ) \ 196 do { \ 197 int f; int saved_errno=get_HSO_errno(); BYTE c=0; \ 198 obtain_lock(&(lock)); \ 199 if ((f=(flag))<=0) (flag)=1; \ 200 release_lock(&(lock)); \ 201 if (f<=0) \ 202 VERIFY(write_pipe((wfd),&c,1)==1); \ 203 set_HSO_errno(saved_errno); \ 204 } while (0) 205 206 #define SUPPORT_WAKEUP_SELECT_VIA_PIPE( pipe_rfd, maxfd, prset ) \ 207 FD_SET((pipe_rfd),(prset)); \ 208 (maxfd)=(maxfd)>(pipe_rfd)?(maxfd):(pipe_rfd) 209 210 #define SUPPORT_WAKEUP_CONSOLE_SELECT_VIA_PIPE( maxfd, prset ) SUPPORT_WAKEUP_SELECT_VIA_PIPE( sysblk.cnslrpipe, (maxfd), (prset) ) 211 #define SUPPORT_WAKEUP_SOCKDEV_SELECT_VIA_PIPE( maxfd, prset ) SUPPORT_WAKEUP_SELECT_VIA_PIPE( sysblk.sockrpipe, (maxfd), (prset) ) 212 213 #define RECV_CONSOLE_THREAD_PIPE_SIGNAL() RECV_PIPE_SIGNAL( sysblk.cnslrpipe, sysblk.cnslpipe_lock, sysblk.cnslpipe_flag ) 214 #define RECV_SOCKDEV_THREAD_PIPE_SIGNAL() RECV_PIPE_SIGNAL( sysblk.sockrpipe, sysblk.sockpipe_lock, sysblk.sockpipe_flag ) 215 #define SIGNAL_CONSOLE_THREAD() SEND_PIPE_SIGNAL( sysblk.cnslwpipe, sysblk.cnslpipe_lock, sysblk.cnslpipe_flag ) 216 #define SIGNAL_SOCKDEV_THREAD() SEND_PIPE_SIGNAL( sysblk.sockwpipe, sysblk.sockpipe_lock, sysblk.sockpipe_flag ) 217 218 #else // !defined( OPTION_WAKEUP_SELECT_VIA_PIPE ) 219 220 #define RECV_PIPE_SIGNAL( rfd, lock, flag ) 221 #define SEND_PIPE_SIGNAL( wfd, lock, flag ) 222 223 #define SUPPORT_WAKEUP_SELECT_VIA_PIPE( pipe_rfd, maxfd, prset ) 224 225 #define SUPPORT_WAKEUP_CONSOLE_SELECT_VIA_PIPE( maxfd, prset ) 226 #define SUPPORT_WAKEUP_SOCKDEV_SELECT_VIA_PIPE( maxfd, prset ) 227 228 #define RECV_CONSOLE_THREAD_PIPE_SIGNAL() 229 #define RECV_SOCKDEV_THREAD_PIPE_SIGNAL() 230 231 #define SIGNAL_CONSOLE_THREAD() signal_thread( sysblk.cnsltid, SIGUSR2 ) 232 #define SIGNAL_SOCKDEV_THREAD() signal_thread( sysblk.socktid, SIGUSR2 ) 233 234 #endif // defined( OPTION_WAKEUP_SELECT_VIA_PIPE ) 235 236 #endif // _HTHREADS_H 237