vSMC
vSMC: Scalable Monte Carlo
dispatch_queue.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/gcd/dispatch_queue.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_QUEUE_HPP
33 #define VSMC_GCD_DISPATCH_QUEUE_HPP
34 
35 #include <vsmc/internal/common.hpp>
37 #include <dispatch/dispatch.h>
38 
39 namespace vsmc {
40 
47 }; // enum DispatchQueueType
48 
49 template <DispatchQueueType> class DispatchQueue;
50 
53 class DispatchQueueBase : public DispatchObject< ::dispatch_queue_t>
54 {
55  public :
56 
57  void resume () const {::dispatch_resume(this->object());}
58 
59  void suspend () const {::dispatch_suspend(this->object());}
60 
61  const char *get_label () const
62  {return ::dispatch_queue_get_label(this->object());}
63 
64 #if VSMC_HAS_GCD_LION
65  void *get_specific (const void *key) const
66  {return ::dispatch_queue_get_specific(this->object(), key);}
67 
68  void set_specific (const void *key, void *context,
69  ::dispatch_function_t destructor) const
70  {::dispatch_queue_set_specific(this->object(), key, context, destructor);}
71 #endif // VSMC_HAS_GCD_LION
72 
73  template <typename DispatchType>
75  {::dispatch_set_target_queue(object.object(), this->object());}
76 
77  void set_target_queue (::dispatch_object_t object) const
78  {::dispatch_set_target_queue(object, this->object());}
79 
80  void after_f (::dispatch_time_t when, void *context,
81  ::dispatch_function_t f) const
82  {::dispatch_after_f(when, this->object(), context, f);}
83 
84  void apply_f (std::size_t iterations, void *context,
85  void (*work) (void *, std::size_t)) const
86  {::dispatch_apply_f(iterations, this->object(), context, work);}
87 
88  void async_f (void *context, ::dispatch_function_t work) const
89  {::dispatch_async_f(this->object(), context, work);}
90 
91  void sync_f (void *context, ::dispatch_function_t work) const
92  {::dispatch_sync_f(this->object(), context, work);}
93 
94 #if VSMC_HAS_GCD_LION
95  void barrier_async_f (void *context, ::dispatch_function_t work) const
96  {::dispatch_barrier_async_f(this->object(), context, work);}
97 
98  void barrier_sync_f (void *context, ::dispatch_function_t work) const
99  {::dispatch_barrier_sync_f(this->object(), context, work);}
100 #endif // VSMC_HAS_GCD_LION
101 
102 #ifdef __BLOCKS__
103  void after (::dispatch_time_t when, ::dispatch_block_t block) const
104  {::dispatch_after(when, this->object(), block);}
105 
106  void apply (std::size_t iterations, void (^block) (std::size_t)) const
107  {::dispatch_apply(iterations, this->object(), block);}
108 
109  void async (::dispatch_block_t block) const
110  {::dispatch_async(this->object(), block);}
111 
112  void sync (::dispatch_block_t block) const
113  {::dispatch_sync(this->object(), block);}
114 
115 #if VSMC_HAS_GCD_LION
116  void barrier_async (::dispatch_block_t block) const
117  {::dispatch_barrier_async(this->object(), block);}
118 
119  void barrier_sync (::dispatch_block_t block) const
120  {::dispatch_barrier_sync(this->object(), block);}
121 #endif // VSMC_HAS_GCD_LION
122 #endif // __BLOCKS__
123 
124  protected :
125 
126  DispatchQueueBase (::dispatch_queue_t queue, bool retained) :
127  DispatchObject< ::dispatch_queue_t>(queue, retained) {}
128 }; // class DispatchQueueBase
129 
132 template <>
134 {
135  public :
136 
137  DispatchQueue () : DispatchQueueBase(dispatch_get_main_queue(), false) {}
138 }; // class DispatchQueue
139 
142 template <>
144 {
145  public :
146 
147 #if VSMC_HAS_GCD_LION
148  DispatchQueue (::dispatch_queue_priority_t priority =
149  DISPATCH_QUEUE_PRIORITY_DEFAULT, unsigned long flags = 0) :
150  DispatchQueueBase(::dispatch_get_global_queue(priority, flags), false)
151  {}
152 #else // VSMC_HAS_GCD_LION
153  DispatchQueue (long priority = 0, unsigned long flags = 0) :
154  DispatchQueueBase(::dispatch_get_global_queue(priority, flags), false)
155  {}
156 #endif // VSMC_HAS_GCD_LION
157 }; // class DispatchQueue
158 
161 template <>
163 {
164  public :
165 
166  DispatchQueue (const char *label = VSMC_NULLPTR,
167  ::dispatch_queue_attr_t attr = VSMC_NULLPTR) :
168  DispatchQueueBase(::dispatch_queue_create(label, attr), true) {}
169 }; // class DispatchQueue
170 
171 } // namespace vsmc
172 
173 #endif // VSMC_GCD_DISPATCH_QUEUE_HPP
Base class of DispatchQueue.
Definition: adapter.hpp:37
void * get_specific(const void *key) const
Base class of Dispatch objects.
void set_target_queue(const DispatchObject< DispatchType > &object) const
void after_f(::dispatch_time_t when, void *context,::dispatch_function_t f) const
void async(::dispatch_block_t block) const
The queue created by dispatch_queue_create
void barrier_async(::dispatch_block_t block) const
DispatchQueueBase(::dispatch_queue_t queue, bool retained)
void barrier_sync_f(void *context,::dispatch_function_t work) const
void after(::dispatch_time_t when,::dispatch_block_t block) const
void sync(::dispatch_block_t block) const
void apply_f(std::size_t iterations, void *context, void(*work)(void *, std::size_t)) const
void set_target_queue(::dispatch_object_t object) const
const char * get_label() const
The queue obtained by dispatch_get_gloal_queue
#define VSMC_NULLPTR
nullptr
Definition: defines.hpp:79
void sync_f(void *context,::dispatch_function_t work) const
void barrier_sync(::dispatch_block_t block) const
The queue obtained by dispatch_get_main_queue
void barrier_async_f(void *context,::dispatch_function_t work) const
DispatchQueue(::dispatch_queue_priority_t priority=DISPATCH_QUEUE_PRIORITY_DEFAULT, unsigned long flags=0)
void apply(std::size_t iterations, void(^block)(std::size_t)) const
DispatchQueue(const char *label=nullptr,::dispatch_queue_attr_t attr=nullptr)
DispatchQueueType
Types of DispatchQueue.
void async_f(void *context,::dispatch_function_t work) const
void set_specific(const void *key, void *context,::dispatch_function_t destructor) const