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 <w_pthread.h>
00075 #include "stcore_pthread.h"
00076
00077 #ifndef HAVE_SEMAPHORE_H
00078
00079
00080
00081
00082 static int sem_init(sthread_core_t::sem_t *sem, int, int count)
00083 {
00084
00085
00086
00087 sem->count = count;
00088 DO_PTHREAD(pthread_mutex_init(&sem->lock, NULL));
00089 DO_PTHREAD(pthread_cond_init(&sem->wake, NULL));
00090
00091 return 0;
00092 }
00093
00094 static void sem_destroy(sthread_core_t::sem_t *sem)
00095 {
00096 DO_PTHREAD(pthread_mutex_destroy(&sem->lock));
00097 DO_PTHREAD(pthread_cond_destroy(&sem->wake));
00098 }
00099
00100 static inline void sem_post(sthread_core_t::sem_t *sem)
00101 {
00102 DO_PTHREAD(pthread_mutex_lock(&sem->lock));
00103 sem->count++;
00104 if (sem->count > 0)
00105 DO_PTHREAD(pthread_cond_signal(&sem->wake));
00106 DO_PTHREAD(pthread_mutex_unlock(&sem->lock));
00107 }
00108
00109 static inline void sem_wait(sthread_core_t::sem_t *sem)
00110 {
00111 DO_PTHREAD(pthread_mutex_lock(&sem->lock));
00112 while (sem->count <= 0)
00113 DO_PTHREAD(pthread_cond_wait(&sem->wake, &sem->lock));
00114 sem->count--;
00115 DO_PTHREAD(pthread_mutex_unlock(&sem->lock));
00116 }
00117 #endif
00118
00119
00120
00121
00122 extern "C" void *pthread_core_start(void *_arg);
00123 void *pthread_core_start(void *_arg)
00124 {
00125 sthread_core_t *me = (sthread_core_t *) _arg;
00126
00127
00128
00129 me->is_virgin = 0;
00130 (me->start_proc)(me->start_arg);
00131 return 0;
00132 }
00133
00134
00135 int sthread_core_init(sthread_core_t *core,
00136 void (*proc)(void *), void *arg,
00137 unsigned stack_size)
00138 {
00139 int n;
00140
00141
00142 if (stack_size > 0 && stack_size < 1024)
00143 return -1;
00144
00145 core->is_virgin = 1;
00146 core->start_proc = proc;
00147 core->start_arg = arg;
00148 core->stack_size = stack_size;
00149
00150 if (stack_size > 0) {
00151
00152 n = pthread_create(&core->pthread, NULL, pthread_core_start, core);
00153 if (n == -1) {
00154 w_rc_t e= RC(fcOS);
00155
00156
00157
00158
00159
00160 cerr << "pthread_create():" << endl << e << endl;
00161 return -1;
00162 }
00163 core->creator = pthread_self();
00164 }
00165 else {
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 core->is_virgin = 0;
00178 core->pthread = pthread_self();
00179 core->creator = core->pthread;
00180 }
00181 return 0;
00182 }
00183
00184
00185
00186
00187 void sthread_core_exit(sthread_core_t* core, bool &joined)
00188 {
00189 void *join_value=NULL;
00190 if(joined) {
00191 return;
00192 }
00193
00194
00195
00196 if (core->stack_size > 0) {
00197 int res = pthread_join(core->pthread, &join_value);
00198 if(res) {
00199 const char *msg="";
00200 switch(res) {
00201 case EINVAL:
00202 msg = "Not a joinable thread: EINVAL";
00203 break;
00204 case ESRCH:
00205 msg = "No such thread: ESRCH";
00206 break;
00207 case EDEADLK:
00208 msg = "Joining with self: EDEADLK";
00209 break;
00210 default:
00211 break;
00212 }
00213 if(res) {
00214 w_ostrstream o;
00215 o << "sthread_core_exit:"
00216 << " Unexpected result from pthread_join: "
00217 << msg << " core is : ";
00218
00219 o << *core << endl;
00220
00221 W_FATAL_MSG(fcINTERNAL, << o.c_str() << endl);
00222 }
00223 }
00224
00225 }
00226 joined = true;
00227 }
00228
00229 ostream &operator<<(ostream &o, const sthread_core_t &core)
00230 {
00231 o << "core: ";
00232 if (core.stack_size == 0)
00233 W_FORM(o)("[ system thread %#lx creator %#lx ]",
00234 (long) core.pthread,
00235 (long) core.creator
00236 );
00237 else
00238 W_FORM(o)("[ thread %#lx creator %#lx ] size=%d",
00239 (long) core.pthread,
00240 (long) core.creator,
00241 core.stack_size);
00242 if (core.is_virgin)
00243 o << ", virgin-core";
00244 return o;
00245 }