diff --git a/include/mycore/mythread.h b/include/mycore/mythread.h index 9dbbd3f..65096ec 100644 --- a/include/mycore/mythread.h +++ b/include/mycore/mythread.h @@ -109,7 +109,7 @@ mythread_id_t myhread_increase_id_by_entry_id(mythread_t* mythread, mythread_id_ mystatus_t mythread_join(mythread_t *mythread, mythread_callback_before_entry_join_f before_join, void* ctx); mystatus_t mythread_quit(mythread_t *mythread, mythread_callback_before_entry_join_f before_join, void* ctx); mystatus_t mythread_stop(mythread_t *mythread); -mystatus_t mythread_resume(mythread_t *mythread); +mystatus_t mythread_resume(mythread_t *mythread, mythread_thread_opt_t send_opt); mystatus_t mythread_suspend(mythread_t *mythread); mystatus_t mythread_check_status(mythread_t *mythread); @@ -122,7 +122,7 @@ mystatus_t myhread_entry_create(mythread_t *mythread, mythread_process_f process mystatus_t mythread_entry_join(mythread_entry_t* entry, mythread_callback_before_entry_join_f before_join, void* ctx); mystatus_t mythread_entry_quit(mythread_entry_t* entry, mythread_callback_before_entry_join_f before_join, void* ctx); mystatus_t mythread_entry_stop(mythread_entry_t* entry); -mystatus_t mythread_entry_resume(mythread_entry_t* entry); +mystatus_t mythread_entry_resume(mythread_entry_t* entry, mythread_thread_opt_t send_opt); mystatus_t mythread_entry_suspend(mythread_entry_t* entry); mystatus_t mythread_entry_status(mythread_entry_t* entry); mythread_t * mythread_entry_mythread(mythread_entry_t* entry); diff --git a/source/modest/finder/thread.c b/source/modest/finder/thread.c index bd7e7c7..fa24bc8 100644 --- a/source/modest/finder/thread.c +++ b/source/modest/finder/thread.c @@ -97,15 +97,15 @@ modest_finder_thread_t * modest_finder_thread_destroy(modest_finder_thread_t* fi if(finder_thread == NULL) return NULL; - finder_thread->entry_obj = mcobject_async_destroy(finder_thread->entry_obj, true); - finder_thread->declaration_obj = mcobject_async_destroy(finder_thread->declaration_obj, true); - #ifndef MyCORE_BUILD_WITHOUT_THREADS if(finder_thread->thread) { finder_thread->thread = mythread_destroy(finder_thread->thread, mythread_callback_quit, NULL, true); } #endif + finder_thread->entry_obj = mcobject_async_destroy(finder_thread->entry_obj, true); + finder_thread->declaration_obj = mcobject_async_destroy(finder_thread->declaration_obj, true); + if(finder_thread->context_list) { mycore_free(finder_thread->context_list); @@ -143,7 +143,7 @@ mystatus_t modest_finder_thread_process(modest_t* modest, modest_finder_thread_t if(finder_thread->finder == NULL) return MODEST_STATUS_ERROR; - mythread_resume(finder_thread->thread); + mythread_resume(finder_thread->thread, MyTHREAD_OPT_UNDEF); modest_finder_thread_wait_for_all_done(finder_thread); /* calc result */ @@ -399,8 +399,6 @@ void modest_finder_thread_stream(mythread_id_t thread_id, void* arg) selector_list = selector_list->next; } - - ctx->opt = MyTHREAD_OPT_STOP|MyTHREAD_OPT_DONE; } #endif diff --git a/source/mycore/mythread.c b/source/mycore/mythread.c index d75b8ac..822fc08 100644 --- a/source/mycore/mythread.c +++ b/source/mycore/mythread.c @@ -64,7 +64,7 @@ mythread_t * mythread_destroy(mythread_t *mythread, mythread_callback_before_ent return NULL; if(mythread->entries) { - mythread_resume(mythread); + mythread_resume(mythread, MyTHREAD_OPT_QUIT); mythread_quit(mythread, before_join, ctx); mycore_free(mythread->entries); } @@ -194,19 +194,19 @@ mystatus_t mythread_suspend(mythread_t *mythread) return MyCORE_STATUS_OK; } -mystatus_t mythread_resume(mythread_t *mythread) +mystatus_t mythread_resume(mythread_t *mythread, mythread_thread_opt_t send_opt) { if(mythread->opt & MyTHREAD_OPT_WAIT) { - mythread_option_set(mythread, MyTHREAD_OPT_UNDEF); + mythread_option_set(mythread, send_opt); return MyCORE_STATUS_OK; } - mythread_option_set(mythread, MyTHREAD_OPT_UNDEF); + mythread_option_set(mythread, send_opt); for (size_t i = 0; i < mythread->entries_length; i++) { if(mythread->entries[i].context.opt & MyTHREAD_OPT_STOP) { - mythread->entries[i].context.opt = MyTHREAD_OPT_UNDEF; + mythread->entries[i].context.opt = send_opt; if(mythread_mutex_post(mythread, mythread->entries[i].context.mutex)) return MyCORE_STATUS_ERROR; @@ -293,19 +293,19 @@ mystatus_t mythread_entry_suspend(mythread_entry_t* entry) return MyCORE_STATUS_OK; } -mystatus_t mythread_entry_resume(mythread_entry_t* entry) +mystatus_t mythread_entry_resume(mythread_entry_t* entry, mythread_thread_opt_t send_opt) { if(entry->context.opt & MyTHREAD_OPT_WAIT) { - entry->context.opt = MyTHREAD_OPT_UNDEF; + entry->context.opt = send_opt; } else if(entry->context.opt & MyTHREAD_OPT_STOP) { - entry->context.opt = MyTHREAD_OPT_UNDEF; + entry->context.opt = send_opt; if(mythread_mutex_post(entry->context.mythread, entry->context.mutex)) return MyCORE_STATUS_ERROR; } else - entry->context.opt = MyTHREAD_OPT_UNDEF; + entry->context.opt = send_opt; return MyCORE_STATUS_OK; } diff --git a/source/mycore/mythread.h b/source/mycore/mythread.h index 9849824..5142c25 100644 --- a/source/mycore/mythread.h +++ b/source/mycore/mythread.h @@ -109,7 +109,7 @@ mythread_id_t myhread_increase_id_by_entry_id(mythread_t* mythread, mythread_id_ mystatus_t mythread_join(mythread_t *mythread, mythread_callback_before_entry_join_f before_join, void* ctx); mystatus_t mythread_quit(mythread_t *mythread, mythread_callback_before_entry_join_f before_join, void* ctx); mystatus_t mythread_stop(mythread_t *mythread); -mystatus_t mythread_resume(mythread_t *mythread); +mystatus_t mythread_resume(mythread_t *mythread, mythread_thread_opt_t send_opt); mystatus_t mythread_suspend(mythread_t *mythread); mystatus_t mythread_check_status(mythread_t *mythread); @@ -122,7 +122,7 @@ mystatus_t myhread_entry_create(mythread_t *mythread, mythread_process_f process mystatus_t mythread_entry_join(mythread_entry_t* entry, mythread_callback_before_entry_join_f before_join, void* ctx); mystatus_t mythread_entry_quit(mythread_entry_t* entry, mythread_callback_before_entry_join_f before_join, void* ctx); mystatus_t mythread_entry_stop(mythread_entry_t* entry); -mystatus_t mythread_entry_resume(mythread_entry_t* entry); +mystatus_t mythread_entry_resume(mythread_entry_t* entry, mythread_thread_opt_t send_opt); mystatus_t mythread_entry_suspend(mythread_entry_t* entry); mystatus_t mythread_entry_status(mythread_entry_t* entry); mythread_t * mythread_entry_mythread(mythread_entry_t* entry); diff --git a/source/mycore/thread_queue.c b/source/mycore/thread_queue.c index 36b6829..4532c6b 100644 --- a/source/mycore/thread_queue.c +++ b/source/mycore/thread_queue.c @@ -375,7 +375,7 @@ mythread_queue_list_entry_t * mythread_queue_list_entry_push(mythread_t** mythre for(size_t i = 0; i < list_size; i++) if(mythread_list[i]) - mythread_resume(mythread_list[i]); + mythread_resume(mythread_list[i], MyTHREAD_OPT_UNDEF); return entry; } @@ -405,7 +405,7 @@ mythread_queue_list_entry_t * mythread_queue_list_entry_delete(mythread_t** myth for(size_t i = 0; i < list_size; i++) if(mythread_list[i]) - mythread_resume(mythread_list[i]); + mythread_resume(mythread_list[i], MyTHREAD_OPT_UNDEF); if(destroy_queue && entry->queue) mythread_queue_destroy(entry->queue); @@ -621,32 +621,30 @@ void * mythread_function(void *arg) mythread_mutex_wait(mythread, ctx->mutex); do { - if(mythread->opt & MyTHREAD_OPT_STOP || ctx->opt & MyTHREAD_OPT_STOP) - { - ctx->opt = MyTHREAD_OPT_DONE|MyTHREAD_OPT_STOP; - mythread_mutex_wait(mythread, ctx->mutex); - - if(mythread->opt & MyTHREAD_OPT_QUIT || ctx->opt & MyTHREAD_OPT_QUIT) - { - mythread_mutex_close(mythread, ctx->mutex); - mythread_nanosleep_destroy(ctx->timespec); - - ctx->opt = MyTHREAD_OPT_DONE|MyTHREAD_OPT_QUIT; - break; + ctx->func(ctx->id, ctx); + + ctx->opt |= MyTHREAD_OPT_DONE; + + if(ctx->opt & MyTHREAD_OPT_WAIT) { + while (ctx->opt & MyTHREAD_OPT_WAIT) { + mythread_nanosleep_sleep(ctx->timespec); } - - ctx->opt = MyTHREAD_OPT_UNDEF; } - else if(mythread->opt & MyTHREAD_OPT_QUIT || ctx->opt & MyTHREAD_OPT_QUIT) + else { + ctx->opt |= MyTHREAD_OPT_STOP; + mythread_mutex_wait(mythread, ctx->mutex); + } + + if(mythread->opt & MyTHREAD_OPT_QUIT || ctx->opt & MyTHREAD_OPT_QUIT) { mythread_mutex_close(mythread, ctx->mutex); mythread_nanosleep_destroy(ctx->timespec); - ctx->opt = MyTHREAD_OPT_DONE|MyTHREAD_OPT_QUIT; + ctx->opt = MyTHREAD_OPT_QUIT; break; } - ctx->func(ctx->id, ctx); + ctx->opt = MyTHREAD_OPT_UNDEF; } while(1); diff --git a/source/myhtml/tokenizer.c b/source/myhtml/tokenizer.c index d05402f..a89750b 100644 --- a/source/myhtml/tokenizer.c +++ b/source/myhtml/tokenizer.c @@ -253,10 +253,10 @@ void myhtml_tokenizer_post(myhtml_tree_t* tree) { #ifndef MyCORE_BUILD_WITHOUT_THREADS if(tree->myhtml->thread_stream) - mythread_resume(tree->myhtml->thread_stream); + mythread_resume(tree->myhtml->thread_stream, MyTHREAD_OPT_UNDEF); if(tree->myhtml->thread_batch) - mythread_resume(tree->myhtml->thread_batch); + mythread_resume(tree->myhtml->thread_batch, MyTHREAD_OPT_UNDEF); #endif }