1 #include "workthread.hpp"
10 gettimeofday(&tv
, NULL
);
11 return static_cast<uint64_t>(tv
.tv_sec
) * 1000000 + tv
.tv_usec
;
15 struct worker_thread_reflector
17 int operator()(worker_thread
* x
)
24 worker_thread::worker_thread()
32 exception_caught
= false;
33 exception_oom
= false;
37 worker_thread::~worker_thread()
39 set_workflag(WORKFLAG_QUIT_REQUEST
);
46 void worker_thread::request_quit()
49 //If the thread isn't there yet, wait for it.
50 umutex_class
h(mutex
);
54 set_workflag(WORKFLAG_QUIT_REQUEST
);
60 void worker_thread::set_busy()
65 void worker_thread::clear_busy()
67 umutex_class
h(mutex
);
69 condition
.notify_all();
72 void worker_thread::wait_busy()
74 umutex_class
h(mutex
);
76 uint64_t tmp
= ticks();
79 waitamt_busy
+= (ticks() - tmp
);
83 void worker_thread::rethrow()
85 if(exception_caught
) {
87 throw std::bad_alloc();
89 throw std::runtime_error(exception_text
);
93 void worker_thread::set_workflag(uint32_t flag
)
95 umutex_class
h(mutex
);
97 condition
.notify_all();
100 uint32_t worker_thread::clear_workflag(uint32_t flag
)
102 umutex_class
h(mutex
);
103 uint32_t tmp
= workflag
;
108 uint32_t worker_thread::wait_workflag()
110 umutex_class
h(mutex
);
112 uint64_t tmp
= ticks();
115 waitamt_work
+= (ticks() - tmp
);
120 std::pair
<uint64_t, uint64_t> worker_thread::get_wait_count()
122 umutex_class
h(mutex
);
123 return std::make_pair(waitamt_busy
, waitamt_work
);
126 int worker_thread::operator()(int dummy
)
130 } catch(std::bad_alloc
& e
) {
131 exception_oom
= true;
132 exception_caught
= true;
134 } catch(std::exception
& e
) {
135 exception_text
= e
.what();
136 exception_caught
= true;
142 void worker_thread::fire()
144 reflector
= new worker_thread_reflector
;
145 thread
= new thread_class(*reflector
, this);