STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
pplinterface.h
Go to the documentation of this file.
1 /***
2 * ==++==
3 *
4 * Copyright (c) Microsoft Corporation. All rights reserved.
5 *
6 * ==--==
7 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
8 *
9 * pplinterface.h
10 *
11 * Parallel Patterns Library - PPL interface definitions
12 *
13 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
14 ****/
15 
16 #pragma once
17 
18 #ifndef _PPLINTERFACE_H
19 #define _PPLINTERFACE_H
20 
21 #if defined(_CRTBLD)
22 #elif defined(_MS_WINDOWS)
23 #if (_MSC_VER >= 1700)
24 #define _USE_REAL_ATOMICS
25 #endif
26 #else // GCC compiler
27 #define _USE_REAL_ATOMICS
28 #endif
29 
30 #include <memory>
31 #ifdef _USE_REAL_ATOMICS
32 #include <atomic>
33 #endif
34 
35 namespace Concurrency
36 {
37 
42 
43 typedef void (__cdecl * TaskProc_t)(void *);
44 
48 struct __declspec(novtable) scheduler_interface
49 {
50  virtual void schedule( TaskProc_t, void* ) = 0;
51 };
52 
59 {
63  explicit scheduler_ptr(std::shared_ptr<scheduler_interface> scheduler) : m_sharedScheduler(std::move(scheduler))
64  {
66  }
67 
71  explicit scheduler_ptr(_In_opt_ scheduler_interface * pScheduler) : m_scheduler(pScheduler)
72  {
73  }
74 
78  scheduler_interface *operator->() const
79  {
80  return get();
81  }
82 
86  scheduler_interface * get() const
87  {
88  return m_scheduler;
89  }
90 
94  operator bool() const { return get() != nullptr; }
95 
96 private:
97 
98  std::shared_ptr<scheduler_interface> m_sharedScheduler;
99  scheduler_interface * m_scheduler;
100 };
101 
102 
113 
115 {
120 
122 
126 
128 
132 
134 };
135 
136 namespace details
137 {
141 #ifdef _USE_REAL_ATOMICS
142 typedef std::atomic<long> atomic_long;
143 typedef std::atomic<size_t> atomic_size_t;
144 
145 template<typename _T>
146 _T atomic_compare_exchange(std::atomic<_T>& _Target, _T _Exchange, _T _Comparand)
147 {
148  _T _Result = _Comparand;
149  _Target.compare_exchange_strong(_Result, _Exchange);
150  return _Result;
151 }
152 
153 template<typename _T>
154 _T atomic_exchange(std::atomic<_T>& _Target, _T _Value)
155 {
156  return _Target.exchange(_Value);
157 }
158 
159 template<typename _T>
160 _T atomic_increment(std::atomic<_T>& _Target)
161 {
162  return _Target.fetch_add(1) + 1;
163 }
164 
165 template<typename _T>
166 _T atomic_decrement(std::atomic<_T>& _Target)
167 {
168  return _Target.fetch_sub(1) - 1;
169 }
170 
171 template<typename _T>
172 _T atomic_add(std::atomic<_T>& _Target, _T value)
173 {
174  return _Target.fetch_add(value) + value;
175 }
176 
177 #else // not _USE_REAL_ATOMICS
178 
179 typedef long volatile atomic_long;
180 typedef size_t volatile atomic_size_t;
181 
182 template<class T>
183 inline T atomic_exchange(T volatile& _Target, T _Value)
184 {
185  return _InterlockedExchange(&_Target, _Value);
186 }
187 
188 inline long atomic_increment(long volatile & _Target)
189 {
190  return _InterlockedIncrement(&_Target);
191 }
192 
193 inline long atomic_add(long volatile & _Target, long value)
194 {
195  return _InterlockedExchangeAdd(&_Target, value) + value;
196 }
197 
198 inline size_t atomic_increment(size_t volatile & _Target)
199 {
200 #if (defined(_M_IX86) || defined(_M_ARM))
201  return static_cast<size_t>(_InterlockedIncrement(reinterpret_cast<long volatile *>(&_Target)));
202 #else
203  return static_cast<size_t>(_InterlockedIncrement64(reinterpret_cast<__int64 volatile *>(&_Target)));
204 #endif
205 }
206 
207 inline long atomic_decrement(long volatile & _Target)
208 {
209  return _InterlockedDecrement(&_Target);
210 }
211 
212 inline size_t atomic_decrement(size_t volatile & _Target)
213 {
214 #if (defined(_M_IX86) || defined(_M_ARM))
215  return static_cast<size_t>(_InterlockedDecrement(reinterpret_cast<long volatile *>(&_Target)));
216 #else
217  return static_cast<size_t>(_InterlockedDecrement64(reinterpret_cast<__int64 volatile *>(&_Target)));
218 #endif
219 }
220 
221 inline long atomic_compare_exchange(long volatile & _Target, long _Exchange, long _Comparand)
222 {
223  return _InterlockedCompareExchange(&_Target, _Exchange, _Comparand);
224 }
225 
226 inline size_t atomic_compare_exchange(size_t volatile & _Target, size_t _Exchange, size_t _Comparand)
227 {
228 #if (defined(_M_IX86) || defined(_M_ARM))
229  return static_cast<size_t>(_InterlockedCompareExchange(reinterpret_cast<long volatile *>(_Target), static_cast<long>(_Exchange), static_cast<long>(_Comparand)));
230 #else
231  return static_cast<size_t>(_InterlockedCompareExchange64(reinterpret_cast<__int64 volatile *>(_Target), static_cast<__int64>(_Exchange), static_cast<__int64>(_Comparand)));
232 #endif
233 }
234 #endif // _USE_REAL_ATOMICS
235 
236 }} // namespace Concurrency
237 
238 #endif // _PPLINTERFACE_H
_T atomic_exchange(std::atomic< _T > &_Target, _T _Value)
Definition: pplinterface.h:154
_CRTIMP _In_ int _Value
Definition: setjmp.h:190
_T atomic_compare_exchange(std::atomic< _T > &_Target, _T _Exchange, _T _Comparand)
Definition: pplinterface.h:146
std::atomic< size_t > atomic_size_t
Definition: pplinterface.h:143
_OutIt move(_InIt _First, _InIt _Last, _OutIt _Dest)
Definition: xutility:2447
scheduler_interface * operator->() const
Behave like a pointer
Definition: pplinterface.h:78
typedef void(__cdecl *_se_translator_function)(unsigned int
STL namespace.
The Concurrency namespace provides classes and functions that provide access to the Concurrency Runti...
Definition: agents.h:42
The tasks queued to the task_group object have not completed. Note that this value is not presently r...
Definition: pplinterface.h:121
_T atomic_decrement(std::atomic< _T > &_Target)
Definition: pplinterface.h:166
scheduler_interface * m_scheduler
Definition: pplinterface.h:99
#define _In_opt_
Definition: sal.h:315
#define bool
Definition: stdbool.h:10
The tasks queued to the task_group or structured_task_group object completed successfully.
Definition: pplinterface.h:127
The task_group or structured_task_group object was canceled. One or more tasks may not have executed...
Definition: pplinterface.h:133
scheduler_ptr(_In_opt_ scheduler_interface *pScheduler)
Creates a scheduler pointer from raw pointer to scheduler
Definition: pplinterface.h:71
Represents a pointer to a scheduler. This class exists to allow the the specification of a shared lif...
Definition: pplinterface.h:58
_T atomic_add(std::atomic< _T > &_Target, _T value)
Definition: pplinterface.h:172
scheduler_ptr(std::shared_ptr< scheduler_interface > scheduler)
Creates a scheduler pointer from shared_ptr to scheduler
Definition: pplinterface.h:63
void(__cdecl * TaskProc_t)(void *)
An elementary abstraction for a task, defined as void (__cdecl * TaskProc_t)(void *)...
Definition: pplinterface.h:43
std::shared_ptr< scheduler_interface > m_sharedScheduler
Definition: pplinterface.h:98
_T atomic_increment(std::atomic< _T > &_Target)
Definition: pplinterface.h:160
#define _T(x)
Definition: tchar.h:2498
task_group_status
Describes the execution status of a task_group or structured_task_group object. A value of this type ...
Definition: pplinterface.h:114
long __cdecl _InterlockedDecrement(long volatile *)
std::atomic< long > atomic_long
Atomics
Definition: pplinterface.h:142
long __cdecl _InterlockedIncrement(long volatile *)
long __cdecl _InterlockedCompareExchange(long volatile *, long, long)