2014-12-22 23:31:11 +00:00
|
|
|
#pragma once
|
|
|
|
|
2015-11-26 08:06:29 +00:00
|
|
|
using sleep_entry_t = class CPUThread;
|
|
|
|
using sleep_queue_t = std::deque<std::shared_ptr<sleep_entry_t>>;
|
2015-07-07 22:33:24 +00:00
|
|
|
|
2015-08-07 21:37:32 +00:00
|
|
|
static struct defer_sleep_t {} const defer_sleep{};
|
2015-07-19 01:56:33 +00:00
|
|
|
|
|
|
|
// automatic object handling a thread entry in the sleep queue
|
2015-07-07 22:33:24 +00:00
|
|
|
class sleep_queue_entry_t final
|
2014-12-22 23:31:11 +00:00
|
|
|
{
|
2015-11-26 08:06:29 +00:00
|
|
|
sleep_entry_t& m_thread;
|
2015-07-07 22:33:24 +00:00
|
|
|
sleep_queue_t& m_queue;
|
2014-12-24 16:09:32 +00:00
|
|
|
|
2015-07-19 01:56:33 +00:00
|
|
|
void add_entry();
|
|
|
|
void remove_entry();
|
|
|
|
bool find() const;
|
|
|
|
|
2014-12-24 16:09:32 +00:00
|
|
|
public:
|
2015-07-19 01:56:33 +00:00
|
|
|
// add specified thread to the sleep queue
|
2015-11-26 08:06:29 +00:00
|
|
|
sleep_queue_entry_t(sleep_entry_t& entry, sleep_queue_t& queue);
|
2014-12-28 13:15:22 +00:00
|
|
|
|
2015-07-19 01:56:33 +00:00
|
|
|
// don't add specified thread to the sleep queue
|
2015-11-26 08:06:29 +00:00
|
|
|
sleep_queue_entry_t(sleep_entry_t& entry, sleep_queue_t& queue, const defer_sleep_t&);
|
2015-07-19 01:56:33 +00:00
|
|
|
|
|
|
|
// removes specified thread from the sleep queue if added
|
2015-08-19 11:04:58 +00:00
|
|
|
~sleep_queue_entry_t();
|
2015-07-19 01:56:33 +00:00
|
|
|
|
|
|
|
// add thread to the sleep queue
|
2015-09-26 20:46:04 +00:00
|
|
|
void enter()
|
2015-07-19 01:56:33 +00:00
|
|
|
{
|
|
|
|
add_entry();
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove thread from the sleep queue
|
2015-09-26 20:46:04 +00:00
|
|
|
void leave()
|
2015-07-19 01:56:33 +00:00
|
|
|
{
|
|
|
|
remove_entry();
|
|
|
|
}
|
|
|
|
|
|
|
|
// check whether the thread exists in the sleep queue
|
2015-09-26 20:46:04 +00:00
|
|
|
explicit operator bool() const
|
2015-07-19 01:56:33 +00:00
|
|
|
{
|
|
|
|
return find();
|
|
|
|
}
|
2014-12-22 23:31:11 +00:00
|
|
|
};
|