diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/luaproc/sched.c | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/libraries/luaproc/sched.c b/libraries/luaproc/sched.c index f19beeb..474c82b 100644 --- a/libraries/luaproc/sched.c +++ b/libraries/luaproc/sched.c | |||
@@ -83,9 +83,11 @@ void *workermain( void *args ) { | |||
83 | /* detach thread so resources are freed as soon as thread exits (no further joining) */ | 83 | /* detach thread so resources are freed as soon as thread exits (no further joining) */ |
84 | pthread_detach( pthread_self( )); | 84 | pthread_detach( pthread_self( )); |
85 | 85 | ||
86 | //printf("NEW WORKER\n"); | ||
86 | /* main worker loop */ | 87 | /* main worker loop */ |
87 | while ( 1 ) { | 88 | while ( 1 ) { |
88 | 89 | ||
90 | //printf("a\n"); | ||
89 | /* get exclusive access to the ready process queue */ | 91 | /* get exclusive access to the ready process queue */ |
90 | pthread_mutex_lock( &mutex_queue_access ); | 92 | pthread_mutex_lock( &mutex_queue_access ); |
91 | 93 | ||
@@ -94,62 +96,83 @@ void *workermain( void *args ) { | |||
94 | pthread_cond_wait( &cond_wakeup_worker, &mutex_queue_access ); | 96 | pthread_cond_wait( &cond_wakeup_worker, &mutex_queue_access ); |
95 | } | 97 | } |
96 | 98 | ||
99 | ////printf("b\n"); | ||
97 | /* pop the first node from the ready process queue */ | 100 | /* pop the first node from the ready process queue */ |
98 | n = list_pop_head( lpready ); | 101 | n = list_pop_head( lpready ); |
99 | 102 | ||
103 | ////printf("c\n"); | ||
100 | /* ensure list pop succeeded before proceeding */ | 104 | /* ensure list pop succeeded before proceeding */ |
101 | if ( n != NULL ) { | 105 | if ( n != NULL ) { |
106 | //printf("c.0\n"); | ||
102 | /* get the popped node's data content (ie, the lua process struct) */ | 107 | /* get the popped node's data content (ie, the lua process struct) */ |
103 | lp = (luaproc )list_data( n ); | 108 | lp = (luaproc )list_data( n ); |
104 | } | 109 | } |
105 | else { | 110 | else { |
111 | ////printf("c.1\n"); | ||
106 | /* free access to the process ready queue */ | 112 | /* free access to the process ready queue */ |
107 | pthread_mutex_unlock( &mutex_queue_access ); | 113 | pthread_mutex_unlock( &mutex_queue_access ); |
108 | /* finished thread */ | 114 | /* finished thread */ |
115 | //printf("c.2 pthread_exit\n"); | ||
109 | pthread_exit( NULL ); | 116 | pthread_exit( NULL ); |
117 | //printf("c.3\n"); | ||
110 | } | 118 | } |
111 | 119 | ||
120 | ////printf("d\n"); | ||
112 | /* free access to the process ready queue */ | 121 | /* free access to the process ready queue */ |
113 | pthread_mutex_unlock( &mutex_queue_access ); | 122 | pthread_mutex_unlock( &mutex_queue_access ); |
114 | 123 | ||
124 | //printf("e lua_resum\n"); | ||
115 | /* execute the lua code specified in the lua process struct */ | 125 | /* execute the lua code specified in the lua process struct */ |
116 | procstat = lua_resume( luaproc_get_state( lp ), luaproc_get_args( lp )); | 126 | procstat = lua_resume( luaproc_get_state( lp ), luaproc_get_args( lp )); |
117 | 127 | ||
128 | //printf("f\n"); | ||
118 | /* reset the process argument count */ | 129 | /* reset the process argument count */ |
119 | luaproc_set_args( lp, 0 ); | 130 | luaproc_set_args( lp, 0 ); |
120 | 131 | ||
132 | ////printf("g\n"); | ||
121 | /* check if process finished its whole execution */ | 133 | /* check if process finished its whole execution */ |
122 | if ( procstat == 0 ) { | 134 | if ( procstat == 0 ) { |
123 | 135 | ||
136 | //printf("g.0\n"); | ||
124 | /* destroy the corresponding list node */ | 137 | /* destroy the corresponding list node */ |
125 | list_destroy_node( n ); | 138 | list_destroy_node( n ); |
126 | 139 | ||
140 | ////printf("g.1\n"); | ||
127 | /* check if worker thread should be destroyed */ | 141 | /* check if worker thread should be destroyed */ |
128 | destroyworker = luaproc_get_destroyworker( lp ); | 142 | destroyworker = luaproc_get_destroyworker( lp ); |
129 | 143 | ||
144 | ////printf("g.2 proc finished\n"); | ||
130 | /* set process status to finished */ | 145 | /* set process status to finished */ |
131 | luaproc_set_status( lp, LUAPROC_STAT_FINISHED ); | 146 | luaproc_set_status( lp, LUAPROC_STAT_FINISHED ); |
132 | 147 | ||
148 | ////printf("g.3\n"); | ||
133 | /* check if lua process should be recycled and, if not, destroy it */ | 149 | /* check if lua process should be recycled and, if not, destroy it */ |
134 | if ( luaproc_recycle_push( lp ) == FALSE ) { | 150 | if ( luaproc_recycle_push( lp ) == FALSE ) { |
151 | //printf("g.3.0 lua_close\n"); | ||
135 | lua_close( luaproc_get_state( lp )); | 152 | lua_close( luaproc_get_state( lp )); |
136 | } | 153 | } |
137 | 154 | ||
155 | ////printf("g.4\n"); | ||
138 | /* decrease active lua process count */ | 156 | /* decrease active lua process count */ |
139 | sched_lpcount_dec(); | 157 | sched_lpcount_dec(); |
140 | 158 | ||
159 | ////printf("g.5\n"); | ||
141 | /* check if thread should be finished after lua process conclusion */ | 160 | /* check if thread should be finished after lua process conclusion */ |
142 | if ( destroyworker ) { | 161 | if ( destroyworker ) { |
162 | //printf("g.5.0 pthread_exit\n"); | ||
143 | /* if so, finish thread */ | 163 | /* if so, finish thread */ |
144 | pthread_exit( NULL ); | 164 | pthread_exit( NULL ); |
145 | } | 165 | } |
166 | //printf("g.6\n"); | ||
146 | } | 167 | } |
147 | 168 | ||
148 | /* check if process yielded */ | 169 | /* check if process yielded */ |
149 | else if ( procstat == LUA_YIELD ) { | 170 | else if ( procstat == LUA_YIELD ) { |
150 | 171 | ||
172 | //printf("??????????????h.0\n"); | ||
151 | /* if so, further check if yield originated from an unmatched send/recv operation */ | 173 | /* if so, further check if yield originated from an unmatched send/recv operation */ |
152 | if ( luaproc_get_status( lp ) == LUAPROC_STAT_BLOCKED_SEND ) { | 174 | if ( luaproc_get_status( lp ) == LUAPROC_STAT_BLOCKED_SEND ) { |
175 | //printf("??????????????h.1\n"); | ||
153 | /* queue blocked lua process on corresponding channel */ | 176 | /* queue blocked lua process on corresponding channel */ |
154 | luaproc_queue_sender( lp ); | 177 | luaproc_queue_sender( lp ); |
155 | /* unlock channel access */ | 178 | /* unlock channel access */ |
@@ -159,6 +182,7 @@ void *workermain( void *args ) { | |||
159 | } | 182 | } |
160 | 183 | ||
161 | else if ( luaproc_get_status( lp ) == LUAPROC_STAT_BLOCKED_RECV ) { | 184 | else if ( luaproc_get_status( lp ) == LUAPROC_STAT_BLOCKED_RECV ) { |
185 | //printf("??????????????h.2\n"); | ||
162 | /* queue blocked lua process on corresponding channel */ | 186 | /* queue blocked lua process on corresponding channel */ |
163 | luaproc_queue_receiver( lp ); | 187 | luaproc_queue_receiver( lp ); |
164 | /* unlock channel access */ | 188 | /* unlock channel access */ |
@@ -169,6 +193,7 @@ void *workermain( void *args ) { | |||
169 | 193 | ||
170 | /* or if yield resulted from an explicit call to coroutine.yield in the lua code being executed */ | 194 | /* or if yield resulted from an explicit call to coroutine.yield in the lua code being executed */ |
171 | else { | 195 | else { |
196 | //printf("??????????????h.3\n"); | ||
172 | /* get exclusive access to the ready process queue */ | 197 | /* get exclusive access to the ready process queue */ |
173 | pthread_mutex_lock( &mutex_queue_access ); | 198 | pthread_mutex_lock( &mutex_queue_access ); |
174 | /* re-insert the job at the end of the ready process queue */ | 199 | /* re-insert the job at the end of the ready process queue */ |
@@ -180,6 +205,7 @@ void *workermain( void *args ) { | |||
180 | 205 | ||
181 | /* check if there was any execution error (LUA_ERRRUN, LUA_ERRSYNTAX, LUA_ERRMEM or LUA_ERRERR) */ | 206 | /* check if there was any execution error (LUA_ERRRUN, LUA_ERRSYNTAX, LUA_ERRMEM or LUA_ERRERR) */ |
182 | else { | 207 | else { |
208 | //printf("??????????????i.0\n"); | ||
183 | /* destroy the corresponding node */ | 209 | /* destroy the corresponding node */ |
184 | list_destroy_node( n ); | 210 | list_destroy_node( n ); |
185 | /* print error message */ | 211 | /* print error message */ |
@@ -189,6 +215,7 @@ void *workermain( void *args ) { | |||
189 | /* decrease active lua process count */ | 215 | /* decrease active lua process count */ |
190 | sched_lpcount_dec(); | 216 | sched_lpcount_dec(); |
191 | } | 217 | } |
218 | //printf("END\n"); | ||
192 | } | 219 | } |
193 | } | 220 | } |
194 | 221 | ||
@@ -260,29 +287,42 @@ int sched_queue_proc( luaproc lp ) { | |||
260 | /* synchronize worker threads */ | 287 | /* synchronize worker threads */ |
261 | void sched_join_workerthreads( void ) { | 288 | void sched_join_workerthreads( void ) { |
262 | 289 | ||
290 | ////printf(" 0\n"); | ||
263 | pthread_mutex_lock( &mutex_lp_count ); | 291 | pthread_mutex_lock( &mutex_lp_count ); |
264 | 292 | ||
293 | //printf(" 1 wait for procs to end\n"); | ||
265 | /* wait until there is no more active lua processes */ | 294 | /* wait until there is no more active lua processes */ |
266 | while( lpcount != 0 ) { | 295 | while( lpcount != 0 ) { |
296 | //printf(" 1.0\n"); | ||
267 | pthread_cond_wait( &cond_no_active_lp, &mutex_lp_count ); | 297 | pthread_cond_wait( &cond_no_active_lp, &mutex_lp_count ); |
268 | } | 298 | } |
269 | /* get exclusive access to the ready process queue */ | 299 | /* get exclusive access to the ready process queue */ |
300 | ////printf(" 2\n"); | ||
270 | pthread_mutex_lock( &mutex_queue_access ); | 301 | pthread_mutex_lock( &mutex_queue_access ); |
271 | /* set the no more active lua processes flag to true */ | 302 | /* set the no more active lua processes flag to true */ |
303 | ////printf(" 3\n"); | ||
272 | no_more_processes = TRUE; | 304 | no_more_processes = TRUE; |
273 | /* wake ALL workers up */ | 305 | /* wake ALL workers up */ |
306 | //printf(" 4 wake up all workers.\n"); | ||
274 | pthread_cond_broadcast( &cond_wakeup_worker ); | 307 | pthread_cond_broadcast( &cond_wakeup_worker ); |
275 | /* free access to the process ready queue */ | 308 | /* free access to the process ready queue */ |
309 | ////printf(" 5\n"); | ||
276 | pthread_mutex_unlock( &mutex_queue_access ); | 310 | pthread_mutex_unlock( &mutex_queue_access ); |
311 | |||
312 | // We don't need this, as we only get here during shutdown. Linking this to EFL results in a hang otherwise anyway. | ||
277 | /* wait for (join) worker threads */ | 313 | /* wait for (join) worker threads */ |
278 | pthread_exit( NULL ); | 314 | //printf(" 6 pthread_exit, waiting for workers to end\n"); |
315 | // pthread_exit( NULL ); | ||
279 | 316 | ||
317 | //printf("7\n"); | ||
280 | pthread_mutex_unlock( &mutex_lp_count ); | 318 | pthread_mutex_unlock( &mutex_lp_count ); |
281 | 319 | ||
320 | //printf("8\n"); | ||
282 | } | 321 | } |
283 | 322 | ||
284 | /* increase active lua process count */ | 323 | /* increase active lua process count */ |
285 | void sched_lpcount_inc( void ) { | 324 | void sched_lpcount_inc( void ) { |
325 | //printf("inc procs++++++++++++++++++++++++++++++++++++++++\n"); | ||
286 | pthread_mutex_lock( &mutex_lp_count ); | 326 | pthread_mutex_lock( &mutex_lp_count ); |
287 | lpcount++; | 327 | lpcount++; |
288 | pthread_mutex_unlock( &mutex_lp_count ); | 328 | pthread_mutex_unlock( &mutex_lp_count ); |
@@ -290,10 +330,12 @@ void sched_lpcount_inc( void ) { | |||
290 | 330 | ||
291 | /* decrease active lua process count */ | 331 | /* decrease active lua process count */ |
292 | void sched_lpcount_dec( void ) { | 332 | void sched_lpcount_dec( void ) { |
333 | //printf("dec procs----------------------------------------\n"); | ||
293 | pthread_mutex_lock( &mutex_lp_count ); | 334 | pthread_mutex_lock( &mutex_lp_count ); |
294 | lpcount--; | 335 | lpcount--; |
295 | /* if count reaches zero, signal there are no more active processes */ | 336 | /* if count reaches zero, signal there are no more active processes */ |
296 | if ( lpcount == 0 ) { | 337 | if ( lpcount == 0 ) { |
338 | //printf("dec procs AND NONE LEFT000000000000000000000000000\n"); | ||
297 | pthread_cond_signal( &cond_no_active_lp ); | 339 | pthread_cond_signal( &cond_no_active_lp ); |
298 | } | 340 | } |
299 | pthread_mutex_unlock( &mutex_lp_count ); | 341 | pthread_mutex_unlock( &mutex_lp_count ); |