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
)
23 worker_thread::worker_thread()
31 exception_caught
= false;
32 exception_oom
= false;
36 worker_thread::~worker_thread()
38 set_workflag(WORKFLAG_QUIT_REQUEST
);
45 void worker_thread::request_quit()
48 //If the thread isn't there yet, wait for it.
49 umutex_class
h(mutex
);
53 set_workflag(WORKFLAG_QUIT_REQUEST
);
59 void worker_thread::set_busy()
64 void worker_thread::clear_busy()
66 umutex_class
h(mutex
);
68 condition
.notify_all();
71 void worker_thread::wait_busy()
73 umutex_class
h(mutex
);
75 uint64_t tmp
= ticks();
78 waitamt_busy
+= (ticks() - tmp
);
82 void worker_thread::rethrow()
84 if(exception_caught
) {
86 throw std::bad_alloc();
88 throw std::runtime_error(exception_text
);
92 void worker_thread::set_workflag(uint32_t flag
)
94 umutex_class
h(mutex
);
96 condition
.notify_all();
99 uint32_t worker_thread::clear_workflag(uint32_t flag
)
101 umutex_class
h(mutex
);
102 uint32_t tmp
= workflag
;
107 uint32_t worker_thread::wait_workflag()
109 umutex_class
h(mutex
);
111 uint64_t tmp
= ticks();
114 waitamt_work
+= (ticks() - tmp
);
119 std::pair
<uint64_t, uint64_t> worker_thread::get_wait_count()
121 umutex_class
h(mutex
);
122 return std::make_pair(waitamt_busy
, waitamt_work
);
125 int worker_thread::operator()(int dummy
)
129 } catch(std::bad_alloc
& e
) {
130 exception_oom
= true;
131 exception_caught
= true;
133 } catch(std::exception
& e
) {
134 exception_text
= e
.what();
135 exception_caught
= true;
141 void worker_thread::fire()
143 reflector
= new worker_thread_reflector
;
144 thread
= new thread_class(*reflector
, this);