lsnes rr2-β23
[lsnes.git] / include / library / threads.hpp
blob871758d3c772ee2011e6dcd27b4f3602faccfe74
1 #ifndef _library_threads__hpp__included__
2 #define _library_threads__hpp__included__
4 #include <cstdint>
5 #include <vector>
7 #ifdef NATIVE_THREADS
8 #include <thread>
9 #include <condition_variable>
10 #include <mutex>
11 #else
12 #include <boost/thread.hpp>
13 #include <boost/thread/locks.hpp>
14 #endif
16 namespace threads
18 #ifdef NATIVE_THREADS
19 typedef std::thread thread;
20 typedef std::condition_variable cv;
21 typedef std::mutex lock;
22 typedef std::recursive_mutex rlock;
23 typedef std::unique_lock<std::mutex> alock;
24 typedef std::unique_lock<std::recursive_mutex> arlock;
25 typedef std::chrono::microseconds ustime;
26 typedef std::thread::id id;
27 inline void cv_timed_wait(cv& c, alock& m, const ustime& t)
29 c.wait_for(m, t);
31 inline id this_id()
33 return std::this_thread::get_id();
35 #else
36 typedef boost::thread thread;
37 typedef boost::condition_variable cv;
38 typedef boost::mutex lock;
39 typedef boost::recursive_mutex rlock;
40 typedef boost::unique_lock<boost::mutex> alock;
41 typedef boost::unique_lock<boost::recursive_mutex> arlock;
42 typedef boost::posix_time::microseconds ustime;
43 typedef boost::thread::id id;
44 inline void cv_timed_wait(cv& c, alock& m, const ustime& t)
46 c.timed_wait(m, t);
48 inline id this_id()
50 return boost::this_thread::get_id();
52 #endif
54 /**
55 * Lock multiple locks.
57 * The locks are always locked in address order. Duplicate locks are only locked once.
59 void lock_multiple(std::initializer_list<lock*> locks);
60 /**
61 * Unlock multiple locks.
63 * Duplicate locks are only unlocked once.
65 void unlock_multiple(std::initializer_list<lock*> locks);
66 void unlock_multiple(std::vector<lock*> locks);
68 class alock_multiple
70 public:
71 alock_multiple(std::initializer_list<lock*> locks)
73 _locks = std::vector<lock*>(locks);
74 lock_multiple(locks);
76 ~alock_multiple()
78 unlock_multiple(_locks);
80 private:
81 std::vector<lock*> _locks;
85 #endif