GRPC C++  1.0.0
thd_no_cxx11.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPCXX_IMPL_THD_NO_CXX11_H
35 #define GRPCXX_IMPL_THD_NO_CXX11_H
36 
37 #include <grpc/support/thd.h>
38 
39 namespace grpc {
40 
41 class thread {
42  public:
43  template <class T>
44  thread(void (T::*fptr)(), T *obj) {
45  func_ = new thread_function<T>(fptr, obj);
46  joined_ = false;
47  start();
48  }
49  template <class T, class U>
50  thread(void (T::*fptr)(U arg), T *obj, U arg) {
51  func_ = new thread_function_arg<T, U>(fptr, obj, arg);
52  joined_ = false;
53  start();
54  }
55  ~thread() {
56  if (!joined_) std::terminate();
57  delete func_;
58  }
59  thread(thread &&other)
60  : func_(other.func_), thd_(other.thd_), joined_(other.joined_) {
61  other.joined_ = true;
62  other.func_ = NULL;
63  }
64  void join() {
65  gpr_thd_join(thd_);
66  joined_ = true;
67  }
68 
69  private:
70  void start() {
71  gpr_thd_options options = gpr_thd_options_default();
72  gpr_thd_options_set_joinable(&options);
73  gpr_thd_new(&thd_, thread_func, (void *)func_, &options);
74  }
75  static void thread_func(void *arg) {
76  thread_function_base *func = (thread_function_base *)arg;
77  func->call();
78  }
79  class thread_function_base {
80  public:
81  virtual ~thread_function_base() {}
82  virtual void call() = 0;
83  };
84  template <class T>
85  class thread_function : public thread_function_base {
86  public:
87  thread_function(void (T::*fptr)(), T *obj) : fptr_(fptr), obj_(obj) {}
88  virtual void call() { (obj_->*fptr_)(); }
89 
90  private:
91  void (T::*fptr_)();
92  T *obj_;
93  };
94  template <class T, class U>
95  class thread_function_arg : public thread_function_base {
96  public:
97  thread_function_arg(void (T::*fptr)(U arg), T *obj, U arg)
98  : fptr_(fptr), obj_(obj), arg_(arg) {}
99  virtual void call() { (obj_->*fptr_)(arg_); }
100 
101  private:
102  void (T::*fptr_)(U arg);
103  T *obj_;
104  U arg_;
105  };
106  thread_function_base *func_;
107  gpr_thd_id thd_;
108  bool joined_;
109 
110  // Disallow copy and assign.
111  thread(const thread &);
112  void operator=(const thread &);
113 };
114 
115 } // namespace grpc
116 
117 #endif // GRPCXX_IMPL_THD_NO_CXX11_H
thread(void(T::*fptr)(), T *obj)
Definition: thd_no_cxx11.h:44
void join()
Definition: thd_no_cxx11.h:64
Definition: thd_no_cxx11.h:41
~thread()
Definition: thd_no_cxx11.h:55
thread(thread &&other)
Definition: thd_no_cxx11.h:59
Definition: alarm.h:48
thread(void(T::*fptr)(U arg), T *obj, U arg)
Definition: thd_no_cxx11.h:50