C++ Rest SDK
The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.
pplxinterface.h
1 /***
2 * ==++==
3 *
4 * Copyright (c) Microsoft Corporation. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * ==--==
17 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
18 *
19 * PPL interfaces
20 *
21 * For the latest on this and related APIs, please see http://casablanca.codeplex.com.
22 *
23 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
24 ****/
25 
26 #pragma once
27 
28 #ifndef _PPLXINTERFACE_H
29 #define _PPLXINTERFACE_H
30 
31 #if (defined(_MSC_VER) && (_MSC_VER >= 1800)) && !CPPREST_FORCE_PPLX
32 #error This file must not be included for Visual Studio 12 or later
33 #endif
34 
35 #if defined(_CRTBLD)
36 #elif defined(_WIN32)
37 #if (_MSC_VER >= 1700)
38 #define _USE_REAL_ATOMICS
39 #endif
40 #else // GCC compiler
41 #define _USE_REAL_ATOMICS
42 #endif
43 
44 #include <memory>
45 #ifdef _USE_REAL_ATOMICS
46 #include <atomic>
47 #endif
48 
49 #define _pplx_cdecl __cdecl
50 
51 namespace pplx
52 {
53 
58 
59 typedef void (_pplx_cdecl * TaskProc_t)(void *);
60 
64 struct __declspec(novtable) scheduler_interface
65 {
66  virtual void schedule( TaskProc_t, _In_ void* ) = 0;
67 };
68 
75 {
79  explicit scheduler_ptr(std::shared_ptr<scheduler_interface> scheduler) : m_sharedScheduler(std::move(scheduler))
80  {
81  m_scheduler = m_sharedScheduler.get();
82  }
83 
87  explicit scheduler_ptr(_In_opt_ scheduler_interface * pScheduler) : m_scheduler(pScheduler)
88  {
89  }
90 
94  scheduler_interface *operator->() const
95  {
96  return get();
97  }
98 
102  scheduler_interface * get() const
103  {
104  return m_scheduler;
105  }
106 
110  operator bool() const { return get() != nullptr; }
111 
112 private:
113 
114  std::shared_ptr<scheduler_interface> m_sharedScheduler;
115  scheduler_interface * m_scheduler;
116 };
117 
118 
129 
131 {
136 
138 
142 
144 
148 
150 };
151 
152 namespace details
153 {
157 #ifdef _USE_REAL_ATOMICS
158 typedef std::atomic<long> atomic_long;
159 typedef std::atomic<size_t> atomic_size_t;
160 
161 template<typename _T>
162 _T atomic_compare_exchange(std::atomic<_T>& _Target, _T _Exchange, _T _Comparand)
163 {
164  _T _Result = _Comparand;
165  _Target.compare_exchange_strong(_Result, _Exchange);
166  return _Result;
167 }
168 
169 template<typename _T>
170 _T atomic_exchange(std::atomic<_T>& _Target, _T _Value)
171 {
172  return _Target.exchange(_Value);
173 }
174 
175 template<typename _T>
176 _T atomic_increment(std::atomic<_T>& _Target)
177 {
178  return _Target.fetch_add(1) + 1;
179 }
180 
181 template<typename _T>
182 _T atomic_decrement(std::atomic<_T>& _Target)
183 {
184  return _Target.fetch_sub(1) - 1;
185 }
186 
187 template<typename _T>
188 _T atomic_add(std::atomic<_T>& _Target, _T value)
189 {
190  return _Target.fetch_add(value) + value;
191 }
192 
193 #else // not _USE_REAL_ATOMICS
194 
195 typedef long volatile atomic_long;
196 typedef size_t volatile atomic_size_t;
197 
198 template<class T>
199 inline T atomic_exchange(T volatile& _Target, T _Value)
200 {
201  return _InterlockedExchange(&_Target, _Value);
202 }
203 
204 inline long atomic_increment(long volatile & _Target)
205 {
206  return _InterlockedIncrement(&_Target);
207 }
208 
209 inline long atomic_add(long volatile & _Target, long value)
210 {
211  return _InterlockedExchangeAdd(&_Target, value) + value;
212 }
213 
214 inline size_t atomic_increment(size_t volatile & _Target)
215 {
216 #if (defined(_M_IX86) || defined(_M_ARM))
217  return static_cast<size_t>(_InterlockedIncrement(reinterpret_cast<long volatile *>(&_Target)));
218 #else
219  return static_cast<size_t>(_InterlockedIncrement64(reinterpret_cast<__int64 volatile *>(&_Target)));
220 #endif
221 }
222 
223 inline long atomic_decrement(long volatile & _Target)
224 {
225  return _InterlockedDecrement(&_Target);
226 }
227 
228 inline size_t atomic_decrement(size_t volatile & _Target)
229 {
230 #if (defined(_M_IX86) || defined(_M_ARM))
231  return static_cast<size_t>(_InterlockedDecrement(reinterpret_cast<long volatile *>(&_Target)));
232 #else
233  return static_cast<size_t>(_InterlockedDecrement64(reinterpret_cast<__int64 volatile *>(&_Target)));
234 #endif
235 }
236 
237 inline long atomic_compare_exchange(long volatile & _Target, long _Exchange, long _Comparand)
238 {
239  return _InterlockedCompareExchange(&_Target, _Exchange, _Comparand);
240 }
241 
242 inline size_t atomic_compare_exchange(size_t volatile & _Target, size_t _Exchange, size_t _Comparand)
243 {
244 #if (defined(_M_IX86) || defined(_M_ARM))
245  return static_cast<size_t>(_InterlockedCompareExchange(reinterpret_cast<long volatile *>(_Target), static_cast<long>(_Exchange), static_cast<long>(_Comparand)));
246 #else
247  return static_cast<size_t>(_InterlockedCompareExchange64(reinterpret_cast<__int64 volatile *>(_Target), static_cast<__int64>(_Exchange), static_cast<__int64>(_Comparand)));
248 #endif
249 }
250 #endif // _USE_REAL_ATOMICS
251 
252 }} // namespace pplx
253 
254 #endif // _PPLXINTERFACE_H
void(_pplx_cdecl * TaskProc_t)(void *)
An elementary abstraction for a task, defined as void (__cdecl * TaskProc_t)(void *)...
Definition: pplxinterface.h:59
The pplx namespace provides classes and functions that give you access to the Concurrency Runtime...
Definition: pplx.h:81
scheduler_ptr(std::shared_ptr< scheduler_interface > scheduler)
Creates a scheduler pointer from shared_ptr to scheduler
Definition: pplxinterface.h:79
Represents a pointer to a scheduler. This class exists to allow the the specification of a shared lif...
Definition: pplxinterface.h:74
The tasks queued to the task_group object have not completed. Note that this value is not presently r...
Definition: pplxinterface.h:137
The tasks queued to the task_group or structured_task_group object completed successfully.
Definition: pplxinterface.h:143
scheduler_ptr(_In_opt_ scheduler_interface *pScheduler)
Creates a scheduler pointer from raw pointer to scheduler
Definition: pplxinterface.h:87
scheduler_interface * operator->() const
Behave like a pointer
Definition: pplxinterface.h:94
task_group_status
Describes the execution status of a task_group or structured_task_group object. A value of this type ...
Definition: pplxinterface.h:130
The task_group or structured_task_group object was canceled. One or more tasks may not have executed...
Definition: pplxinterface.h:149