Zero  0.1.0
thread_wrapper.h
Go to the documentation of this file.
1 #ifndef __THREAD_WRAPPER_H
2 #define __THREAD_WRAPPER_H
3 
4 #include <thread>
5 #include <memory>
6 
7 #include "w_rc.h"
8 #include "tls.h"
9 #include "latch.h"
10 
11 /*
12  * The sole purpose of this class is to replace sthread_t with as little code impact as
13  * possible -- new code should use the C++11 thread library directly (as soon as the TODO
14  * below about tls_manager is fixed...)
15  */
17 public:
18 
20 
21  virtual ~thread_wrapper_t() {
22  thread_ptr.reset();
23  }
24 
25  /*
26  * Virtual methods to be overridden by sub-classes.
27  */
28  virtual void run() = 0;
29 
30  virtual void before_run() {};
31 
32  virtual void after_run() {};
33 
34  void spawn() {
35  // CS TODO: these are required for the old shore TLS allocator, which is still used.
36  // With C++11 and the thread_local specifier, it should be much easier to perform this
37  // type of static initialization;
40 
41  before_run();
42  run();
43  after_run();
44 
47 
48  // latch_t maintains some static data structures that must be deleted manually
50  }
51 
52  void fork() {
53  thread_ptr.reset(new std::thread([this] {
54  spawn();
55  }));
56  }
57 
58  void join() {
59  if (thread_ptr) {
60  thread_ptr->join();
61  thread_ptr = nullptr;
62  }
63  }
64 
65 private:
66  std::unique_ptr<std::thread> thread_ptr;
67 };
68 
69 #endif // __THREAD_WRAPPER_H
thread_wrapper_t()
Definition: thread_wrapper.h:19
Definition: thread_wrapper.h:16
static void thread_init()
Definition: tls.cpp:107
virtual void after_run()
Definition: thread_wrapper.h:32
static void on_thread_destroy()
Destroy TLS structures.
Definition: latch.cpp:567
void spawn()
Definition: thread_wrapper.h:34
static void thread_fini()
Definition: tls.cpp:118
static void add_me_to_thread_list()
Definition: smthread.h:561
virtual void run()=0
virtual ~thread_wrapper_t()
Definition: thread_wrapper.h:21
void join()
Definition: thread_wrapper.h:58
virtual void before_run()
Definition: thread_wrapper.h:30
void fork()
Definition: thread_wrapper.h:52
std::unique_ptr< std::thread > thread_ptr
Definition: thread_wrapper.h:66
static void remove_me_from_thread_list()
Definition: smthread.h:565