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
|
/***
* Copyright (C) Microsoft. All rights reserved.
* Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
*
*
* Simple Linux implementation of a static thread pool.
*
* For the latest on this and related APIs, please see: https://github.com/Microsoft/cpprestsdk
*
* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
***/
#pragma once
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wconversion"
#pragma clang diagnostic ignored "-Wunreachable-code"
#pragma clang diagnostic ignored "-Winfinite-recursion"
#endif
#include "boost/asio.hpp"
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
#if defined(__ANDROID__)
#include "pplx/pplx.h"
#include <atomic>
#include <jni.h>
#endif
#include "cpprest/details/cpprest_compat.h"
namespace crossplat
{
#if defined(__ANDROID__)
// IDEA: Break this section into a separate android/jni header
extern std::atomic<JavaVM*> JVM;
JNIEnv* get_jvm_env();
struct java_local_ref_deleter
{
void operator()(jobject lref) const { crossplat::get_jvm_env()->DeleteLocalRef(lref); }
};
template<class T>
using java_local_ref = std::unique_ptr<typename std::remove_pointer<T>::type, java_local_ref_deleter>;
#endif
class threadpool
{
public:
_ASYNCRTIMP static threadpool& shared_instance();
_ASYNCRTIMP static std::unique_ptr<threadpool> __cdecl construct(size_t num_threads);
virtual ~threadpool() = default;
/// <summary>
/// Initializes the cpprestsdk threadpool with a custom number of threads
/// </summary>
/// <remarks>
/// This function allows an application (in their main function) to initialize the cpprestsdk
/// threadpool with a custom threadcount. Libraries should avoid calling this function to avoid
/// a diamond problem with multiple consumers attempting to customize the pool.
/// </remarks>
/// <exception cref="std::exception">Thrown if the threadpool has already been initialized</exception>
static void initialize_with_threads(size_t num_threads);
template<typename T>
CASABLANCA_DEPRECATED("Use `.service().post(task)` directly.")
void schedule(T task)
{
service().post(task);
}
boost::asio::io_service& service() { return m_service; }
protected:
threadpool(size_t num_threads) : m_service(static_cast<int>(num_threads)) {}
boost::asio::io_service m_service;
};
} // namespace crossplat
|