Pol  Revision:cb584c9
threadhelp.h
Go to the documentation of this file.
1 
7 #ifndef CLIB_THREADHELP_H
8 #define CLIB_THREADHELP_H
9 
10 #include <atomic>
11 #include <boost/noncopyable.hpp>
12 #include <functional>
13 #include <future>
14 #include <map>
15 #include <mutex>
16 #include <string>
17 #include <thread>
18 #include <vector>
19 
20 #include "Header_Windows.h"
21 #include "message_queue.h"
22 #include "spinlock.h"
23 
24 namespace Pol
25 {
26 namespace threadhelp
27 {
28 extern std::atomic<unsigned int> child_threads;
29 
30 void init_threadhelp();
31 void run_thread( void ( *threadf )( void ) );
32 void run_thread( void ( *threadf )( void* ), void* arg );
33 
34 void start_thread( void ( *entry )( void* ), const char* thread_name, void* arg );
35 void start_thread( void ( *entry )( void ), const char* thread_name );
36 
37 void thread_sleep_ms( unsigned milliseconds );
38 size_t thread_pid();
39 
40 class ThreadMap
41 {
42 public:
43  typedef std::map<size_t, std::string> Contents;
44 #ifdef _WIN32
45  typedef std::map<size_t, HANDLE> HANDLES;
46  HANDLE getThreadHandle( size_t pid ) const;
47 #endif
48  void Register( size_t pid, const std::string& name );
49  void Unregister( size_t pid );
50  void CopyContents( Contents& out ) const;
51 
52  ThreadMap();
53 
54 private:
56  Contents _contents;
57 #ifdef _WIN32
58  HANDLES _handles;
59 #endif
60 };
61 extern ThreadMap threadmap;
62 #ifdef _WIN32
63 void SetThreadName( int dwThreadID, std::string threadName );
64 #endif
65 
67 {
68 public:
69  ThreadRegister( const std::string& name );
70  ~ThreadRegister();
71 };
72 
73 
74 class TaskThreadPool : boost::noncopyable
75 {
76  typedef std::function<void()> msg;
78 
79 public:
81  TaskThreadPool( const std::string& name );
82  TaskThreadPool( unsigned int max_count, const std::string& name );
83  ~TaskThreadPool();
84  void push( const msg& msg );
85  std::future<bool> checked_push( const msg& msg );
86  size_t size() const;
87 
88  void init_pool( unsigned int max_count, const std::string& name );
89  void deinit_pool();
90 
91 private:
92  void init( unsigned int max_count, const std::string& name );
93  std::atomic<bool> _done;
94  msg_queue _msg_queue;
95  std::vector<std::thread> _threads;
96 };
97 
98 class DynTaskThreadPool : boost::noncopyable
99 {
100  class PoolWorker;
101 
102  friend class PoolWorker;
103  typedef std::function<void()> msg;
105 
106 public:
107  DynTaskThreadPool( const std::string& name );
109  void push( const msg& msg );
110  std::future<bool> checked_push( const msg& msg );
111  size_t threadpoolsize() const;
112 
113 protected:
114  std::atomic<bool> _done;
115 
116 private:
117  void create_thread();
118  msg_queue _msg_queue;
119  std::vector<std::unique_ptr<PoolWorker>> _threads;
120  mutable std::mutex _pool_mutex;
121  std::string _name;
122 };
123 
124 
125 } // namespace threadhelp
126 }
127 #endif // CLIB_THREADHELP_H
std::vector< std::unique_ptr< PoolWorker > > _threads
Definition: threadhelp.h:119
void create_thread(ThreadData *td, bool dec_child=false)
Definition: threadhelp.cpp:213
size_t thread_pid()
Definition: threadhelp.cpp:107
void start_thread(void(*entry)(void *), const char *thread_name, void *arg)
Definition: threadhelp.cpp:233
u64 pid
Definition: osmod.cpp:945
Clib::message_queue< msg > msg_queue
Definition: threadhelp.h:77
Clib::message_queue< msg > msg_queue
Definition: threadhelp.h:104
std::function< void()> msg
Definition: threadhelp.h:103
void init_threadhelp()
Definition: threadhelp.cpp:94
void Unregister(size_t pid)
Definition: threadhelp.cpp:296
void CopyContents(Contents &out) const
Definition: threadhelp.cpp:307
void run_thread(void(*threadf)(void))
Definition: threadhelp.cpp:113
std::atomic< unsigned int > child_threads
std::map< size_t, std::string > Contents
Definition: threadhelp.h:43
std::string name
Definition: osmod.cpp:943
Clib::SpinLock _spinlock
Definition: threadhelp.h:55
void thread_sleep_ms(unsigned millis)
Definition: threadhelp.cpp:103
std::atomic< bool > _done
Definition: threadhelp.h:93
Definition: berror.cpp:12
ThreadMap threadmap
Definition: threadhelp.cpp:40
std::function< void()> msg
Definition: threadhelp.h:76
void Register(size_t pid, const std::string &name)
Definition: threadhelp.cpp:281
std::vector< std::thread > _threads
Definition: threadhelp.h:95