| 63 | } |
| 64 | |
| 65 | int thread_base::_thread_run(thread_base* this_thread_obj) { |
| 66 | this_thread_obj->lock(); |
| 67 | core::set_current_thread_name(this_thread_obj->thread_name); |
| 68 | this_thread_obj->unlock(); |
| 69 | |
| 70 | while(true) { |
| 71 | // wait until we get the thread lock |
| 72 | if(this_thread_obj->try_lock()) { |
| 73 | // if the "finish flag" has been set in the mean time, don't call the run method! |
| 74 | if(!this_thread_obj->thread_should_finish()) { |
| 75 | try { |
| 76 | this_thread_obj->run(); |
| 77 | } catch(exception& exc) { |
| 78 | log_error("encountered an unhandled exception while running a thread \"$\": $", |
| 79 | core::get_current_thread_name(), exc.what()); |
| 80 | } catch(...) { |
| 81 | log_error("encountered an unhandled exception while running a thread \"$\"", |
| 82 | core::get_current_thread_name()); |
| 83 | } |
| 84 | } |
| 85 | this_thread_obj->unlock(); |
| 86 | |
| 87 | // again: if the "finish flag" has been set, don't wait, but continue immediately |
| 88 | if(!this_thread_obj->thread_should_finish()) { |
| 89 | // reduce system load and make other locks possible |
| 90 | const size_t thread_delay = this_thread_obj->get_thread_delay(); |
| 91 | if(thread_delay > 0) { |
| 92 | this_thread::sleep_for(chrono::milliseconds(thread_delay)); |
| 93 | } |
| 94 | else { |
| 95 | if(this_thread_obj->get_yield_after_run()) { |
| 96 | // just yield when delay == 0 and "yield after run" flag is set |
| 97 | this_thread::yield(); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | else { |
| 103 | if(this_thread_obj->get_yield_after_run()) { |
| 104 | this_thread::yield(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | if(this_thread_obj->thread_should_finish()) { |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | this_thread_obj->set_thread_status(THREAD_STATUS::FINISHED); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | void thread_base::finish() { |
| 118 | if (thread_obj != nullptr) { |
nothing calls this directly
no test coverage detected