vSMC
vSMC: Scalable Monte Carlo
dispatch_source.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/gcd/dispatch_source.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013,2014, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef VSMC_GCD_DISPATCH_SOURCE_HPP
33 #define VSMC_GCD_DISPATCH_SOURCE_HPP
34 
35 #include <vsmc/internal/common.hpp>
38 
39 namespace vsmc {
40 
54 }; // enum DispatchSourceType
55 
56 template <DispatchSourceType> class DispatchSource;
57 
70 template <DispatchSourceType Type>
71 class DispatchSourceBase : public DispatchObject< ::dispatch_source_t>
72 {
73  public :
74 
75  void resume () const {::dispatch_resume(this->object());}
76 
77  void suspend () const {::dispatch_suspend(this->object());}
78 
79  void cancel () const {::dispatch_source_cancel(this->object());}
80 
81  long testcancel () const
82  {return ::dispatch_source_testcancel(this->object());}
83 
84  unsigned long get_data () const
85  {return ::dispatch_source_get_data(this->object());}
86 
87  uintptr_t get_handle () const
88  {return ::dispatch_source_get_handle(this->object());}
89 
90  unsigned long get_mask () const
91  {return ::dispatch_source_get_mask(this->object());}
92 
93  void set_cancel_handler_f (::dispatch_function_t cancel_handler) const
94  {::dispatch_source_set_cancel_handler_f(this->object(), cancel_handler);}
95 
96  void set_event_handler_f (::dispatch_function_t event_handler) const
97  {::dispatch_source_set_event_handler_f(this->object(), event_handler);}
98 
99 #if VSMC_HAS_GCD_LION
100  void set_registration_handler_f (::dispatch_function_t
101  registration_handler) const
102  {
103  ::dispatch_source_set_registration_handler_f(
104  this->object(), registration_handler);
105  }
106 #endif // VSMC_HAS_GCD_LION
107 
108 #ifdef __BLOCKS__
109  void set_cancel_handler (::dispatch_block_t cancel_handler) const
110  {::dispatch_source_set_cancel_handler(this->object(), cancel_handler);}
111 
112  void set_event_handler (::dispatch_block_t event_handler) const
113  {::dispatch_source_set_event_handler(this->object(), event_handler);}
114 
115 #if VSMC_HAS_GCD_LION
116  void set_registration_handler (::dispatch_block_t
117  registration_handler) const
118  {
119  ::dispatch_source_set_registration_handler(
120  this->object(), registration_handler);
121  }
122 #endif // VSMC_HAS_GCD_LION
123 #endif // __BLOCKS__
124 
125  private :
126 
127  template <DispatchSourceType> struct source_type {};
128 
129  protected :
130 
131  DispatchSourceBase (uintptr_t handle, unsigned long mask,
132  ::dispatch_queue_t queue) :
133  DispatchObject< ::dispatch_source_t>(::dispatch_source_create(
134  source_type_t(source_type<Type>()),
135  handle, mask, queue), false) {}
136 
137  private :
138 
139  static ::dispatch_source_type_t source_type_t (
140  source_type<DispatchDataAdd>)
141  {return DISPATCH_SOURCE_TYPE_DATA_ADD;}
142 
143  static ::dispatch_source_type_t source_type_t (
144  source_type<DispatchDataOr>)
145  {return DISPATCH_SOURCE_TYPE_DATA_OR;}
146 
147  static ::dispatch_source_type_t source_type_t (
148  source_type<DispatchMachRecv>)
149  {return DISPATCH_SOURCE_TYPE_MACH_RECV;}
150 
151  static ::dispatch_source_type_t source_type_t (
152  source_type<DispatchMachSend>)
153  {return DISPATCH_SOURCE_TYPE_MACH_SEND;}
154 
155  static ::dispatch_source_type_t source_type_t (
156  source_type<DispatchProc>)
157  {return DISPATCH_SOURCE_TYPE_PROC;}
158 
159  static ::dispatch_source_type_t source_type_t (
160  source_type<DispatchRead>)
161  {return DISPATCH_SOURCE_TYPE_READ;}
162 
163  static ::dispatch_source_type_t source_type_t (
164  source_type<DispatchSignal>)
165  {return DISPATCH_SOURCE_TYPE_SIGNAL;}
166 
167  static ::dispatch_source_type_t source_type_t (
168  source_type<DispatchTimer>)
169  {return DISPATCH_SOURCE_TYPE_TIMER;}
170 
171  static ::dispatch_source_type_t source_type_t (
172  source_type<DispatchVnode>)
173  {return DISPATCH_SOURCE_TYPE_VNODE;}
174 
175  static ::dispatch_source_type_t source_type_t (
176  source_type<DispatchWrite>)
177  {return DISPATCH_SOURCE_TYPE_WRITE;}
178 }; // class DispatchSourceBase
179 
182 template <DispatchSourceType Type>
183 class DispatchSource : public DispatchSourceBase<Type>
184 {
185  public :
186 
187  template <DispatchQueueType QType>
188  DispatchSource (uintptr_t handle, unsigned long mask,
189  const DispatchQueue<QType> &queue) :
190  DispatchSourceBase<Type>(handle, mask, queue.object()) {}
191 
192  DispatchSource (uintptr_t handle, unsigned long mask,
193  ::dispatch_queue_t queue) :
194  DispatchSourceBase<Type>(handle, mask, queue) {}
195 }; // class DispatchSource
196 
199 template <>
201  public DispatchSourceBase<DispatchDataAdd>
202 {
203  public :
204 
205  template <DispatchQueueType QType>
206  DispatchSource (uintptr_t handle, unsigned long mask,
207  const DispatchQueue<QType> &queue) :
208  DispatchSourceBase<DispatchDataAdd>(handle, mask, queue.object()) {}
209 
210  DispatchSource (uintptr_t handle, unsigned long mask,
211  ::dispatch_queue_t queue) :
212  DispatchSourceBase<DispatchDataAdd>(handle, mask, queue) {}
213 
214  void merge_data (unsigned long value) const
215  {::dispatch_source_merge_data(this->object(), value);}
216 }; // class DispatchSource
217 
220 template <>
222  public DispatchSourceBase<DispatchDataOr>
223 {
224  public :
225 
226  template <DispatchQueueType QType>
227  DispatchSource (uintptr_t handle, unsigned long mask,
228  const DispatchQueue<QType> &queue) :
229  DispatchSourceBase<DispatchDataOr>(handle, mask, queue.object()) {}
230 
231  DispatchSource (uintptr_t handle, unsigned long mask,
232  ::dispatch_queue_t queue) :
233  DispatchSourceBase<DispatchDataOr>(handle, mask, queue) {}
234 
235  void merge_data (unsigned long value) const
236  {::dispatch_source_merge_data(this->object(), value);}
237 }; // class DispatchSource
238 
241 template <>
243  public DispatchSourceBase<DispatchTimer>
244 {
245  public :
246 
247  template <DispatchQueueType QType>
248  DispatchSource (uintptr_t handle, unsigned long mask,
249  const DispatchQueue<QType> &queue) :
250  DispatchSourceBase<DispatchTimer>(handle, mask, queue.object()) {}
251 
252  DispatchSource (uintptr_t handle, unsigned long mask,
253  ::dispatch_queue_t queue) :
254  DispatchSourceBase<DispatchTimer>(handle, mask, queue) {}
255 
256  void set_timer (::dispatch_time_t start,
257  uint64_t interval, uint64_t leeway) const
258  {::dispatch_source_set_timer(this->object(), start, interval, leeway);}
259 }; // class DispatchSource
260 
261 } // namespace vsmc
262 
263 #endif // VSMC_GCD_DISPATCH_SOURCE_HPP
DISPATCH_SOURCE_TYPE_SIGNAL.
Definition: adapter.hpp:37
Base class of Dispatch objects.
uintptr_t get_handle() const
DispatchSourceBase(uintptr_t handle, unsigned long mask,::dispatch_queue_t queue)
DispatchSource(uintptr_t handle, unsigned long mask, const DispatchQueue< QType > &queue)
void set_cancel_handler_f(::dispatch_function_t cancel_handler) const
DISPATCH_SOURCE_TYPE_PROC.
DispatchSource(uintptr_t handle, unsigned long mask, const DispatchQueue< QType > &queue)
void set_event_handler_f(::dispatch_function_t event_handler) const
void set_event_handler(::dispatch_block_t event_handler) const
void merge_data(unsigned long value) const
DISPATCH_SOURCE_TYPE_VNODE.
void set_cancel_handler(::dispatch_block_t cancel_handler) const
void set_registration_handler_f(::dispatch_function_t registration_handler) const
Base class of DispatchSource.
void set_timer(::dispatch_time_t start, uint64_t interval, uint64_t leeway) const
DispatchSource(uintptr_t handle, unsigned long mask, const DispatchQueue< QType > &queue)
DISPATCH_SOURCE_TYPE_MACH_RECV.
DISPATCH_SOURCE_TYPE_READ.
DispatchSource(uintptr_t handle, unsigned long mask,::dispatch_queue_t queue)
DISPATCH_SOURCE_TYPE_WRITE.
DISPATCH_SOURCE_TYPE_MACH_SEND.
void set_registration_handler(::dispatch_block_t registration_handler) const
A dispatch source.
DispatchSource(uintptr_t handle, unsigned long mask,::dispatch_queue_t queue)
DISPATCH_SOURCE_TYPE_DATA_OR.
unsigned long get_data() const
DispatchSourceType
Types of DispatchSource.
void merge_data(unsigned long value) const
DISPATCH_SOURCE_TYPE_DATA_ADD.
unsigned long get_mask() const
DISPATCH_SOURCE_TYPE_TIMER.
DispatchSource(uintptr_t handle, unsigned long mask,::dispatch_queue_t queue)
DispatchSource(uintptr_t handle, unsigned long mask, const DispatchQueue< QType > &queue)
DispatchSource(uintptr_t handle, unsigned long mask,::dispatch_queue_t queue)
::dispatch_source_t object() const
Return the underlying Dispatch object.