stdArrayQueue critical section
@@ -25,6 +25,7 @@ | ||
25 | 25 | ~stdOpenSSL(); |
26 | 26 | static stdOpenSSL * New(tsApplication * app); |
27 | 27 | private: |
28 | + tsApplication * _app; | |
28 | 29 | stdThreadOpenSSL * thread; |
29 | 30 | static stdOpenSSL * current; |
30 | 31 | static void pthreads_locking_callback( |
@@ -41,8 +41,8 @@ | ||
41 | 41 | CRYPTO_set_locking_callback(pthreads_locking_callback); |
42 | 42 | |
43 | 43 | REF_SET(thread,NEW stdThreadOpenSSL(this)); |
44 | - tsThread::ins_setup(thread); | |
45 | - | |
44 | + app->getThreadRoot()->ins_setup(thread); | |
45 | + REF_SET(_app,app); | |
46 | 46 | } |
47 | 47 | |
48 | 48 | stdOpenSSL::~stdOpenSSL() |
@@ -65,8 +65,9 @@ | ||
65 | 65 | |
66 | 66 | CRYPTO_cleanup_all_ex_data(); |
67 | 67 | |
68 | - tsThread::del_setup(thread); | |
68 | + _app->getThreadRoot()->del_setup(thread); | |
69 | 69 | REF_SET(thread,0); |
70 | + REF_SET(_app,0); | |
70 | 71 | |
71 | 72 | ::printf("stdOpenSSL finish\n"); |
72 | 73 | } |
@@ -20,7 +20,7 @@ | ||
20 | 20 | void inherit(gtType * parent, |
21 | 21 | gtType * target, |
22 | 22 | gtTypeLockOn * lockon); |
23 | - virtual int priority(); | |
23 | + virtual int priority(tinyState * caller); | |
24 | 24 | |
25 | 25 | gtType *& parent; |
26 | 26 | private: |
@@ -67,15 +67,18 @@ | ||
67 | 67 | } |
68 | 68 | |
69 | 69 | int |
70 | -gtTypeLockOnHelper_::priority() | |
70 | +gtTypeLockOnHelper_::priority(tinyState * caller) | |
71 | 71 | { |
72 | 72 | gtType * pp; |
73 | - if ( fix_pri ) | |
73 | + if ( fix_pri ) { | |
74 | + if ( caller ) | |
75 | + listen(caller,TSE_PRIORITY); | |
74 | 76 | return fix_pri; |
77 | + } | |
75 | 78 | for ( pp = parent ; pp ; pp = dynamic_cast<gtType*>(pp->parent) ) |
76 | 79 | if ( pp->helper ) |
77 | - return pp->helper->priority(); | |
78 | - return parent->priority(); | |
80 | + return pp->helper->priority(caller); | |
81 | + return parent->priority(caller); | |
79 | 82 | } |
80 | 83 | |
81 | 84 |
@@ -95,6 +98,7 @@ | ||
95 | 98 | TS_STATE(ACT_TINYSTATE_START) |
96 | 99 | { |
97 | 100 | int pri; |
101 | +tinyState * gbr; | |
98 | 102 | if ( C_TEST(parent->state,C_ZOM|C_FIN) ) |
99 | 103 | return rDO|FIN_START; |
100 | 104 | if ( C_TEST(target->state,C_ZOM|C_FIN) ) |
@@ -102,11 +106,16 @@ | ||
102 | 106 | if ( !target->is_updated(ifThis) ) |
103 | 107 | return ACT_START; |
104 | 108 | REF_SET(idstr,(NEW stdString("gtTypeLockOnHelper:%i:", |
105 | - pri=priority()))->add(parent->uri())); | |
106 | - if ( application->get_global(idstr->get_str()) == 0 ) { | |
109 | + pri=priority(0)))->add(parent->uri())); | |
110 | + gbr = dynamic_cast<tinyState*>(application->get_global(idstr->get_str())); | |
111 | + if ( gbr == 0 ) { | |
107 | 112 | application->set_global(idstr->get_str(),ifThis); |
108 | 113 | fix_pri = pri + 1; |
114 | + invoke_listen(NEW stdEvent(TSE_PRIORITY,ifThis,(INTEGER64)0)); | |
109 | 115 | } |
116 | + else { | |
117 | + gbr->listen(ifThis,TSE_DESTROY); | |
118 | + } | |
110 | 119 | application->gc->exe(ifThis); |
111 | 120 | return rDO|ACT_RET; |
112 | 121 | } |
@@ -124,7 +133,10 @@ | ||
124 | 133 | lockon->sync(); |
125 | 134 | if ( fix_pri ) |
126 | 135 | parent->report(ifThis); |
127 | - fix_pri = 0; | |
136 | + if ( fix_pri ) { | |
137 | + fix_pri = 0; | |
138 | + invoke_listen(NEW stdEvent(TSE_PRIORITY,ifThis,(INTEGER64)0)); | |
139 | + } | |
128 | 140 | return ACT_START; |
129 | 141 | } |
130 | 142 | TS_STATE(FIN_START) |
@@ -132,6 +144,7 @@ | ||
132 | 144 | REF_SET(lockon,0); |
133 | 145 | REF_SET(target,0); |
134 | 146 | REF_SET(idstr,0); |
147 | + invoke_listen(NEW stdEvent(TSE_PRIORITY,ifThis,(INTEGER64)0)); | |
135 | 148 | return rDO|FIN_TINYSTATE_START; |
136 | 149 | } |
137 | 150 |
@@ -605,7 +605,7 @@ | ||
605 | 605 | ok = this->read_objs->del(mfd,ts_io_get_comein); |
606 | 606 | if ( ok == 0 ) |
607 | 607 | break; |
608 | - result->ins(ok->obj->priority(),ok); | |
608 | + result->ins(ok->obj->priority(0),ok); | |
609 | 609 | } |
610 | 610 | mfd->fsp = &fdwrite; |
611 | 611 | mfd->fexp = &fdexp; |
@@ -613,7 +613,7 @@ | ||
613 | 613 | ok = this->write_objs->del(mfd,ts_io_get_comein); |
614 | 614 | if ( ok == 0 ) |
615 | 615 | break; |
616 | - result->ins(ok->obj->priority(),ok); | |
616 | + result->ins(ok->obj->priority(0),ok); | |
617 | 617 | } |
618 | 618 | } |
619 | 619 | { |
@@ -624,7 +624,7 @@ | ||
624 | 624 | now,ts_io_get_interval_comein); |
625 | 625 | if ( ok == 0 ) |
626 | 626 | break; |
627 | - result->ins(ok->obj->priority(),ok); | |
627 | + result->ins(ok->obj->priority(0),ok); | |
628 | 628 | } |
629 | 629 | } |
630 | 630 | for ( ; ; ) { |
@@ -6,19 +6,19 @@ | ||
6 | 6 | { |
7 | 7 | } |
8 | 8 | |
9 | -stdHalfOrderQueueTS::stdHalfOrderQueueTS(stdHalfOrderQueueTS * q) | |
9 | +stdHalfOrderQueueTS::stdHalfOrderQueueTS(stdHalfOrderQueueTS * q,tinyState * caller) | |
10 | 10 | { |
11 | 11 | tinyState * ts; |
12 | 12 | for ( ; ; ) { |
13 | - ts = q->del(); | |
13 | + ts = q->del(caller); | |
14 | 14 | if ( ts == 0 ) |
15 | 15 | break; |
16 | - ins(ts->priority(),ts); | |
16 | + ins(ts->priority(0),ts); | |
17 | 17 | } |
18 | 18 | } |
19 | 19 | |
20 | 20 | tinyState * |
21 | -stdHalfOrderQueueTS::del() | |
21 | +stdHalfOrderQueueTS::del(tinyState * caller) | |
22 | 22 | { |
23 | 23 | tinyState * ret; |
24 | 24 | int key,key2; |
@@ -26,10 +26,10 @@ | ||
26 | 26 | ret = dynamic_cast<tinyState*>(stdHalfOrderQueue::del(&key)); |
27 | 27 | if ( ret == 0 ) |
28 | 28 | return 0; |
29 | - key2 = ret->priority(); | |
30 | - if ( key == key2 ) | |
29 | + key2 = ret->priority(caller); | |
30 | + if ( caller == 0 || key == key2 ) | |
31 | 31 | return ret; |
32 | - hoq = NEW stdHalfOrderQueueTS(this); | |
32 | + hoq = NEW stdHalfOrderQueueTS(this,caller); | |
33 | 33 | hoq->ins(key2,ret); |
34 | 34 | REF_SET(top,hoq->__top()); |
35 | 35 | return dynamic_cast<tinyState*>(stdHalfOrderQueue::del(&key)); |
@@ -1,4 +1,7 @@ | ||
1 | 1 | |
2 | + | |
3 | + | |
4 | + | |
2 | 5 | #include <stdio.h> |
3 | 6 | |
4 | 7 | #include "_ts/c++/tinyState_.h" |
@@ -53,7 +56,7 @@ | ||
53 | 56 | virtual int eventHandler(stdEvent * ev); |
54 | 57 | static int static_eventHandler(tinyState*,stdEvent*); |
55 | 58 | void inherit(tinyState* parent); |
56 | - virtual int priority(); | |
59 | + virtual int priority(tinyState * caller=0); | |
57 | 60 | virtual void wakeup(); |
58 | 61 | void remove_listener(stdEventHandle*); |
59 | 62 | virtual void destroy(); |
@@ -278,7 +281,8 @@ | ||
278 | 281 | onlyOneEvent_mask = |
279 | 282 | TSE_EVM(TSE_WAKEUP)| |
280 | 283 | TSE_EVM(TSE_STATE)| |
281 | - TSE_EVM(TSE_DESTROY); | |
284 | + TSE_EVM(TSE_DESTROY)| | |
285 | + TSE_EVM(TSE_PRIORITY); | |
282 | 286 | REF_SET(this->que,NEW stdQueue<stdEvent>()); |
283 | 287 | REF_SET(this->rv_que,NEW stdQueue<stdEvent>()); |
284 | 288 | REF_SET(this->parent,parent); |
@@ -359,7 +363,7 @@ | ||
359 | 363 | } |
360 | 364 | |
361 | 365 | int |
362 | -tinyState_::priority() | |
366 | +tinyState_::priority(tinyState * caller) | |
363 | 367 | { |
364 | 368 | return TS_DEFAULT_PRIORITY; |
365 | 369 | } |
@@ -749,6 +753,8 @@ | ||
749 | 753 | tinyState_::listen(tinyState * listener,int type,TS_HANDLER_FUNC handler) |
750 | 754 | { |
751 | 755 | stdEventHandle * eh; |
756 | + if ( listener == 0 ) | |
757 | + return 0; | |
752 | 758 | eh = this->search_listen(type,listener); |
753 | 759 | if ( eh ) |
754 | 760 | return eh; |
@@ -759,6 +765,8 @@ | ||
759 | 765 | tinyState_::listen(tinyState * listener,int type) |
760 | 766 | { |
761 | 767 | stdEventHandle * eh; |
768 | + if ( listener == 0 ) | |
769 | + return 0; | |
762 | 770 | eh = this->search_listen(type,listener); |
763 | 771 | if ( eh ) |
764 | 772 | return eh; |
@@ -44,12 +44,12 @@ | ||
44 | 44 | |
45 | 45 | void inherit(tinyState* parent,TS_APPLICATION_FUNC func,void * arg); |
46 | 46 | stdFrameWork * getFrameWork(); |
47 | + tsThreadRoot * getThreadRoot(); | |
47 | 48 | void setFrameWork(stdFrameWork * fw); |
48 | 49 | void set_global(const char * name,stdObject *); |
49 | 50 | void del_global(const char * name); |
50 | 51 | stdObject * get_global(const char * name); |
51 | 52 | |
52 | - int dinamic_priority; | |
53 | 53 | tsInsensitive * insensitive; |
54 | 54 | tsGC * gc; |
55 | 55 | tinyState * refCountQueue; |
@@ -59,6 +59,7 @@ | ||
59 | 59 | uint8_t restart_flag; |
60 | 60 | private: |
61 | 61 | stdFrameWork * fwClass; |
62 | + tsThreadRoot * threadRoot; | |
62 | 63 | stdQueue<tsApplicationGlobal> * global; |
63 | 64 | void (*initial_func)(tsApplication*,void*); |
64 | 65 | void * initial_arg; |
@@ -139,6 +140,14 @@ | ||
139 | 140 | invoke_listen(NEW stdEvent(TSE_UPDATED,ifThis,(INTEGER64)0)); |
140 | 141 | } |
141 | 142 | |
143 | + | |
144 | +tsThreadRoot * | |
145 | +tsApplication_::getThreadRoot() | |
146 | +{ | |
147 | + return threadRoot; | |
148 | +} | |
149 | + | |
150 | + | |
142 | 151 | void |
143 | 152 | tsApplication_::del_global(const char * name) |
144 | 153 | { |
@@ -179,6 +188,7 @@ | ||
179 | 188 | REF_SET(gc,NEW tsGC(ifThis)); |
180 | 189 | if ( this->initial_func ) |
181 | 190 | (*this->initial_func)(ifThis,this->initial_arg); |
191 | + REF_SET(threadRoot,NEW tsThreadRoot(ifThis)); | |
182 | 192 | return rDO|INI_APPLICATION_START; |
183 | 193 | } |
184 | 194 | TS_STATE(INI_APPLICATION_START) |
@@ -197,6 +207,15 @@ | ||
197 | 207 | } |
198 | 208 | TS_STATE(FIN_TS_APPLICATION_START) |
199 | 209 | { |
210 | + threadRoot->destroy(); | |
211 | + return rDO|FIN_THREAD_ROOT_LOOP; | |
212 | +} | |
213 | +TS_STATE(FIN_THREAD_ROOT_LOOP) | |
214 | +{ | |
215 | + if ( this->fwClass && this->fwClass->loop(ifThis) > 0 ) | |
216 | + return rDO; | |
217 | + REF_SET(threadRoot,0); | |
218 | + | |
200 | 219 | eventHandler_flag = 0; |
201 | 220 | insensitive->listen(ifThis,TSE_DESTROY); |
202 | 221 | insensitive->destroy(); |
@@ -210,10 +229,15 @@ | ||
210 | 229 | return 0; |
211 | 230 | if ( C_TEST(gc->state,C_ZOM) == 0 ) |
212 | 231 | return 0; |
213 | - tsThread::stop(); | |
232 | + return rDO|FIN_TS_APPLICATION_WAIT2; | |
233 | +} | |
234 | +TS_STATE(FIN_TS_APPLICATION_WAIT2) | |
235 | +{ | |
214 | 236 | REF_SET(this->fwClass,0); |
215 | 237 | REF_SET(this->global,0); |
216 | 238 | REF_SET(insensitive,0); |
239 | + REF_SET(gc,0); | |
240 | + REF_SET(threadRoot,0); | |
217 | 241 | if ( restart_flag ) { |
218 | 242 | restart_flag = 0; |
219 | 243 | return rDO|INI_START; |
@@ -22,12 +22,14 @@ | ||
22 | 22 | tinyState * parent); |
23 | 23 | |
24 | 24 | void exe(tinyState * caller); |
25 | + virtual stdEvent * filter(stdEvent * ev); | |
26 | + | |
25 | 27 | private: |
26 | 28 | protected: |
27 | 29 | stdHalfOrderQueueTS * que; |
28 | 30 | int last_pri; |
29 | - int dinamic_priority; | |
30 | 31 | |
32 | + unsigned priority_flag:1; | |
31 | 33 | sTimer timer; |
32 | 34 | }; |
33 | 35 |
@@ -72,11 +74,20 @@ | ||
72 | 74 | tsGC_::exe(tinyState * caller) |
73 | 75 | { |
74 | 76 | int pri; |
75 | - que->ins(caller->priority(),caller); | |
77 | + que->ins(caller->priority(ifThis),caller); | |
76 | 78 | wakeup(); |
77 | 79 | } |
78 | 80 | |
79 | 81 | |
82 | +stdEvent * | |
83 | +tsGC_::filter(stdEvent * ev) | |
84 | +{ | |
85 | + if ( ev->type == TSE_PRIORITY ) | |
86 | + priority_flag = 1; | |
87 | + return ev; | |
88 | +} | |
89 | + | |
90 | + | |
80 | 91 | /******************************************* |
81 | 92 | STATE MACHINE |
82 | 93 | ********************************************/ |
@@ -83,7 +94,6 @@ | ||
83 | 94 | |
84 | 95 | TS_STATE(INI_START) |
85 | 96 | { |
86 | - dinamic_priority = application->dinamic_priority; | |
87 | 97 | REF_SET(que,(NEW stdHalfOrderQueueTS())); |
88 | 98 | return rDO|ACT_START; |
89 | 99 | } |
@@ -108,16 +118,16 @@ | ||
108 | 118 | int pri; |
109 | 119 | if ( tsGC::timer_mode && timer.is_expire(ifThis) ) |
110 | 120 | return rDO|ACT_START; |
111 | - if ( dinamic_priority != application->dinamic_priority ) { | |
112 | - dinamic_priority = application->dinamic_priority; | |
113 | - REF_SET(que,NEW stdHalfOrderQueueTS(que)); | |
121 | + if ( priority_flag ) { | |
122 | + priority_flag = 0; | |
123 | + REF_SET(que,NEW stdHalfOrderQueueTS(que,ifThis)); | |
114 | 124 | } |
115 | - ok = que->del(); | |
125 | + ok = que->del(0); | |
116 | 126 | if ( ok == 0 ) |
117 | 127 | return rDO|ACT_START; |
118 | - pri = ok->priority(); | |
128 | + pri = ok->priority(0); | |
119 | 129 | if ( tsGC::timer_mode == 0 && last_pri && last_pri != pri ) { |
120 | - que->ins(ok->priority(),ok); | |
130 | + que->ins(ok->priority(ifThis),ok); | |
121 | 131 | return rDO|ACT_START; |
122 | 132 | } |
123 | 133 | ok->eventHandler( |
@@ -1,3 +1,10 @@ | ||
1 | +// | |
2 | +// after rev.5992. | |
3 | +// | |
4 | +// | |
5 | + | |
6 | + | |
7 | + | |
1 | 8 | #include <pthread.h> |
2 | 9 | #include <sys/time.h> |
3 | 10 | #include <sys/types.h> |
@@ -8,13 +15,7 @@ | ||
8 | 15 | #include "ts/c++/stdQueue.h" |
9 | 16 | #include "ts/c++/co_tsThreadKill.h" |
10 | 17 | |
11 | -#define R (0) | |
12 | -#define W (1) | |
13 | 18 | |
14 | -#define ACC_TIMER 2 | |
15 | -#define DEC_TIMER 10 | |
16 | - | |
17 | - | |
18 | 19 | CLASS_TINYSTATE(tsThread,tinyState) |
19 | 20 | |
20 | 21 | #if 0 |
@@ -23,28 +24,15 @@ | ||
23 | 24 | #include "ts/c++/fwIO.h" |
24 | 25 | #include "ts/c++/stdInterval.h" |
25 | 26 | #include "ts/c++/sTimer.h" |
27 | +#include "ts/c++/stdHalfOrderQueue.h" | |
26 | 28 | |
27 | -#define PIPE_SIZE 10 | |
28 | 29 | |
29 | -class thrQueue; | |
30 | - | |
31 | - | |
32 | -#define THR_STATE_INIT 0 | |
33 | -#define THR_STATE_RUN 1 | |
34 | -#define THR_STATE_TERMINATE 4 | |
35 | -#define THR_STATE_CALL_FUNC 5 | |
36 | -#define THR_STATE_SUSPEND 6 | |
37 | - | |
38 | 30 | class TS_THISCLASS : public TS_BASECLASS { |
39 | 31 | public: |
40 | - tsThread * thread_next; | |
41 | - tsThread * thread_prev; | |
42 | - | |
43 | - tsThread * que_next; | |
44 | - | |
45 | 32 | tsThread_( |
46 | 33 | tinyState * parent, |
47 | 34 | tsThread ** this_p, |
35 | + tsThreadRoot * _root, | |
48 | 36 | TS_STATE_TYPE pos_state, |
49 | 37 | TS_STATE_TYPE thr_func, |
50 | 38 | stdEvent * evt, |
@@ -52,6 +40,7 @@ | ||
52 | 40 | void inherit( |
53 | 41 | tinyState * parent, |
54 | 42 | tsThread ** this_p, |
43 | + tsThreadRoot * _root, | |
55 | 44 | TS_STATE_TYPE pos_state, |
56 | 45 | TS_STATE_TYPE thr_func, |
57 | 46 | stdEvent * evt, |
@@ -63,15 +52,6 @@ | ||
63 | 52 | void kill_finish(); |
64 | 53 | void thr_set_kill_state(pthread_t * me=0); |
65 | 54 | |
66 | - static void ins_setup(stdThread * thr); | |
67 | - static void del_setup(stdThread * thr); | |
68 | - static int get_current_tsThread_nos(); | |
69 | - static void stop(); | |
70 | - | |
71 | - static int max_thread(); | |
72 | - static void max_thread(int nos); | |
73 | - | |
74 | - static void * __tsThread_body(void * arg); | |
75 | 55 | void __tsThread_body_private(); |
76 | 56 | |
77 | 57 | void thr_call(THR_FUNC func,void * msg); |
@@ -78,7 +58,9 @@ | ||
78 | 58 | void thr_event(); |
79 | 59 | void thr_throw(int ev_type); |
80 | 60 | void thr_throw_invoke(); |
61 | + void base_call_func(int force_ret); | |
81 | 62 | |
63 | + | |
82 | 64 | int thr_state; |
83 | 65 | int thr_attr; |
84 | 66 |
@@ -89,49 +71,15 @@ | ||
89 | 71 | INTEGER64 ins_time; |
90 | 72 | pthread_cond_t private_cond; |
91 | 73 | |
74 | + | |
75 | + tsThread * _thr_next; | |
76 | + uint8_t thr_inQue; | |
92 | 77 | private: |
78 | + tsThreadRoot * root; | |
93 | 79 | |
94 | - static void _do_setup(); | |
95 | - static void _do_cleanup(); | |
80 | + sTimer timer; | |
96 | 81 | |
97 | - fwIO * io; | |
98 | 82 | |
99 | - static tsThread * thread_head; | |
100 | - | |
101 | - static int initialize_flag; | |
102 | - static int max_thread_nos; | |
103 | - static int current_thread_nos; | |
104 | - static int current_tsThread_nos; | |
105 | - static int run_thread_nos; | |
106 | - static uint8_t thread_stop; | |
107 | - static pthread_mutex_t mu; | |
108 | - static pthread_cond_t cond; | |
109 | - static int pipe_fd[2]; | |
110 | - static tsThread * host_thread; | |
111 | - | |
112 | - static thrQueue * ready; | |
113 | - static thrQueue * call_main; | |
114 | - static char buffer[PIPE_SIZE]; | |
115 | - static int pipe_count; | |
116 | - | |
117 | - static INTEGER64 acc_start_time; | |
118 | - static INTEGER64 delta_start_time; | |
119 | - static INTEGER64 total_run_time; | |
120 | - static INTEGER64 last_dec_time; | |
121 | - | |
122 | - static stdQueue<stdThread> * setup_list; | |
123 | - | |
124 | - static void lock(); | |
125 | - static void unlock(); | |
126 | - static void cond_signal(); | |
127 | - static void create(); | |
128 | - static void launch(); | |
129 | - static int cond_timed_wait(); | |
130 | - void inc_max_thread_nos(); | |
131 | - static void acc_max_thread_nos(int acc); | |
132 | - | |
133 | - static void _push_pipe(); | |
134 | - | |
135 | 83 | stdEvent * initial_event; |
136 | 84 | TS_STATE_TYPE pos_state; |
137 | 85 | TS_STATE_TYPE thread_func; |
@@ -140,14 +88,9 @@ | ||
140 | 88 | pthread_t * current_pthread; |
141 | 89 | uint8_t kill_flag; |
142 | 90 | |
143 | - void base_call_func(tsThread * target,int force_ret); | |
144 | - | |
145 | 91 | void _wait_private_cond(); |
146 | 92 | |
147 | - tsThread ** this_p; | |
148 | 93 | protected: |
149 | - sTimer timer; | |
150 | - unsigned timer_flag:1; | |
151 | 94 | unsigned private_cond_init:1; |
152 | 95 | |
153 | 96 | INTEGER64 throw_ev_flags; |
@@ -154,47 +97,7 @@ | ||
154 | 97 | }; |
155 | 98 | |
156 | 99 | |
157 | -class thrQueue : public stdObject { | |
158 | -public: | |
159 | - tsThread * head; | |
160 | - tsThread * tail; | |
161 | - int count; | |
162 | 100 | |
163 | - thrQueue() { | |
164 | - } | |
165 | - ~thrQueue() { | |
166 | - } | |
167 | - void ins(tsThread * thr) { | |
168 | - if ( thr->que_in ) | |
169 | - return; | |
170 | - thr->que_in = 1; | |
171 | - thr->que_next = 0; | |
172 | - if ( head == 0 ) | |
173 | - head = tail = thr; | |
174 | - else { | |
175 | - tail->que_next = thr; | |
176 | - tail = thr; | |
177 | - } | |
178 | - thr->ins_time = stdInterval::now(); | |
179 | - count ++; | |
180 | - } | |
181 | - tsThread * del() { | |
182 | - tsThread * ret; | |
183 | - if ( head == 0 ) | |
184 | - return 0; | |
185 | - ret = head; | |
186 | - head = ret->que_next; | |
187 | - if ( head == 0 ) | |
188 | - tail = 0; | |
189 | - if ( ret->que_in == 0 ) | |
190 | - stdObject::panic("NON QUEUED OBJECT !!"); | |
191 | - ret->que_in = 0; | |
192 | - count --; | |
193 | - return ret; | |
194 | - } | |
195 | -}; | |
196 | - | |
197 | - | |
198 | 101 | TS_END_IMPLEMENT |
199 | 102 | |
200 | 103 | TS_BEGIN_INTERFACE |
@@ -211,120 +114,17 @@ | ||
211 | 114 | |
212 | 115 | |
213 | 116 | |
214 | -/******************************************* | |
215 | - RELATED FUNCTIONS | |
216 | -********************************************/ | |
217 | 117 | |
218 | -stdThread::stdThread(stdObject *parent) | |
219 | -{ | |
220 | - REF_SET(this->parent,parent); | |
221 | -} | |
222 | -stdThread::~stdThread() | |
223 | -{ | |
224 | - REF_SET(parent,0); | |
225 | -} | |
226 | -void | |
227 | -stdThread::setup() | |
228 | -{ | |
229 | -} | |
230 | -void | |
231 | -stdThread::cleanup() | |
232 | -{ | |
233 | -} | |
234 | - | |
235 | 118 | /******************************************* |
236 | 119 | PUBLIC FUNCTIONS |
237 | 120 | ********************************************/ |
238 | 121 | |
239 | -tsThread * | |
240 | -tsThread_::thread_head; | |
241 | 122 | |
242 | -int | |
243 | -tsThread_::initialize_flag; | |
244 | -int | |
245 | -tsThread_::max_thread_nos = 1; | |
246 | -int | |
247 | -tsThread_::current_thread_nos; | |
248 | -int | |
249 | -tsThread_::run_thread_nos; | |
250 | -int | |
251 | -tsThread_::current_tsThread_nos; | |
252 | -pthread_mutex_t | |
253 | -tsThread_::mu; | |
254 | -pthread_cond_t | |
255 | -tsThread_::cond; | |
256 | -int | |
257 | -tsThread_::pipe_fd[2]; | |
258 | -uint8_t | |
259 | -tsThread_::thread_stop; | |
260 | 123 | |
261 | -tsThread * | |
262 | -tsThread_::host_thread; | |
263 | -thrQueue * | |
264 | -tsThread_::ready; | |
265 | -thrQueue * | |
266 | -tsThread_::call_main; | |
267 | -char | |
268 | -tsThread_::buffer[PIPE_SIZE]; | |
269 | -INTEGER64 | |
270 | -tsThread_::acc_start_time; | |
271 | -INTEGER64 | |
272 | -tsThread_::delta_start_time; | |
273 | -INTEGER64 | |
274 | -tsThread_::total_run_time; | |
275 | -INTEGER64 | |
276 | -tsThread_::last_dec_time; | |
277 | -int | |
278 | -tsThread_::pipe_count; | |
279 | -stdQueue<stdThread> * | |
280 | -tsThread_::setup_list; | |
281 | - | |
282 | -void | |
283 | -tsThread_::ins_setup(stdThread * thr) | |
284 | -{ | |
285 | - lock(); | |
286 | - if ( setup_list == 0 ) | |
287 | - REF_SET(setup_list,(NEW stdQueue<stdThread>())); | |
288 | - setup_list->ins(MAX_INTEGER64,thr); | |
289 | - unlock(); | |
290 | -} | |
291 | - | |
292 | -void | |
293 | -tsThread_::del_setup(stdThread * thr) | |
294 | -{ lock(); | |
295 | - if ( setup_list ) | |
296 | - setup_list->del(thr,0); | |
297 | - unlock(); | |
298 | -} | |
299 | - | |
300 | -void | |
301 | -tsThread_::_do_setup() | |
302 | -{ | |
303 | -stdQueueElement<stdThread> * elp; | |
304 | - if ( setup_list ) | |
305 | - for ( elp = setup_list->head ; elp ; elp = elp->next ) | |
306 | - elp->data->setup(); | |
307 | -} | |
308 | - | |
309 | -void | |
310 | -tsThread_::_do_cleanup() | |
311 | -{ | |
312 | -stdQueueElement<stdThread> * elp; | |
313 | - if ( setup_list ) | |
314 | - for ( elp = setup_list->head ; elp ; elp = elp->next ) | |
315 | - elp->data->cleanup(); | |
316 | -} | |
317 | - | |
318 | -int | |
319 | -tsThread_::get_current_tsThread_nos() | |
320 | -{ | |
321 | - return current_tsThread_nos; | |
322 | -} | |
323 | - | |
324 | - | |
325 | 124 | tsThread_::tsThread_( |
326 | 125 | tinyState * parent, |
327 | 126 | tsThread ** this_p, |
127 | + tsThreadRoot * _root, | |
328 | 128 | TS_STATE_TYPE pos_state, |
329 | 129 | TS_STATE_TYPE thr_func, |
330 | 130 | stdEvent * evt, |
@@ -332,27 +132,12 @@ | ||
332 | 132 | : |
333 | 133 | tinyState_(parent) |
334 | 134 | { |
335 | - if ( initialize_flag == 0 ) { | |
336 | - initialize_flag = 1; | |
337 | - pthread_mutex_init(&mu,0); | |
338 | - pthread_cond_init(&cond,0); | |
339 | - if ( soPIPE(pipe_fd) < 0 ) { | |
340 | - perror("THREAD PIPE"); | |
341 | - stdObject::panic("THREAD PIPE"); | |
342 | - } | |
343 | - REF_SET(ready,NEW thrQueue()); | |
344 | - REF_SET(call_main,NEW thrQueue()); | |
345 | - } | |
346 | - current_tsThread_nos ++; | |
347 | - | |
348 | - this->this_p = this_p; | |
349 | 135 | this->pos_state = pos_state; |
350 | 136 | thr_attr = attr; |
351 | 137 | thr_state = THR_STATE_INIT; |
352 | 138 | REF_SET(initial_event,evt); |
139 | + REF_SET(root,_root); | |
353 | 140 | thread_func = thr_func; |
354 | - if ( max_thread_nos == 0 ) | |
355 | - max_thread(1); | |
356 | 141 | } |
357 | 142 | |
358 | 143 | void |
@@ -359,22 +144,13 @@ | ||
359 | 144 | tsThread_::inherit( |
360 | 145 | tinyState * parent, |
361 | 146 | tsThread ** this_p, |
147 | + tsThreadRoot * _root, | |
362 | 148 | TS_STATE_TYPE pos_state, |
363 | 149 | TS_STATE_TYPE thr_func, |
364 | 150 | stdEvent * evt, |
365 | 151 | int attr) |
366 | 152 | { |
367 | - if ( thread_head == 0 ) { | |
368 | - REF_SET(thread_head,ifThis); | |
369 | - REF_SET(thread_next,ifThis); | |
370 | - REF_SET(thread_prev,ifThis); | |
371 | - } | |
372 | - else { | |
373 | - REF_SET(thread_next,thread_head); | |
374 | - REF_SET(thread_prev,thread_head->thread_prev); | |
375 | - REF_SET(thread_next->thread_prev,ifThis); | |
376 | - REF_SET(thread_prev->thread_next,ifThis); | |
377 | - } | |
153 | + REF_SET(*this_p,ifThis); | |
378 | 154 | this->TS_BASECLASS::inherit(parent); |
379 | 155 | } |
380 | 156 |
@@ -384,15 +160,15 @@ | ||
384 | 160 | { |
385 | 161 | if ( ev_type == 0 ) |
386 | 162 | return; |
387 | - lock(); | |
163 | + root->lock(); | |
388 | 164 | if ( throw_ev_flags & (1LL<<ev_type) ) { |
389 | - unlock(); | |
165 | + root->unlock(); | |
390 | 166 | return; |
391 | 167 | } |
392 | 168 | throw_ev_flags |= (1LL<<ev_type); |
393 | - call_main->ins(ifThis); | |
394 | - _push_pipe(); | |
395 | - unlock(); | |
169 | + root->_insQue(ifThis); | |
170 | + root->_push_pipe(); | |
171 | + root->unlock(); | |
396 | 172 | } |
397 | 173 | |
398 | 174 | void |
@@ -400,10 +176,10 @@ | ||
400 | 176 | { |
401 | 177 | int ev_type; |
402 | 178 | INTEGER64 tef; |
403 | - lock(); | |
179 | + root->lock(); | |
404 | 180 | tef = throw_ev_flags; |
405 | 181 | throw_ev_flags = 0; |
406 | - unlock(); | |
182 | + root->unlock(); | |
407 | 183 | for ( ; tef ; ) |
408 | 184 | for ( ev_type = 1 ; tef >= (1LL<<ev_type) ; ev_type ++ ) { |
409 | 185 | if ( !(tef & (1LL<<ev_type)) ) { |
@@ -418,7 +194,7 @@ | ||
418 | 194 | void |
419 | 195 | tsThread_::thr_call(THR_FUNC func,void*msg) |
420 | 196 | { |
421 | - lock(); | |
197 | + root->lock(); | |
422 | 198 | if ( private_cond_init == 0 ) { |
423 | 199 | pthread_cond_init(&private_cond,0); |
424 | 200 | private_cond_init = 1; |
@@ -426,322 +202,62 @@ | ||
426 | 202 | thr_call_func = func; |
427 | 203 | thr_call_msg = msg; |
428 | 204 | thr_state = THR_STATE_CALL_FUNC; |
429 | - call_main->ins(ifThis); | |
430 | - _push_pipe(); | |
205 | + root->_insQue(ifThis); | |
206 | + root->_push_pipe(); | |
431 | 207 | _wait_private_cond(); |
432 | - unlock(); | |
208 | + root->unlock(); | |
433 | 209 | } |
434 | 210 | |
435 | 211 | void |
436 | -tsThread_::thr_event() | |
437 | -{ | |
438 | - lock(); | |
439 | - if ( thr_state != THR_STATE_SUSPEND ) { | |
440 | - unlock(); | |
441 | - return; | |
442 | - } | |
443 | - unlock(); | |
444 | - base_call_func(ifThis,1); | |
445 | -} | |
446 | - | |
447 | - | |
448 | -TS_STATE_TYPE | |
449 | -tsThread::thr_call_state(tinyState * THIS,void * msg) | |
450 | -{ | |
451 | -THR_CALL_TYPE * c_msg; | |
452 | -TS_STATE_TYPE ret; | |
453 | - c_msg = (THR_CALL_TYPE*)msg; | |
454 | - c_msg->ret = ret = THIS->do_thread(c_msg->call, | |
455 | - NEW stdEvent(TSE_PACKET, | |
456 | - THIS, | |
457 | - c_msg->msg)); | |
458 | - return ret; | |
459 | -} | |
460 | - | |
461 | - | |
462 | - | |
463 | -void | |
464 | -tsThread_::base_call_func(tsThread * target,int force_ret) | |
465 | -{ | |
466 | -TS_STATE_TYPE ret; | |
467 | - if ( target->thr_call_func ) | |
468 | - ret = (*target->thr_call_func)(target->parent,target->thr_call_msg); | |
469 | - else ret = force_ret; | |
470 | - lock(); | |
471 | - if ( ret == 0 ) { | |
472 | - target->thr_state = THR_STATE_SUSPEND; | |
473 | - } | |
474 | - else { | |
475 | - target->thr_state = THR_STATE_RUN; | |
476 | - pthread_cond_signal(&target->private_cond); | |
477 | - } | |
478 | - unlock(); | |
479 | -} | |
480 | - | |
481 | -void | |
482 | 212 | tsThread_::_wait_private_cond() |
483 | 213 | { |
484 | 214 | int ret; |
485 | 215 | for ( ; thr_state != THR_STATE_RUN ; ) { |
486 | - pthread_cond_wait(&private_cond,&mu); | |
216 | + pthread_cond_wait(&private_cond,&root->mu); | |
487 | 217 | } |
488 | 218 | } |
489 | 219 | |
490 | -int | |
491 | -tsThread_::max_thread() | |
492 | -{ | |
493 | - return max_thread_nos; | |
494 | -} | |
495 | -void | |
496 | -tsThread_::max_thread(int nos) | |
497 | -{ | |
498 | -} | |
499 | 220 | |
500 | 221 | void |
501 | -tsThread_::lock() | |
222 | +tsThread_::thr_event() | |
502 | 223 | { |
503 | - pthread_mutex_lock(&mu); | |
504 | -} | |
505 | - | |
506 | -void | |
507 | -tsThread_::unlock() | |
508 | -{ | |
509 | - pthread_mutex_unlock(&mu); | |
510 | -} | |
511 | - | |
512 | -void | |
513 | -tsThread_::cond_signal() | |
514 | -{ | |
515 | - pthread_cond_signal(&cond); | |
516 | -} | |
517 | - | |
518 | - | |
519 | -int | |
520 | -tsThread_::cond_timed_wait() | |
521 | -{ | |
522 | -struct timespec timeout; | |
523 | -struct timeval now; | |
524 | -int ret; | |
525 | - gettimeofday(&now,0); | |
526 | - timeout.tv_sec = now.tv_sec + DEC_TIMER; | |
527 | - timeout.tv_nsec = now.tv_usec * 1000; | |
528 | - for ( ; ; ) { | |
529 | - ret = pthread_cond_timedwait(&cond,&mu,&timeout); | |
530 | - switch ( ret ) { | |
531 | - case ETIMEDOUT: | |
532 | - return -1; | |
533 | - case EINTR: | |
534 | - continue; | |
535 | - default: | |
536 | - return 0; | |
537 | - } | |
538 | - break; | |
539 | - } | |
540 | -} | |
541 | - | |
542 | -void | |
543 | -tsThread_::create() | |
544 | -{ | |
545 | -pthread_attr_t phy_attr; | |
546 | -pthread_t phy_thread; | |
547 | - pthread_attr_init(&phy_attr); | |
548 | - pthread_attr_setdetachstate(&phy_attr,PTHREAD_CREATE_DETACHED); | |
549 | - pthread_create(&phy_thread,&phy_attr,__tsThread_body,0); | |
550 | -} | |
551 | - | |
552 | -void | |
553 | -tsThread_::launch() | |
554 | -{ | |
555 | - if ( ready->head == 0 ) | |
224 | + root->lock(); | |
225 | + if ( thr_state != THR_STATE_SUSPEND ) { | |
226 | + root->unlock(); | |
556 | 227 | return; |
557 | - if ( max_thread_nos > 0 ) { | |
558 | - if ( current_thread_nos >= max_thread_nos ) { | |
559 | -if ( run_thread_nos >= current_thread_nos ) | |
560 | -::printf("--------------------------------------------------- LAUNCH-FULL %i %i\n",run_thread_nos,current_thread_nos); | |
561 | - cond_signal(); | |
562 | - return; | |
563 | - } | |
564 | - current_thread_nos ++; | |
565 | - create(); | |
566 | 228 | } |
567 | - else if ( current_thread_nos < current_tsThread_nos ) { | |
568 | - current_thread_nos ++; | |
569 | - create(); | |
570 | - } | |
571 | - cond_signal(); | |
229 | + root->unlock(); | |
230 | + base_call_func(1); | |
572 | 231 | } |
573 | 232 | |
574 | -void | |
575 | -tsThread_::stop() | |
576 | -{ | |
577 | - lock(); | |
578 | - thread_stop = 1; | |
579 | - for ( ; current_thread_nos ; ) { | |
580 | - cond_signal(); | |
581 | - cond_timed_wait(); | |
582 | - } | |
583 | - unlock(); | |
584 | -} | |
585 | 233 | |
586 | - | |
587 | 234 | void |
588 | -tsThread_::acc_max_thread_nos(int inc) | |
235 | +tsThread_::base_call_func(int force_ret) | |
589 | 236 | { |
590 | -INTEGER64 now; | |
591 | - if ( delta_start_time ) { | |
592 | - now = stdInterval::now(); | |
593 | -//::printf("acc_max_thread_nos %i %lli\n",run_thread_nos,now - delta_start_time); | |
594 | - total_run_time += | |
595 | - (now - delta_start_time) | |
596 | - * run_thread_nos; | |
597 | - delta_start_time = now; | |
237 | +TS_STATE_TYPE ret; | |
238 | + if ( thr_call_func ) | |
239 | + ret = (*thr_call_func)(parent,thr_call_msg); | |
240 | + else ret = force_ret; | |
241 | + root->lock(); | |
242 | + if ( ret == 0 ) { | |
243 | + thr_state = THR_STATE_SUSPEND; | |
598 | 244 | } |
599 | - if ( inc > 0 ) | |
600 | - run_thread_nos ++; | |
601 | - else if ( inc < 0 ) | |
602 | - run_thread_nos --; | |
603 | -} | |
604 | - | |
605 | -void | |
606 | -tsThread_::inc_max_thread_nos() | |
607 | -{ | |
608 | -INTEGER64 now,interval; | |
609 | -int launch_flag; | |
610 | - lock(); | |
611 | - launch_flag = 0; | |
612 | - for ( ; ; ) { | |
613 | - now = stdInterval::now(); | |
614 | - if ( acc_start_time == 0 ) { | |
615 | - acc_start_time = now; | |
616 | - delta_start_time = now; | |
617 | - total_run_time = 0; | |
618 | - timer_flag = 1; | |
619 | - timer.start(ifThis,ACC_TIMER*1000*1000,TSE_TIMER); | |
620 | - break; | |
621 | - } | |
622 | - if ( timer_flag == 0 ) { | |
623 | - INTEGER64 dif; | |
624 | - dif = ACC_TIMER*1000*1000 - now + acc_start_time; | |
625 | - if ( dif <= 0 ) | |
626 | - goto expire; | |
627 | - timer_flag = 1; | |
628 | - timer.start(ifThis,dif,TSE_TIMER); | |
629 | - break; | |
630 | - } | |
631 | - if ( timer.is_expire(ifThis) ) { | |
632 | - INTEGER64 p; | |
633 | - | |
634 | - expire: | |
635 | - acc_max_thread_nos(0); | |
636 | - p = total_run_time * 10 / | |
637 | - ((now - acc_start_time) ); | |
638 | -//::printf("MAX THREAD NOS-1 %lli %lli %lli\n",p,total_run_time,now - acc_start_time); | |
639 | - p = (p + 32)/10; | |
640 | - if ( p < max_thread_nos && max_thread_nos > 1 ) { | |
641 | - if ( now - last_dec_time >= | |
642 | - DEC_TIMER*1000*1000 ) { | |
643 | - max_thread_nos --; | |
644 | - last_dec_time = now; | |
645 | - } | |
646 | - } | |
647 | - else if ( p > max_thread_nos ) { | |
648 | - max_thread_nos ++; | |
649 | - launch_flag = 1; | |
650 | - last_dec_time = now; | |
651 | - } | |
652 | - else | |
653 | - last_dec_time = now; | |
654 | -//::printf("MAX THREAD NOS-2 %lli %i\n",p,max_thread_nos); | |
655 | - acc_start_time = now; | |
656 | - total_run_time = 0; | |
657 | - timer_flag = 1; | |
658 | - timer.start(ifThis,ACC_TIMER*1000*1000,TSE_TIMER); | |
659 | - break; | |
660 | - } | |
661 | - break; | |
245 | + else { | |
246 | + thr_state = THR_STATE_RUN; | |
247 | + pthread_cond_signal(&private_cond); | |
662 | 248 | } |
663 | - if ( launch_flag ) | |
664 | - launch(); | |
665 | - unlock(); | |
249 | + root->unlock(); | |
666 | 250 | } |
667 | 251 | |
668 | -void * | |
669 | -tsThread_::__tsThread_body(void * arg) | |
670 | -{ | |
671 | -tsThread * target; | |
672 | -pthread_t self_id; | |
673 | - | |
674 | - self_id = pthread_self(); | |
675 | - lock(); | |
676 | - _do_setup(); | |
677 | - for ( ; ; ) { | |
678 | - target = ready->del(); | |
679 | - if ( target == 0 ) { | |
680 | - if ( (max_thread_nos > 0 && | |
681 | - current_thread_nos > max_thread_nos) || | |
682 | - thread_stop || | |
683 | - cond_timed_wait() < 0 ) { | |
684 | - target = ready->del(); | |
685 | - if ( target ) | |
686 | - goto exec; | |
687 | - _do_cleanup(); | |
688 | - current_thread_nos --; | |
689 | - if ( current_thread_nos == 0 ) | |
690 | - cond_signal(); | |
691 | - unlock(); | |
692 | - return 0; | |
693 | - } | |
694 | - continue; | |
695 | - } | |
696 | - exec: | |
697 | - acc_max_thread_nos(1); | |
698 | -//::printf("TH ST %i %i %i %i\n",ready->count,max_thread_nos,current_thread_nos,run_thread_nos); | |
699 | - target->thr_set_kill_state(&self_id); | |
700 | - target->__tsThread_body_private(); | |
701 | - target->thr_set_kill_state(); | |
702 | -//::printf("TH ED %i %i %i %i\n",ready->count,max_thread_nos,current_thread_nos,run_thread_nos); | |
703 | - call_main->ins(target); | |
704 | - acc_max_thread_nos(-1); | |
705 | - _push_pipe(); | |
706 | - } | |
707 | - _do_cleanup(); | |
708 | - unlock(); | |
709 | - return 0; | |
710 | -} | |
711 | - | |
712 | 252 | void |
713 | -tsThread_::_push_pipe() | |
714 | -{ | |
715 | -char push; | |
716 | -int ret; | |
717 | - push = 0; | |
718 | - if ( pipe_count <= 0 ) { | |
719 | - for ( ; ; ) { | |
720 | - ret = ::write(pipe_fd[W],&push,1); | |
721 | - if ( ret < 0 ) { | |
722 | - if ( errno == EINTR ) | |
723 | - continue; | |
724 | - perror("THREAD WRITE PIPE ERROR"); | |
725 | - stdObject::panic("cannot write 1 byte\n"); | |
726 | - } | |
727 | - if ( ret == 0 ) { | |
728 | - stdObject::panic("THREAD WRITE END PIPE ?\n"); | |
729 | - } | |
730 | - break; | |
731 | - } | |
732 | - pipe_count ++; | |
733 | - } | |
734 | -} | |
735 | - | |
736 | -void | |
737 | 253 | tsThread_::__tsThread_body_private() |
738 | 254 | { |
739 | 255 | TS_STATE_TYPE state; |
740 | - thr_state = THR_STATE_RUN; | |
256 | +// thr_state = THR_STATE_RUN; | |
741 | 257 | for ( ; ; ) { |
742 | - unlock(); | |
258 | + root->unlock(); | |
743 | 259 | state = parent->do_thread(thread_func,initial_event); |
744 | - lock(); | |
260 | + root->lock(); | |
745 | 261 | switch ( state ) { |
746 | 262 | case 0: |
747 | 263 | terminate_state = pos_state; |
@@ -761,7 +277,7 @@ | ||
761 | 277 | tsThread_::kill(int sig) |
762 | 278 | { |
763 | 279 | int ret; |
764 | - lock(); | |
280 | + root->lock(); | |
765 | 281 | if ( current_pthread ) |
766 | 282 | pthread_kill(*current_pthread,sig); |
767 | 283 | ret = kill_flag; |
@@ -777,7 +293,7 @@ | ||
777 | 293 | case KF_CATCH_REQUEST: |
778 | 294 | break; |
779 | 295 | } |
780 | - unlock(); | |
296 | + root->unlock(); | |
781 | 297 | return ret; |
782 | 298 | } |
783 | 299 |
@@ -799,7 +315,7 @@ | ||
799 | 315 | { |
800 | 316 | int ret; |
801 | 317 | ret = kill_flag; |
802 | - lock(); | |
318 | + root->lock(); | |
803 | 319 | switch ( kill_flag ) { |
804 | 320 | case KF_IDLE: |
805 | 321 | kill_flag = KF_CATCH; |
@@ -811,13 +327,13 @@ | ||
811 | 327 | case KF_CATCH_REQUEST: |
812 | 328 | break; |
813 | 329 | } |
814 | - unlock(); | |
815 | - return 0; | |
330 | + root->unlock(); | |
331 | + return ret; | |
816 | 332 | } |
817 | 333 | void |
818 | 334 | tsThread_::kill_finish() |
819 | 335 | { |
820 | - lock(); | |
336 | + root->lock(); | |
821 | 337 | switch ( kill_flag ) { |
822 | 338 | case KF_IDLE: |
823 | 339 | case KF_REQUEST: |
@@ -829,116 +345,76 @@ | ||
829 | 345 | kill_flag = KF_REQUEST; |
830 | 346 | break; |
831 | 347 | } |
832 | - unlock(); | |
348 | + root->unlock(); | |
833 | 349 | } |
834 | 350 | |
351 | + | |
835 | 352 | /******************************************* |
836 | 353 | STATE MACHINE |
837 | 354 | ********************************************/ |
838 | 355 | |
839 | - | |
840 | -TS_STATE(INI_TINYSTATE_FINISH) | |
356 | +TS_STATE(INI_START) | |
841 | 357 | { |
842 | - REF_SET(*this_p,ifThis); | |
843 | - REF_SET(io,dynamic_cast<fwIO*>(application->getFrameWork())); | |
844 | - lock(); | |
845 | - ready->ins(ifThis); | |
846 | - launch(); | |
847 | - unlock(); | |
848 | - | |
849 | 358 | return rDO|ACT_START; |
850 | 359 | } |
851 | -TS_STATE(ACT_TINYSTATE_START) | |
360 | +TS_STATE(ACT_START) | |
852 | 361 | { |
853 | - if ( host_thread == 0 ) { | |
854 | - REF_SET(host_thread,ifThis); | |
855 | - return rDO|ACT_HOST; | |
856 | - } | |
857 | - if ( host_thread == ifThis ) | |
858 | - return rDO|ACT_HOST; | |
859 | - return ACT_START; | |
362 | + root->ins(ifThis); | |
363 | + return rDO|ACT_TOP_WAIT; | |
860 | 364 | } |
861 | -TS_STATE(ACT_HOST) | |
365 | +TS_STATE(ACT_TOP_WAIT) | |
862 | 366 | { |
863 | - io->read(ifThis,pipe_fd[R]); | |
864 | - inc_max_thread_nos(); | |
865 | - return ACT_HOST_RET; | |
367 | + if ( is_destroyed() ) | |
368 | + return rDO|FIN_START; | |
369 | + if ( thr_state != THR_STATE_INIT ) | |
370 | + return rDO|ACT_FINISH_WAIT; | |
371 | + if ( root->get_readyTop() != ifThis ) | |
372 | + return 0; | |
373 | + timer.start(ifThis,ACC_TIMER*1000*1000); | |
374 | + return rDO|ACT_RUN_WAIT; | |
866 | 375 | } |
867 | -TS_STATE(ACT_HOST_RET) | |
376 | +TS_STATE(ACT_RUN_WAIT) | |
868 | 377 | { |
869 | -tsThread * target; | |
870 | -int er; | |
871 | - inc_max_thread_nos(); | |
872 | - if ( ev->type != TSE_RETURN ) | |
873 | - return 0; | |
874 | - lock(); | |
875 | - for ( ; (er = ::read(pipe_fd[R],buffer,PIPE_SIZE)) < 0 ; ) { | |
876 | - if ( errno == EINTR ) | |
877 | - continue; | |
878 | - perror("THREAD READ PIPE ERROR !!"); | |
879 | - stdObject::panic("THRAED READ PIPE ERROR !!"); | |
378 | + if ( thr_state != THR_STATE_INIT ) | |
379 | + return rDO|ACT_FINISH_WAIT; | |
380 | + if ( root->get_readyTop() != ifThis ) | |
381 | + return rDO|ACT_TOP_WAIT; | |
382 | + if ( timer.is_expire(ifThis) ) { | |
383 | + root->create(); | |
384 | + return rDO|ACT_FINISH_WAIT; | |
880 | 385 | } |
881 | - pipe_count -= er; | |
882 | - unlock(); | |
883 | - for ( ; ; ) { | |
884 | - lock(); | |
885 | - target = call_main->del(); | |
886 | - unlock(); | |
887 | - if ( target == 0 ) | |
888 | - break; | |
889 | - target->thr_throw_invoke(); | |
890 | - switch ( target->thr_state ) { | |
891 | - case THR_STATE_CALL_FUNC: | |
892 | - base_call_func(target,0); | |
893 | - break; | |
894 | - case THR_STATE_TERMINATE: | |
895 | - target->destroy(); | |
896 | - break; | |
897 | - } | |
898 | - } | |
899 | - return rDO|ACT_START; | |
386 | + return 0; | |
900 | 387 | } |
388 | +TS_STATE(ACT_FINISH_WAIT) | |
389 | +{ | |
390 | + if ( is_destroyed() ) | |
391 | + return rDO|FIN_START; | |
392 | + return 0; | |
393 | +} | |
901 | 394 | |
902 | 395 | TS_STATE(FIN_START) |
903 | 396 | { |
397 | + root->del(ifThis); | |
398 | + return rDO|FIN_WAIT; | |
399 | +} | |
400 | +TS_STATE(FIN_WAIT) | |
401 | +{ | |
402 | + root->lock(); | |
403 | + if ( thr_inQue ) { | |
404 | + root->unlock(); | |
405 | + return 0; | |
406 | + } | |
407 | + root->unlock(); | |
408 | + | |
904 | 409 | parent->eventHandler( |
905 | 410 | NEW stdEvent(TSE_THREAD,ifThis, |
906 | 411 | (INTEGER64)terminate_state)); |
907 | - if ( thread_head == ifThis ) { | |
908 | - if ( thread_head == thread_next ) { | |
909 | - REF_SET(thread_head,0); | |
910 | - REF_SET(thread_next,0); | |
911 | - REF_SET(thread_prev,0); | |
912 | - return rDO|FIN_FREE_MUTEX; | |
913 | - } | |
914 | - REF_SET(thread_head,thread_next); | |
915 | - } | |
916 | - REF_SET(thread_prev->thread_next,thread_next); | |
917 | - REF_SET(thread_next->thread_prev,thread_prev); | |
918 | - REF_SET(thread_next,0); | |
919 | - REF_SET(thread_prev,0); | |
920 | 412 | |
921 | - REF_SET(io,0); | |
922 | - return rDO|FIN_FREE_MUTEX; | |
923 | -} | |
924 | -TS_STATE(FIN_FREE_MUTEX) | |
925 | -{ | |
926 | 413 | REF_SET(initial_event,0); |
927 | 414 | if ( private_cond_init ) |
928 | 415 | pthread_cond_destroy(&private_cond); |
929 | 416 | |
930 | - if ( host_thread == 0 || host_thread == ifThis) { | |
931 | - if ( thread_head ) { | |
932 | - REF_SET(host_thread,thread_head); | |
933 | - host_thread->wakeup(); | |
934 | - } | |
935 | - else { | |
936 | - REF_SET(host_thread,0); | |
937 | - } | |
938 | - } | |
939 | - current_tsThread_nos --; | |
940 | - if ( current_tsThread_nos == 0 ) | |
941 | - application->wakeup(); | |
417 | + REF_SET(root,0); | |
942 | 418 | return rDO|FIN_TINYSTATE_START; |
943 | 419 | } |
944 | 420 |
@@ -175,6 +175,12 @@ | ||
175 | 175 | wait_count ++; |
176 | 176 | pthread_cond_wait(&cond,&mu); |
177 | 177 | } |
178 | +#ifdef AQ_DEBUG_MODE | |
179 | + if ( ix->ary[tail%ix->length()] == 0 && | |
180 | + debug_list->ary[tail%a->length()] ) { | |
181 | + stdObject::panic("invalid debug_list"); | |
182 | + } | |
183 | +#endif | |
178 | 184 | tail ++; |
179 | 185 | return 0; |
180 | 186 | } |
@@ -214,6 +220,7 @@ | ||
214 | 220 | return 0; |
215 | 221 | } |
216 | 222 | int detach(unsigned int p,sAQindex<__TYPE> * org) { |
223 | + int ins_flag = 0; | |
217 | 224 | if ( head < tail ) { |
218 | 225 | if ( head <= p && p < tail ) |
219 | 226 | return AQE_ERROR; |
@@ -224,6 +231,7 @@ | ||
224 | 231 | return AQE_ERROR; |
225 | 232 | if ( ix->ary[p%ix->length()] == AQ_INS ) { |
226 | 233 | ix->ary[p%ix->length()] = 0; |
234 | + ins_flag = 1; | |
227 | 235 | } |
228 | 236 | else { |
229 | 237 | ix->ary[p%ix->length()] --; |
@@ -239,7 +247,12 @@ | ||
239 | 247 | pp = &(*pp)->debug_next); |
240 | 248 | if ( *pp ) |
241 | 249 | *pp = org->debug_next; |
242 | - | |
250 | + else if ( ins_flag == 0 ) | |
251 | + stdObject::panic("no object that detach"); | |
252 | + if ( ix->ary[p%ix->length()] == 0 && | |
253 | + debug_list->ary[p%a->length()] ) { | |
254 | + stdObject::panic("invalid debug_list"); | |
255 | + } | |
243 | 256 | #endif |
244 | 257 | return 0; |
245 | 258 | } |
@@ -274,16 +287,38 @@ | ||
274 | 287 | sAQindex() { |
275 | 288 | pos = 0; |
276 | 289 | ptr = 0; |
290 | + a = 0; | |
291 | + proc_flag = 0; | |
292 | +#ifdef AQ_DEBUG_MODE | |
293 | + debug_next = 0; | |
294 | + _line = 0; | |
295 | + _file = 0; | |
296 | + _stamp = this; | |
297 | +#endif | |
277 | 298 | } |
278 | 299 | sAQindex(sPtr<stdArrayQueue<__TYPE> > aa) { |
279 | 300 | pos = 0; |
280 | 301 | ptr = 0; |
281 | 302 | a = aa; |
303 | + proc_flag = 0; | |
304 | +#ifdef AQ_DEBUG_MODE | |
305 | + debug_next = 0; | |
306 | + _line = 0; | |
307 | + _file = 0; | |
308 | + _stamp = this; | |
309 | +#endif | |
282 | 310 | } |
283 | 311 | sAQindex(const sAQindex & inp) { |
284 | 312 | a = inp.a; |
285 | 313 | pos = 0; |
286 | 314 | ptr = 0; |
315 | + proc_flag = 0; | |
316 | +#ifdef AQ_DEBUG_MODE | |
317 | + debug_next = 0; | |
318 | + _line = 0; | |
319 | + _file = 0; | |
320 | + _stamp = this; | |
321 | +#endif | |
287 | 322 | if ( a.is_clear() ) { |
288 | 323 | pos = inp.pos; |
289 | 324 | return; |
@@ -299,6 +334,10 @@ | ||
299 | 334 | a->lock(); |
300 | 335 | detach(); |
301 | 336 | a->unlock(); |
337 | +#ifdef AQ_DEBUG_MODE | |
338 | + if ( _stamp != this ) | |
339 | + stdObject::panic("not equal stamp"); | |
340 | +#endif | |
302 | 341 | } |
303 | 342 | int status() { |
304 | 343 | if ( a.is_clear() ) |
@@ -398,6 +437,9 @@ | ||
398 | 437 | detach(); |
399 | 438 | pos = ret; |
400 | 439 | ptr = a->get(pos); |
440 | + if ( proc_flag ) | |
441 | + stdObject::panic("proc_flag ins"); | |
442 | + proc_flag = 1; | |
401 | 443 | return 0; |
402 | 444 | } |
403 | 445 | int del() { |
@@ -425,12 +467,65 @@ | ||
425 | 467 | return a->pop(); |
426 | 468 | } |
427 | 469 | sAQindex& operator=(sAQindex inp) { |
428 | - if ( a ) { | |
470 | +sPtr<stdArrayQueue<__TYPE> > prev_a; | |
471 | +int prev_pos; | |
472 | +__TYPE * prev_ptr; | |
473 | +prev_a = a; | |
474 | +prev_pos = pos; | |
475 | +prev_ptr = ptr; | |
476 | +int prev_tt = 0; | |
477 | + | |
478 | + if ( a.is_clear() ) { | |
479 | +if ( ptr ) | |
480 | +stdObject::panic("???1"); | |
481 | + if ( inp.a.is_clear() ) { | |
482 | + pos = inp.pos; | |
483 | + } | |
484 | + else { | |
485 | + a = inp.a; | |
486 | + a->lock(); | |
487 | + pos = inp.pos; | |
488 | + attach(); | |
489 | + a->unlock(); | |
490 | + } | |
491 | + } | |
492 | + else { | |
493 | + if ( inp.a.is_clear() ) { | |
494 | + a->lock(); | |
495 | + detach(); | |
496 | + a->unlock(); | |
497 | + pos = inp.pos; | |
498 | + } | |
499 | + else { | |
500 | + if ( a == inp.a ) { | |
501 | + a->lock(); | |
502 | + detach(); | |
503 | + pos = inp.pos; | |
504 | + attach(); | |
505 | + a->unlock(); | |
506 | + } | |
507 | + else { | |
508 | + a->lock(); | |
509 | + inp.a->lock(); | |
510 | + detach(); | |
511 | + a->unlock(); | |
512 | + | |
513 | + a = inp.a; | |
514 | + pos = inp.pos; | |
515 | + attach(); | |
516 | + a->unlock(); | |
517 | + } | |
518 | + } | |
519 | + } | |
520 | + return *this; | |
521 | + | |
522 | + if ( !a.is_clear() ) { | |
523 | +prev_tt = 1; | |
429 | 524 | a->lock(); |
430 | 525 | detach(); |
431 | 526 | a->unlock(); |
432 | 527 | } |
433 | - if ( inp.a ) | |
528 | + if ( !inp.a.is_clear() ) | |
434 | 529 | a = inp.a; |
435 | 530 | a->lock(); |
436 | 531 | pos = inp.pos; |
@@ -529,7 +624,9 @@ | ||
529 | 624 | sAQindex<__TYPE> * debug_next; |
530 | 625 | const char * _file; |
531 | 626 | int _line; |
627 | + sAQindex<__TYPE> * _stamp; | |
532 | 628 | #endif |
629 | + unsigned proc_flag:1; | |
533 | 630 | |
534 | 631 | sPtr<stdArrayQueue<__TYPE> > a; |
535 | 632 | int pos; |
@@ -537,11 +634,17 @@ | ||
537 | 634 | |
538 | 635 | int attach() { |
539 | 636 | int ret; |
540 | - if ( ptr ) | |
637 | + if ( ptr ) { | |
638 | + stdObject::panic("double attach"); | |
541 | 639 | return 0; |
640 | + } | |
641 | + if ( this != _stamp ) | |
642 | + stdObject::panic("different stamp"); | |
542 | 643 | ret = a->attach(pos,this); |
543 | - if ( ret == 0 ) | |
644 | + if ( ret == 0 ) { | |
544 | 645 | ptr = a->get(pos); |
646 | + proc_flag = 1; | |
647 | + } | |
545 | 648 | else ptr = 0; |
546 | 649 | return ret; |
547 | 650 | } |
@@ -548,6 +651,9 @@ | ||
548 | 651 | int detach() { |
549 | 652 | if ( ptr == 0 ) |
550 | 653 | return 0; |
654 | + if ( proc_flag == 0 ) | |
655 | + stdObject::panic("no attach detach"); | |
656 | + proc_flag = 0; | |
551 | 657 | a->detach(pos,this); |
552 | 658 | ptr = 0; |
553 | 659 | return 0; |
@@ -79,7 +79,8 @@ | ||
79 | 79 | #define TSE_THREAD 17 |
80 | 80 | #define TSE_PACKET 18 |
81 | 81 | #define TSE_ACCESS 19 |
82 | -#define TSE_MAX 20 | |
82 | +#define TSE_PRIORITY 20 | |
83 | +#define TSE_MAX 21 | |
83 | 84 | |
84 | 85 | #define TSE_EVM(x) (1<<(x)) |
85 | 86 |
@@ -33,6 +33,11 @@ | ||
33 | 33 | stdHalfOrderNode * __top() { |
34 | 34 | return top; |
35 | 35 | }; |
36 | + stdObject * getTop() { | |
37 | + if ( top ) | |
38 | + return top->data; | |
39 | + return 0; | |
40 | + } | |
36 | 41 | int count(); |
37 | 42 | protected: |
38 | 43 | static void _ins(stdHalfOrderNode *,int key,stdObject * data); |
@@ -9,8 +9,11 @@ | ||
9 | 9 | class stdHalfOrderQueueTS : public stdHalfOrderQueue { |
10 | 10 | public: |
11 | 11 | stdHalfOrderQueueTS(); |
12 | - stdHalfOrderQueueTS(stdHalfOrderQueueTS * q); | |
13 | - tinyState * del(); | |
12 | + stdHalfOrderQueueTS(stdHalfOrderQueueTS * q,tinyState * caller); | |
13 | + tinyState * del(tinyState * caller); | |
14 | + tinyState * getTop() { | |
15 | + return dynamic_cast<tinyState*>(stdHalfOrderQueue::getTop()); | |
16 | + } | |
14 | 17 | }; |
15 | 18 | |
16 | 19 | #endif |
@@ -46,6 +46,7 @@ | ||
46 | 46 | stdQueueElement<__TYPE> * tail; |
47 | 47 | |
48 | 48 | int count; |
49 | + unsigned insNeq:1; | |
49 | 50 | |
50 | 51 | |
51 | 52 |
@@ -80,8 +81,14 @@ | ||
80 | 81 | else { |
81 | 82 | elp = &head; |
82 | 83 | for ( ; *elp ; elp = &(*elp)->next ) { |
83 | - if ( (*elp)->key >= key ) | |
84 | - break; | |
84 | + if ( insNeq ) { | |
85 | + if ( (*elp)->key > key ) | |
86 | + break; | |
87 | + } | |
88 | + else { | |
89 | + if ( (*elp)->key >= key ) | |
90 | + break; | |
91 | + } | |
85 | 92 | } |
86 | 93 | REF_SET(el->next,*elp); |
87 | 94 | REF_SET(*elp,el); |
@@ -362,6 +369,7 @@ | ||
362 | 369 | { |
363 | 370 | this->head = this->tail = 0; |
364 | 371 | this->count = 0; |
372 | + this->insNeq = q->insNeq; | |
365 | 373 | q->check(this,copy_stdQueue); |
366 | 374 | } |
367 | 375 |
@@ -383,8 +391,10 @@ | ||
383 | 391 | { |
384 | 392 | this->head = this->tail = 0; |
385 | 393 | this->count = 0; |
394 | + this->insNeq = 0; | |
386 | 395 | if ( q == 0 ) |
387 | 396 | return; |
397 | + this->insNeq = q->insNeq; | |
388 | 398 | q->check(this,copy_stdQueue); |
389 | 399 | } |
390 | 400 |
@@ -7,6 +7,7 @@ | ||
7 | 7 | #include "ts/c++/stdString.h" |
8 | 8 | #include "ts/c++/tsInsensitive.h" |
9 | 9 | #include "ts/c++/tsGC.h" |
10 | +#include "ts/c++/tsThreadRoot.h" | |
10 | 11 | |
11 | 12 | class tsApplication; |
12 | 13 | class tsApplication_; |
@@ -5,21 +5,13 @@ | ||
5 | 5 | |
6 | 6 | #include <pthread.h> |
7 | 7 | #include <signal.h> |
8 | +#include "ts/c++/tsThreadRoot.h" | |
8 | 9 | |
10 | +class tsThreadRoot; | |
9 | 11 | class tinyState; |
10 | 12 | typedef TS_STATE_TYPE (*THR_FUNC)(tinyState * THIS,void*msg); |
11 | 13 | |
12 | 14 | |
13 | -class stdThread : public stdObject { | |
14 | -public: | |
15 | - stdThread(stdObject * parent); | |
16 | - ~stdThread(); | |
17 | - virtual void setup(); | |
18 | - virtual void cleanup(); | |
19 | -private: | |
20 | - stdObject * parent; | |
21 | -}; | |
22 | - | |
23 | 15 | #include "ts/c++/tinyState.h" |
24 | 16 | #include "_ts/c++/tsThread_pb.h" |
25 | 17 |
@@ -35,7 +27,8 @@ | ||
35 | 27 | inline TS_STATE_TYPE FORCEINLINE \ |
36 | 28 | TS_THISCLASS::_state_##thrState(stdEvent*ev) \ |
37 | 29 | { \ |
38 | - NEW tsThread(ifThis,&thread,thrState,thrState##__thread,ev,thread_attr); \ | |
30 | + NEW tsThread(ifThis,&thread,application->getThreadRoot(), \ | |
31 | + thrState,thrState##__thread,ev,thread_attr); \ | |
39 | 32 | return thrState##__thread_wait; \ |
40 | 33 | } \ |
41 | 34 | TS_STATE_TYPE \ |