forked from microsoft/cpprestsdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpplx.cpp
More file actions
158 lines (129 loc) · 3.89 KB
/
pplx.cpp
File metadata and controls
158 lines (129 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/***
* ==++==
*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ==--==
* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
*
* Parallel Patterns Library implementation (common code across platforms)
*
* For the latest on this and related APIs, please see: https://github.com/Microsoft/cpprestsdk
*
* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
****/
#include "stdafx.h"
#if !defined(_WIN32) || _MSC_VER < 1800 || CPPREST_FORCE_PPLX
#include "pplx/pplx.h"
// Disable false alarm code analyze warning
#if defined(_MSC_VER)
#pragma warning (disable : 26165 26110)
#endif
namespace pplx
{
namespace details
{
/// <summary>
/// Spin lock to allow for locks to be used in global scope
/// </summary>
class _Spin_lock
{
public:
_Spin_lock()
: _M_lock(0)
{
}
void lock()
{
if ( details::atomic_compare_exchange(_M_lock, 1l, 0l) != 0l )
{
do
{
pplx::details::platform::YieldExecution();
} while ( details::atomic_compare_exchange(_M_lock, 1l, 0l) != 0l );
}
}
void unlock()
{
// fence for release semantics
details::atomic_exchange(_M_lock, 0l);
}
private:
atomic_long _M_lock;
};
typedef ::pplx::scoped_lock<_Spin_lock> _Scoped_spin_lock;
} // namespace details
static struct _pplx_g_sched_t
{
typedef std::shared_ptr<pplx::scheduler_interface> sched_ptr;
_pplx_g_sched_t()
{
m_state = post_ctor;
}
~_pplx_g_sched_t()
{
m_state = post_dtor;
}
sched_ptr get_scheduler()
{
switch (m_state)
{
case post_ctor:
// This is the 99.9% case.
if (!m_scheduler)
{
::pplx::details::_Scoped_spin_lock lock(m_spinlock);
if (!m_scheduler)
{
m_scheduler = std::make_shared< ::pplx::default_scheduler_t>();
}
}
return m_scheduler;
default:
// This case means the global m_scheduler is not available.
// We spin off an individual scheduler instead.
return std::make_shared< ::pplx::default_scheduler_t>();
}
}
void set_scheduler(sched_ptr scheduler)
{
if (m_state == pre_ctor || m_state == post_dtor) {
throw invalid_operation("Scheduler cannot be initialized now");
}
::pplx::details::_Scoped_spin_lock lock(m_spinlock);
if (m_scheduler != nullptr)
{
throw invalid_operation("Scheduler is already initialized");
}
m_scheduler = std::move(scheduler);
}
enum
{
pre_ctor = 0,
post_ctor = 1,
post_dtor = 2
} m_state;
private:
pplx::details::_Spin_lock m_spinlock;
sched_ptr m_scheduler;
} _pplx_g_sched;
_PPLXIMP std::shared_ptr<pplx::scheduler_interface> _pplx_cdecl get_ambient_scheduler()
{
return _pplx_g_sched.get_scheduler();
}
_PPLXIMP void _pplx_cdecl set_ambient_scheduler(std::shared_ptr<pplx::scheduler_interface> _Scheduler)
{
_pplx_g_sched.set_scheduler(std::move(_Scheduler));
}
} // namespace pplx
#endif