• R/O
  • SSH
  • HTTPS

globalbase: Commit


Commit MetaInfo

Revision5930 (tree)
Time2018-10-07 13:42:27
Authorjoshua

Log Message

tsThread intercommunications

Change Summary

Incremental Difference

--- modules/tinyState/trunk/src/classes/ts/c++/tinyState.cpp (revision 5929)
+++ modules/tinyState/trunk/src/classes/ts/c++/tinyState.cpp (revision 5930)
@@ -479,6 +479,10 @@
479479 {
480480 TS_STATE_FUNC func;
481481 TS_STATE_TYPE ret;
482+ if ( state == 0 ) {
483+ invoke_listen(ev);
484+ return 0;
485+ }
482486 func = getFunc(state);
483487 return (*func)(this,ev);
484488 }
--- modules/tinyState/trunk/src/classes/ts/c++/tsThread.cpp (revision 5929)
+++ modules/tinyState/trunk/src/classes/ts/c++/tsThread.cpp (revision 5930)
@@ -33,6 +33,7 @@
3333 #define THR_STATE_RUN 1
3434 #define THR_STATE_TERMINATE 4
3535 #define THR_STATE_CALL_FUNC 5
36+#define THR_STATE_SUSPEND 6
3637
3738 class TS_THISCLASS : public TS_BASECLASS {
3839 public:
@@ -74,6 +75,9 @@
7475 void __tsThread_body_private();
7576
7677 void thr_call(THR_FUNC func,void * msg);
78+ void thr_event();
79+ void thr_throw(int ev_type);
80+ void thr_throw_invoke();
7781
7882 int thr_state;
7983 int thr_attr;
@@ -83,6 +87,7 @@
8387
8488
8589 INTEGER64 ins_time;
90+ pthread_cond_t private_cond;
8691
8792 private:
8893
@@ -122,7 +127,6 @@
122127 static void create();
123128 static void launch();
124129 static int cond_timed_wait();
125- void wait_main_thread();
126130 void inc_max_thread_nos();
127131 static void acc_max_thread_nos(int acc);
128132
@@ -136,13 +140,17 @@
136140 pthread_t * current_pthread;
137141 uint8_t kill_flag;
138142
139- void base_call_func(tsThread * target);
143+ void base_call_func(tsThread * target,int force_ret);
140144
145+ void _wait_private_cond();
146+
141147 tsThread ** this_p;
142148 protected:
143149 sTimer timer;
144150 unsigned timer_flag:1;
151+ unsigned private_cond_init:1;
145152
153+ INTEGER64 throw_ev_flags;
146154 };
147155
148156
@@ -158,7 +166,7 @@
158166 }
159167 void ins(tsThread * thr) {
160168 if ( thr->que_in )
161- stdObject::panic("DOUBLE INSERTED !!");
169+ return;
162170 thr->que_in = 1;
163171 thr->que_next = 0;
164172 if ( head == 0 )
@@ -194,7 +202,7 @@
194202 class TS_INTERFACE : public TS_BASEINTERFACE {
195203 public:
196204 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);
198206 };
199207
200208 TS_END_INTERFACE
@@ -371,52 +379,111 @@
371379 }
372380
373381
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+}
374397
375398 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
376418 tsThread_::thr_call(THR_FUNC func,void*msg)
377419 {
378420 lock();
421+ if ( private_cond_init == 0 ) {
422+ pthread_cond_init(&private_cond,0);
423+ private_cond_init = 1;
424+ }
379425 thr_call_func = func;
380426 thr_call_msg = msg;
381427 thr_state = THR_STATE_CALL_FUNC;
382428 call_main->ins(ifThis);
383429 _push_pipe();
430+ _wait_private_cond();
384431 unlock();
385- wait_main_thread();
386432 }
387433
388434 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
389448 tsThread::thr_call_state(tinyState * THIS,void * msg)
390449 {
391450 THR_CALL_TYPE * c_msg;
451+TS_STATE_TYPE ret;
392452 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,
394454 NEW stdEvent(TSE_PACKET,
395455 THIS,
396456 c_msg->msg));
457+ return ret;
397458 }
398459
399460
400461
401462 void
402-tsThread_::base_call_func(tsThread * target)
463+tsThread_::base_call_func(tsThread * target,int force_ret)
403464 {
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;
405469 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+ }
408477 unlock();
409478 }
410479
411480 void
412-tsThread_::wait_main_thread()
481+tsThread_::_wait_private_cond()
413482 {
414483 int ret;
415- lock();
416484 for ( ; thr_state != THR_STATE_RUN ; ) {
417- pthread_cond_wait(&cond,&mu);
485+ pthread_cond_wait(&private_cond,&mu);
418486 }
419- unlock();
420487 }
421488
422489 int
@@ -816,9 +883,10 @@
816883 unlock();
817884 if ( target == 0 )
818885 break;
886+ target->thr_throw_invoke();
819887 switch ( target->thr_state ) {
820888 case THR_STATE_CALL_FUNC:
821- base_call_func(target);
889+ base_call_func(target,0);
822890 break;
823891 case THR_STATE_TERMINATE:
824892 target->destroy();
@@ -853,6 +921,8 @@
853921 TS_STATE(FIN_FREE_MUTEX)
854922 {
855923 REF_SET(initial_event,0);
924+ if ( private_cond_init )
925+ pthread_cond_destroy(&private_cond);
856926
857927 if ( host_thread == 0 || host_thread == ifThis) {
858928 if ( thread_head ) {
--- modules/tinyState/trunk/src/h/ts/c++/tsThread.h (revision 5929)
+++ modules/tinyState/trunk/src/h/ts/c++/tsThread.h (revision 5930)
@@ -7,7 +7,7 @@
77 #include <signal.h>
88
99 class tinyState;
10-typedef void (*THR_FUNC)(tinyState * THIS,void*msg);
10+typedef TS_STATE_TYPE (*THR_FUNC)(tinyState * THIS,void*msg);
1111
1212
1313 class stdThread : public stdObject {
@@ -51,6 +51,7 @@
5151 REF_SET(thread,0); \
5252 return (TS_STATE_TYPE)ev->msg_int; \
5353 } \
54+ else thread->thr_event(); \
5455 return 0; \
5556 } \
5657 TS_STATE_TYPE \
@@ -85,9 +86,7 @@
8586 c_msg.call = (state); \
8687 c_msg.ret = 0; \
8788 thread->thr_call(tsThread::thr_call_state,&c_msg); \
88- if ( c_msg.ret ) \
89- return c_msg.ret; \
9089 }
90+#define THR_THROW(ev_type) (thread->thr_throw(ev_type))
9191
92-
9392 #endif
Show on old repository browser