tsThread intercommunications
@@ -479,6 +479,10 @@ | ||
479 | 479 | { |
480 | 480 | TS_STATE_FUNC func; |
481 | 481 | TS_STATE_TYPE ret; |
482 | + if ( state == 0 ) { | |
483 | + invoke_listen(ev); | |
484 | + return 0; | |
485 | + } | |
482 | 486 | func = getFunc(state); |
483 | 487 | return (*func)(this,ev); |
484 | 488 | } |
@@ -33,6 +33,7 @@ | ||
33 | 33 | #define THR_STATE_RUN 1 |
34 | 34 | #define THR_STATE_TERMINATE 4 |
35 | 35 | #define THR_STATE_CALL_FUNC 5 |
36 | +#define THR_STATE_SUSPEND 6 | |
36 | 37 | |
37 | 38 | class TS_THISCLASS : public TS_BASECLASS { |
38 | 39 | public: |
@@ -74,6 +75,9 @@ | ||
74 | 75 | void __tsThread_body_private(); |
75 | 76 | |
76 | 77 | void thr_call(THR_FUNC func,void * msg); |
78 | + void thr_event(); | |
79 | + void thr_throw(int ev_type); | |
80 | + void thr_throw_invoke(); | |
77 | 81 | |
78 | 82 | int thr_state; |
79 | 83 | int thr_attr; |
@@ -83,6 +87,7 @@ | ||
83 | 87 | |
84 | 88 | |
85 | 89 | INTEGER64 ins_time; |
90 | + pthread_cond_t private_cond; | |
86 | 91 | |
87 | 92 | private: |
88 | 93 |
@@ -122,7 +127,6 @@ | ||
122 | 127 | static void create(); |
123 | 128 | static void launch(); |
124 | 129 | static int cond_timed_wait(); |
125 | - void wait_main_thread(); | |
126 | 130 | void inc_max_thread_nos(); |
127 | 131 | static void acc_max_thread_nos(int acc); |
128 | 132 |
@@ -136,13 +140,17 @@ | ||
136 | 140 | pthread_t * current_pthread; |
137 | 141 | uint8_t kill_flag; |
138 | 142 | |
139 | - void base_call_func(tsThread * target); | |
143 | + void base_call_func(tsThread * target,int force_ret); | |
140 | 144 | |
145 | + void _wait_private_cond(); | |
146 | + | |
141 | 147 | tsThread ** this_p; |
142 | 148 | protected: |
143 | 149 | sTimer timer; |
144 | 150 | unsigned timer_flag:1; |
151 | + unsigned private_cond_init:1; | |
145 | 152 | |
153 | + INTEGER64 throw_ev_flags; | |
146 | 154 | }; |
147 | 155 | |
148 | 156 |
@@ -158,7 +166,7 @@ | ||
158 | 166 | } |
159 | 167 | void ins(tsThread * thr) { |
160 | 168 | if ( thr->que_in ) |
161 | - stdObject::panic("DOUBLE INSERTED !!"); | |
169 | + return; | |
162 | 170 | thr->que_in = 1; |
163 | 171 | thr->que_next = 0; |
164 | 172 | if ( head == 0 ) |
@@ -194,7 +202,7 @@ | ||
194 | 202 | class TS_INTERFACE : public TS_BASEINTERFACE { |
195 | 203 | public: |
196 | 204 | unsigned que_in:1; |
197 | - static void thr_call_state(tinyState * THIS,void * msg); | |
205 | + static TS_STATE_TYPE thr_call_state(tinyState * THIS,void * msg); | |
198 | 206 | }; |
199 | 207 | |
200 | 208 | TS_END_INTERFACE |
@@ -371,52 +379,111 @@ | ||
371 | 379 | } |
372 | 380 | |
373 | 381 | |
382 | +void | |
383 | +tsThread_::thr_throw(int ev_type) | |
384 | +{ | |
385 | + if ( ev_type == 0 ) | |
386 | + return; | |
387 | + lock(); | |
388 | + if ( throw_ev_flags & (1LL<<ev_type) ) { | |
389 | + unlock(); | |
390 | + return; | |
391 | + } | |
392 | + throw_ev_flags |= (1LL<<ev_type); | |
393 | + call_main->ins(ifThis); | |
394 | + _push_pipe(); | |
395 | + unlock(); | |
396 | +} | |
374 | 397 | |
375 | 398 | void |
399 | +tsThread_::thr_throw_invoke() | |
400 | +{ | |
401 | +int ev_type; | |
402 | + lock(); | |
403 | + for ( ; throw_ev_flags ; ) | |
404 | + for ( ev_type = 1 ; throw_ev_flags >= (1LL<<ev_type) ; ev_type ++ ) { | |
405 | + if ( !(throw_ev_flags & (1LL<<ev_type)) ) { | |
406 | + continue; | |
407 | + } | |
408 | + throw_ev_flags &= ~(1LL<<ev_type); | |
409 | + unlock(); | |
410 | + parent->do_thread(0,NEW stdEvent(ev_type,ifThis,(INTEGER64)0)); | |
411 | + lock(); | |
412 | + } | |
413 | + unlock(); | |
414 | +} | |
415 | + | |
416 | + | |
417 | +void | |
376 | 418 | tsThread_::thr_call(THR_FUNC func,void*msg) |
377 | 419 | { |
378 | 420 | lock(); |
421 | + if ( private_cond_init == 0 ) { | |
422 | + pthread_cond_init(&private_cond,0); | |
423 | + private_cond_init = 1; | |
424 | + } | |
379 | 425 | thr_call_func = func; |
380 | 426 | thr_call_msg = msg; |
381 | 427 | thr_state = THR_STATE_CALL_FUNC; |
382 | 428 | call_main->ins(ifThis); |
383 | 429 | _push_pipe(); |
430 | + _wait_private_cond(); | |
384 | 431 | unlock(); |
385 | - wait_main_thread(); | |
386 | 432 | } |
387 | 433 | |
388 | 434 | void |
435 | +tsThread_::thr_event() | |
436 | +{ | |
437 | + lock(); | |
438 | + if ( thr_state != THR_STATE_SUSPEND ) { | |
439 | + unlock(); | |
440 | + return; | |
441 | + } | |
442 | + unlock(); | |
443 | + base_call_func(ifThis,1); | |
444 | +} | |
445 | + | |
446 | + | |
447 | +TS_STATE_TYPE | |
389 | 448 | tsThread::thr_call_state(tinyState * THIS,void * msg) |
390 | 449 | { |
391 | 450 | THR_CALL_TYPE * c_msg; |
451 | +TS_STATE_TYPE ret; | |
392 | 452 | c_msg = (THR_CALL_TYPE*)msg; |
393 | - c_msg->ret = THIS->do_thread(c_msg->call, | |
453 | + c_msg->ret = ret = THIS->do_thread(c_msg->call, | |
394 | 454 | NEW stdEvent(TSE_PACKET, |
395 | 455 | THIS, |
396 | 456 | c_msg->msg)); |
457 | + return ret; | |
397 | 458 | } |
398 | 459 | |
399 | 460 | |
400 | 461 | |
401 | 462 | void |
402 | -tsThread_::base_call_func(tsThread * target) | |
463 | +tsThread_::base_call_func(tsThread * target,int force_ret) | |
403 | 464 | { |
404 | - (*target->thr_call_func)(target->parent,target->thr_call_msg); | |
465 | +TS_STATE_TYPE ret; | |
466 | + if ( target->thr_call_func ) | |
467 | + ret = (*target->thr_call_func)(target->parent,target->thr_call_msg); | |
468 | + else ret = force_ret; | |
405 | 469 | lock(); |
406 | - target->thr_state = THR_STATE_RUN; | |
407 | - pthread_cond_signal(&cond); | |
470 | + if ( ret == 0 ) { | |
471 | + target->thr_state = THR_STATE_SUSPEND; | |
472 | + } | |
473 | + else { | |
474 | + target->thr_state = THR_STATE_RUN; | |
475 | + pthread_cond_signal(&target->private_cond); | |
476 | + } | |
408 | 477 | unlock(); |
409 | 478 | } |
410 | 479 | |
411 | 480 | void |
412 | -tsThread_::wait_main_thread() | |
481 | +tsThread_::_wait_private_cond() | |
413 | 482 | { |
414 | 483 | int ret; |
415 | - lock(); | |
416 | 484 | for ( ; thr_state != THR_STATE_RUN ; ) { |
417 | - pthread_cond_wait(&cond,&mu); | |
485 | + pthread_cond_wait(&private_cond,&mu); | |
418 | 486 | } |
419 | - unlock(); | |
420 | 487 | } |
421 | 488 | |
422 | 489 | int |
@@ -816,9 +883,10 @@ | ||
816 | 883 | unlock(); |
817 | 884 | if ( target == 0 ) |
818 | 885 | break; |
886 | + target->thr_throw_invoke(); | |
819 | 887 | switch ( target->thr_state ) { |
820 | 888 | case THR_STATE_CALL_FUNC: |
821 | - base_call_func(target); | |
889 | + base_call_func(target,0); | |
822 | 890 | break; |
823 | 891 | case THR_STATE_TERMINATE: |
824 | 892 | target->destroy(); |
@@ -853,6 +921,8 @@ | ||
853 | 921 | TS_STATE(FIN_FREE_MUTEX) |
854 | 922 | { |
855 | 923 | REF_SET(initial_event,0); |
924 | + if ( private_cond_init ) | |
925 | + pthread_cond_destroy(&private_cond); | |
856 | 926 | |
857 | 927 | if ( host_thread == 0 || host_thread == ifThis) { |
858 | 928 | if ( thread_head ) { |
@@ -7,7 +7,7 @@ | ||
7 | 7 | #include <signal.h> |
8 | 8 | |
9 | 9 | class tinyState; |
10 | -typedef void (*THR_FUNC)(tinyState * THIS,void*msg); | |
10 | +typedef TS_STATE_TYPE (*THR_FUNC)(tinyState * THIS,void*msg); | |
11 | 11 | |
12 | 12 | |
13 | 13 | class stdThread : public stdObject { |
@@ -51,6 +51,7 @@ | ||
51 | 51 | REF_SET(thread,0); \ |
52 | 52 | return (TS_STATE_TYPE)ev->msg_int; \ |
53 | 53 | } \ |
54 | + else thread->thr_event(); \ | |
54 | 55 | return 0; \ |
55 | 56 | } \ |
56 | 57 | TS_STATE_TYPE \ |
@@ -85,9 +86,7 @@ | ||
85 | 86 | c_msg.call = (state); \ |
86 | 87 | c_msg.ret = 0; \ |
87 | 88 | thread->thr_call(tsThread::thr_call_state,&c_msg); \ |
88 | - if ( c_msg.ret ) \ | |
89 | - return c_msg.ret; \ | |
90 | 89 | } |
90 | +#define THR_THROW(ev_type) (thread->thr_throw(ev_type)) | |
91 | 91 | |
92 | - | |
93 | 92 | #endif |