1 #include "workthread.hpp"
6 const uint32_t workthread::quit_request
= 0x80000000U
;
13 gettimeofday(&tv
, NULL
);
14 return static_cast<uint64_t>(tv
.tv_sec
) * 1000000 + tv
.tv_usec
;
18 int workthread::reflector::operator()(workthread
* x
)
24 workthread::workthread()
32 exception_caught
= false;
33 exception_oom
= false;
37 workthread::~workthread()
39 set_workflag(quit_request
);
46 void workthread::request_quit()
49 //If the thread isn't there yet, wait for it.
50 threads::alock
h(mlock
);
54 set_workflag(quit_request
);
60 void workthread::set_busy()
65 void workthread::clear_busy()
67 threads::alock
h(mlock
);
69 condition
.notify_all();
72 void workthread::wait_busy()
74 threads::alock
h(mlock
);
76 uint64_t tmp
= ticks();
79 waitamt_busy
+= (ticks() - tmp
);
83 void workthread::rethrow()
85 if(exception_caught
) {
87 throw std::bad_alloc();
89 throw std::runtime_error(exception_text
);
93 void workthread::set_workflag(uint32_t flag
)
95 threads::alock
h(mlock
);
97 condition
.notify_all();
100 uint32_t workthread::clear_workflag(uint32_t flag
)
102 threads::alock
h(mlock
);
103 uint32_t tmp
= workflag
;
108 uint32_t workthread::wait_workflag()
110 threads::alock
h(mlock
);
112 uint64_t tmp
= ticks();
115 waitamt_work
+= (ticks() - tmp
);
120 std::pair
<uint64_t, uint64_t> workthread::get_wait_count()
122 threads::alock
h(mlock
);
123 return std::make_pair(waitamt_busy
, waitamt_work
);
126 int workthread::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 workthread::fire()
144 _reflector
= new reflector
;
145 thread
= new threads::thread(*_reflector
, this);