Skip to content

Export methods to set/get the ambient scheduler in cpprest dll #670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Release/include/pplx/pplxtasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,27 @@
#ifndef _PPLXTASKS_H
#define _PPLXTASKS_H

#include "cpprest/details/cpprest_compat.h"

#if (defined(_MSC_VER) && (_MSC_VER >= 1800)) && !CPPREST_FORCE_PPLX
#include <ppltasks.h>
namespace pplx = Concurrency;

namespace Concurrency
{

/// <summary>
/// Sets the ambient scheduler to be used by the PPL constructs.
/// </summary>
_ASYNCRTIMP void __cdecl set_cpprestsdk_ambient_scheduler(const std::shared_ptr<scheduler_interface>& _Scheduler);

/// <summary>
/// Gets the ambient scheduler to be used by the PPL constructs
/// </summary>
_ASYNCRTIMP const std::shared_ptr<scheduler_interface>& __cdecl get_cpprestsdk_ambient_scheduler();

} // namespace Concurrency

#if (_MSC_VER >= 1900)
#include <concrt.h>
namespace Concurrency {
Expand Down
1 change: 1 addition & 0 deletions Release/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ elseif(CPPREST_PPLX_IMPL STREQUAL "linux")
install(FILES ../include/pplx/threadpool.h DESTINATION include/pplx)
endif()
elseif(CPPREST_PPLX_IMPL STREQUAL "win")
target_sources(cpprest PRIVATE pplx/pplxwin.cpp)
if(CPPREST_WEBSOCKETS_IMPL STREQUAL "wspp")
target_sources(cpprest PRIVATE pplx/threadpool.cpp ../include/pplx/threadpool.h)
if(CPPREST_INSTALL_HEADERS)
Expand Down
15 changes: 15 additions & 0 deletions Release/src/pplx/pplxwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,19 @@ namespace details

} // namespace pplx

#else
namespace Concurrency
{

void __cdecl set_cpprestsdk_ambient_scheduler(const std::shared_ptr<scheduler_interface>& _Scheduler)
{
pplx::set_ambient_scheduler(_Scheduler);
}

const std::shared_ptr<scheduler_interface>& __cdecl get_cpprestsdk_ambient_scheduler()
{
return pplx::get_ambient_scheduler();
}

} // namespace pplx
#endif
37 changes: 37 additions & 0 deletions Release/tests/functional/http/client/outside_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,43 @@ TEST_FIXTURE(uri_address, multiple_https_requests)
});
}

#if (defined(_MSC_VER) && (_MSC_VER >= 1800)) && !CPPREST_FORCE_PPLX
TEST_FIXTURE(uri_address, multiple_https_requests_sync_scheduler)
{
struct sync_scheduler : public scheduler_interface
{
public:
virtual void schedule(TaskProc_t function, PVOID context) override
{
function(context);
}
};

// Save the current ambient scheduler
const auto scheduler = get_cpprestsdk_ambient_scheduler();

// Change the ambient scheduler to one that schedules synchronously
static std::shared_ptr<scheduler_interface> syncScheduler = std::make_shared<sync_scheduler>();
set_cpprestsdk_ambient_scheduler(syncScheduler);

handle_timeout([&] {
// Use code.google.com instead of www.google.com, which redirects
http_client client(U("https://code.google.com"));

http_response response;
for (int i = 0; i < 5; ++i)
{
response = client.request(methods::GET).get();
VERIFY_ARE_EQUAL(status_codes::OK, response.status_code());
response.content_ready().wait();
}
});

// Revert to the original scheduler
set_cpprestsdk_ambient_scheduler(scheduler);
}
#endif

TEST_FIXTURE(uri_address, reading_google_stream)
{
handle_timeout([&]
Expand Down