Skip to content

Commit e5c6d84

Browse files
chakrab-msftBillyONeal
authored andcommitted
Export methods to set/get the ambient scheduler in cpprest dll (#670)
* Export methods to set/get the ambient scheduler in cpprest dll * move the definiation of get/set_cpprestsdk_ambient_scheduler to pplxwin.cpp * Add test for http request with ambient scheduler
1 parent b3a7141 commit e5c6d84

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

Release/include/pplx/pplxtasks.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,27 @@
1616
#ifndef _PPLXTASKS_H
1717
#define _PPLXTASKS_H
1818

19+
#include "cpprest/details/cpprest_compat.h"
20+
1921
#if (defined(_MSC_VER) && (_MSC_VER >= 1800)) && !CPPREST_FORCE_PPLX
2022
#include <ppltasks.h>
2123
namespace pplx = Concurrency;
24+
25+
namespace Concurrency
26+
{
27+
28+
/// <summary>
29+
/// Sets the ambient scheduler to be used by the PPL constructs.
30+
/// </summary>
31+
_ASYNCRTIMP void __cdecl set_cpprestsdk_ambient_scheduler(const std::shared_ptr<scheduler_interface>& _Scheduler);
32+
33+
/// <summary>
34+
/// Gets the ambient scheduler to be used by the PPL constructs
35+
/// </summary>
36+
_ASYNCRTIMP const std::shared_ptr<scheduler_interface>& __cdecl get_cpprestsdk_ambient_scheduler();
37+
38+
} // namespace Concurrency
39+
2240
#if (_MSC_VER >= 1900)
2341
#include <concrt.h>
2442
namespace Concurrency {

Release/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ elseif(CPPREST_PPLX_IMPL STREQUAL "linux")
103103
install(FILES ../include/pplx/threadpool.h DESTINATION include/pplx)
104104
endif()
105105
elseif(CPPREST_PPLX_IMPL STREQUAL "win")
106+
target_sources(cpprest PRIVATE pplx/pplxwin.cpp)
106107
if(CPPREST_WEBSOCKETS_IMPL STREQUAL "wspp")
107108
target_sources(cpprest PRIVATE pplx/threadpool.cpp ../include/pplx/threadpool.h)
108109
if(CPPREST_INSTALL_HEADERS)

Release/src/pplx/pplxwin.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,19 @@ namespace details
275275

276276
} // namespace pplx
277277

278+
#else
279+
namespace Concurrency
280+
{
281+
282+
void __cdecl set_cpprestsdk_ambient_scheduler(const std::shared_ptr<scheduler_interface>& _Scheduler)
283+
{
284+
pplx::set_ambient_scheduler(_Scheduler);
285+
}
286+
287+
const std::shared_ptr<scheduler_interface>& __cdecl get_cpprestsdk_ambient_scheduler()
288+
{
289+
return pplx::get_ambient_scheduler();
290+
}
291+
292+
} // namespace pplx
278293
#endif

Release/tests/functional/http/client/outside_tests.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,43 @@ TEST_FIXTURE(uri_address, multiple_https_requests)
111111
});
112112
}
113113

114+
#if (defined(_MSC_VER) && (_MSC_VER >= 1800)) && !CPPREST_FORCE_PPLX
115+
TEST_FIXTURE(uri_address, multiple_https_requests_sync_scheduler)
116+
{
117+
struct sync_scheduler : public scheduler_interface
118+
{
119+
public:
120+
virtual void schedule(TaskProc_t function, PVOID context) override
121+
{
122+
function(context);
123+
}
124+
};
125+
126+
// Save the current ambient scheduler
127+
const auto scheduler = get_cpprestsdk_ambient_scheduler();
128+
129+
// Change the ambient scheduler to one that schedules synchronously
130+
static std::shared_ptr<scheduler_interface> syncScheduler = std::make_shared<sync_scheduler>();
131+
set_cpprestsdk_ambient_scheduler(syncScheduler);
132+
133+
handle_timeout([&] {
134+
// Use code.google.com instead of www.google.com, which redirects
135+
http_client client(U("https://code.google.com"));
136+
137+
http_response response;
138+
for (int i = 0; i < 5; ++i)
139+
{
140+
response = client.request(methods::GET).get();
141+
VERIFY_ARE_EQUAL(status_codes::OK, response.status_code());
142+
response.content_ready().wait();
143+
}
144+
});
145+
146+
// Revert to the original scheduler
147+
set_cpprestsdk_ambient_scheduler(scheduler);
148+
}
149+
#endif
150+
114151
TEST_FIXTURE(uri_address, reading_google_stream)
115152
{
116153
handle_timeout([&]

0 commit comments

Comments
 (0)