00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 #include "w_defines.h"
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 #include <w.h>
00072 #include "sthread.h"
00073 #include <w_stream.h>
00074 #include <pthread.h>
00075 #include "stcore_pthread.h"
00076
00077
00078
00079 #define USER_STACK_SIZE (sthread_t::default_stack)
00080 #define DEFAULT_STACK_SIZE (USER_STACK_SIZE)
00081
00082
00083 #ifndef HAVE_SEMAPHORE_H
00084
00085
00086
00087
00088 static int sem_init(sthread_core_t::sem_t *sem, int, int count)
00089 {
00090
00091
00092
00093 sem->count = count;
00094 DO_PTHREAD(pthread_mutex_init(&sem->lock, NULL));
00095 DO_PTHREAD(pthread_cond_init(&sem->wake, NULL));
00096
00097 return 0;
00098 }
00099
00100 static void sem_destroy(sthread_core_t::sem_t *sem)
00101 {
00102 DO_PTHREAD(pthread_mutex_destroy(&sem->lock));
00103 DO_PTHREAD(pthread_cond_destroy(&sem->wake));
00104 }
00105
00106 static inline void sem_post(sthread_core_t::sem_t *sem)
00107 {
00108 DO_PTHREAD(pthread_mutex_lock(&sem->lock));
00109 sem->count++;
00110 if (sem->count > 0)
00111 DO_PTHREAD(pthread_cond_signal(&sem->wake));
00112 DO_PTHREAD(pthread_mutex_unlock(&sem->lock));
00113 }
00114
00115 static inline void sem_wait(sthread_core_t::sem_t *sem)
00116 {
00117 DO_PTHREAD(pthread_mutex_lock(&sem->lock));
00118 while (sem->count <= 0)
00119 DO_PTHREAD(pthread_cond_wait(&sem->wake, &sem->lock));
00120 sem->count--;
00121 DO_PTHREAD(pthread_mutex_unlock(&sem->lock));
00122 }
00123 #endif
00124
00125
00126
00127
00128 extern "C" void *pthread_core_start(void *_arg);
00129 void *pthread_core_start(void *_arg)
00130 {
00131 sthread_core_t *me = (sthread_core_t *) _arg;
00132
00133
00134
00135 me->is_virgin = 0;
00136 (me->start_proc)(me->start_arg);
00137 return 0;
00138 }
00139
00140
00141 int sthread_core_init(sthread_core_t *core,
00142 void (*proc)(void *), void *arg,
00143 unsigned stack_size)
00144 {
00145 int n;
00146
00147
00148 if (stack_size > 0 && stack_size < 1024)
00149 return -1;
00150
00151 core->is_virgin = 1;
00152 core->start_proc = proc;
00153 core->start_arg = arg;
00154 core->stack_size = stack_size;
00155
00156 if (stack_size > 0) {
00157
00158 n = pthread_create(&core->pthread, NULL, pthread_core_start, core);
00159 if (n == -1) {
00160 w_rc_t e= RC(fcOS);
00161
00162
00163
00164
00165
00166 cerr << "pthread_create():" << endl << e << endl;
00167 return -1;
00168 }
00169 core->creator = pthread_self();
00170 }
00171 else {
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 core->is_virgin = 0;
00184 core->pthread = pthread_self();
00185 core->creator = core->pthread;
00186 }
00187 return 0;
00188 }
00189
00190
00191
00192
00193 void sthread_core_exit(sthread_core_t* core, bool &joined)
00194 {
00195 void *join_value=NULL;
00196 if(joined) {
00197 return;
00198 }
00199
00200
00201
00202 if (core->stack_size > 0) {
00203 int res = pthread_join(core->pthread, &join_value);
00204 if(res) {
00205 const char *msg="";
00206 switch(res) {
00207 case EINVAL:
00208 msg = "Not a joinable thread: EINVAL";
00209 break;
00210 case ESRCH:
00211 msg = "No such thread: ESRCH";
00212 break;
00213 case EDEADLK:
00214 msg = "Joining with self: EDEADLK";
00215 break;
00216 default:
00217 break;
00218 }
00219 if(res) {
00220 w_ostrstream o;
00221 o << "sthread_core_exit:"
00222 << " Unexpected result from pthread_join: "
00223 << msg << " core is : ";
00224
00225 o << *core << endl;
00226
00227 W_FATAL_MSG(fcINTERNAL, << o.c_str() << endl);
00228 }
00229 }
00230
00231 }
00232 joined = true;
00233 }
00234
00235 ostream &operator<<(ostream &o, const sthread_core_t &core)
00236 {
00237 o << "core: ";
00238 if (core.stack_size == 0)
00239 W_FORM(o)("[ system thread %#lx creator %#lx ]",
00240 (long) core.pthread,
00241 (long) core.creator
00242 );
00243 else
00244 W_FORM(o)("[ thread %#lx creator %#lx ] size=%d",
00245 (long) core.pthread,
00246 (long) core.creator,
00247 core.stack_size);
00248 if (core.is_virgin)
00249 o << ", virgin-core";
00250 return o;
00251 }