/usr/include/pplx/threadpool.h is in libcpprest-dev 2.9.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /***
* 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
#include <pthread.h>
#include <vector>
#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) || defined(__ANDROID__))
#include <atomic>
#include <jni.h>
#include "pplx/pplx.h"
#endif
namespace crossplat {
#if (defined(ANDROID) || 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:
threadpool(size_t n)
: m_service(n),
m_work(m_service)
{
for (size_t i = 0; i < n; i++)
add_thread();
}
static threadpool& shared_instance();
~threadpool()
{
m_service.stop();
for (auto iter = m_threads.begin(); iter != m_threads.end(); ++iter)
{
pthread_t t = *iter;
void* res;
pthread_join(t, &res);
}
}
template<typename T>
void schedule(T task)
{
m_service.post(task);
}
boost::asio::io_service& service()
{
return m_service;
}
private:
struct _cancel_thread { };
void add_thread()
{
pthread_t t;
auto result = pthread_create(&t, nullptr, &thread_start, this);
if (result == 0)
m_threads.push_back(t);
}
void remove_thread()
{
schedule([]() -> void { throw _cancel_thread(); });
}
#if (defined(ANDROID) || defined(__ANDROID__))
static void detach_from_java(void*)
{
JVM.load()->DetachCurrentThread();
}
#endif
static void* thread_start(void *arg)
{
#if (defined(ANDROID) || defined(__ANDROID__))
// Calling get_jvm_env() here forces the thread to be attached.
get_jvm_env();
pthread_cleanup_push(detach_from_java, nullptr);
#endif
threadpool* _this = reinterpret_cast<threadpool*>(arg);
try
{
_this->m_service.run();
}
catch (const _cancel_thread&)
{
// thread was cancelled
}
catch (...)
{
// Something bad happened
#if (defined(ANDROID) || defined(__ANDROID__))
// Reach into the depths of the 'droid!
// NOTE: Uses internals of the bionic library
// Written against android ndk r9d, 7/26/2014
__pthread_cleanup_pop(&__cleanup, true);
throw;
#endif
}
#if (defined(ANDROID) || defined(__ANDROID__))
pthread_cleanup_pop(true);
#endif
return arg;
}
std::vector<pthread_t> m_threads;
boost::asio::io_service m_service;
boost::asio::io_service::work m_work;
};
}
|