2017-02-06 23:43:55 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2017-02-06 23:43:55 +01:00
|
|
|
//
|
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
2020-09-23 21:54:29 +02:00
|
|
|
#if defined(OS_WIN)
|
2021-06-29 15:51:11 +02:00
|
|
|
// Most Mingw builds support std::thread only when using posix threads.
|
|
|
|
// In that case, some of these functions will be unavailable.
|
|
|
|
// Note that we're using either WindowsThread or std::thread, depending on
|
|
|
|
// which one is available.
|
|
|
|
#ifndef _POSIX_THREADS
|
2020-09-23 21:54:29 +02:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
#include "port/win/win_thread.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <process.h> // __beginthreadex
|
2017-05-06 01:09:24 +02:00
|
|
|
#include <windows.h>
|
2017-02-06 23:43:55 +01:00
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <system_error>
|
|
|
|
#include <thread>
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2017-02-06 23:43:55 +01:00
|
|
|
namespace port {
|
|
|
|
|
|
|
|
struct WindowsThread::Data {
|
|
|
|
|
|
|
|
std::function<void()> func_;
|
|
|
|
uintptr_t handle_;
|
|
|
|
|
|
|
|
Data(std::function<void()>&& func) :
|
|
|
|
func_(std::move(func)),
|
|
|
|
handle_(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Data(const Data&) = delete;
|
|
|
|
Data& operator=(const Data&) = delete;
|
|
|
|
|
|
|
|
static unsigned int __stdcall ThreadProc(void* arg);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void WindowsThread::Init(std::function<void()>&& func) {
|
|
|
|
|
2017-12-07 22:41:35 +01:00
|
|
|
data_ = std::make_shared<Data>(std::move(func));
|
2018-11-09 20:17:34 +01:00
|
|
|
// We create another instance of std::shared_ptr to get an additional ref
|
2017-12-07 22:41:35 +01:00
|
|
|
// since we may detach and destroy this instance before the threadproc
|
|
|
|
// may start to run. We choose to allocate this additional ref on the heap
|
|
|
|
// so we do not need to synchronize and allow this thread to proceed
|
|
|
|
std::unique_ptr<std::shared_ptr<Data>> th_data(new std::shared_ptr<Data>(data_));
|
2017-02-06 23:43:55 +01:00
|
|
|
|
|
|
|
data_->handle_ = _beginthreadex(NULL,
|
|
|
|
0, // stack size
|
|
|
|
&Data::ThreadProc,
|
2017-12-07 22:41:35 +01:00
|
|
|
th_data.get(),
|
2017-02-06 23:43:55 +01:00
|
|
|
0, // init flag
|
|
|
|
&th_id_);
|
|
|
|
|
|
|
|
if (data_->handle_ == 0) {
|
|
|
|
throw std::system_error(std::make_error_code(
|
|
|
|
std::errc::resource_unavailable_try_again),
|
|
|
|
"Unable to create a thread");
|
|
|
|
}
|
2017-12-07 22:41:35 +01:00
|
|
|
th_data.release();
|
2017-02-06 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WindowsThread::WindowsThread() :
|
|
|
|
data_(nullptr),
|
|
|
|
th_id_(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
WindowsThread::~WindowsThread() {
|
|
|
|
// Must be joined or detached
|
|
|
|
// before destruction.
|
|
|
|
// This is the same as std::thread
|
|
|
|
if (data_) {
|
|
|
|
if (joinable()) {
|
|
|
|
assert(false);
|
|
|
|
std::terminate();
|
|
|
|
}
|
|
|
|
data_.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WindowsThread::WindowsThread(WindowsThread&& o) noexcept :
|
|
|
|
WindowsThread() {
|
|
|
|
*this = std::move(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
WindowsThread& WindowsThread::operator=(WindowsThread&& o) noexcept {
|
|
|
|
|
|
|
|
if (joinable()) {
|
|
|
|
assert(false);
|
|
|
|
std::terminate();
|
|
|
|
}
|
|
|
|
|
|
|
|
data_ = std::move(o.data_);
|
|
|
|
|
|
|
|
// Per spec both instances will have the same id
|
|
|
|
th_id_ = o.th_id_;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WindowsThread::joinable() const {
|
|
|
|
return (data_ && data_->handle_ != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
WindowsThread::native_handle_type WindowsThread::native_handle() const {
|
|
|
|
return reinterpret_cast<native_handle_type>(data_->handle_);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned WindowsThread::hardware_concurrency() {
|
|
|
|
return std::thread::hardware_concurrency();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WindowsThread::join() {
|
|
|
|
|
|
|
|
if (!joinable()) {
|
|
|
|
assert(false);
|
|
|
|
throw std::system_error(
|
|
|
|
std::make_error_code(std::errc::invalid_argument),
|
|
|
|
"Thread is no longer joinable");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetThreadId(GetCurrentThread()) == th_id_) {
|
|
|
|
assert(false);
|
|
|
|
throw std::system_error(
|
|
|
|
std::make_error_code(std::errc::resource_deadlock_would_occur),
|
|
|
|
"Can not join itself");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ret = WaitForSingleObject(reinterpret_cast<HANDLE>(data_->handle_),
|
|
|
|
INFINITE);
|
|
|
|
if (ret != WAIT_OBJECT_0) {
|
|
|
|
auto lastError = GetLastError();
|
|
|
|
assert(false);
|
|
|
|
throw std::system_error(static_cast<int>(lastError),
|
|
|
|
std::system_category(),
|
2017-12-07 22:41:35 +01:00
|
|
|
"WaitForSingleObjectFailed: thread join");
|
2017-02-06 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
2019-11-27 06:40:16 +01:00
|
|
|
BOOL rc
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
= FALSE;
|
|
|
|
#else
|
|
|
|
__attribute__((__unused__));
|
|
|
|
#endif
|
2018-03-21 02:33:44 +01:00
|
|
|
rc = CloseHandle(reinterpret_cast<HANDLE>(data_->handle_));
|
|
|
|
assert(rc != 0);
|
2017-02-06 23:43:55 +01:00
|
|
|
data_->handle_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WindowsThread::detach() {
|
|
|
|
|
|
|
|
if (!joinable()) {
|
|
|
|
assert(false);
|
|
|
|
throw std::system_error(
|
|
|
|
std::make_error_code(std::errc::invalid_argument),
|
|
|
|
"Thread is no longer available");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ret = CloseHandle(reinterpret_cast<HANDLE>(data_->handle_));
|
|
|
|
data_->handle_ = 0;
|
|
|
|
|
2018-03-21 02:33:44 +01:00
|
|
|
return (ret != 0);
|
2017-02-06 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowsThread::swap(WindowsThread& o) {
|
|
|
|
data_.swap(o.data_);
|
|
|
|
std::swap(th_id_, o.th_id_);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int __stdcall WindowsThread::Data::ThreadProc(void* arg) {
|
2017-12-07 22:41:35 +01:00
|
|
|
auto ptr = reinterpret_cast<std::shared_ptr<Data>*>(arg);
|
|
|
|
std::unique_ptr<std::shared_ptr<Data>> data(ptr);
|
|
|
|
(*data)->func_();
|
2017-02-06 23:43:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace port
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2020-09-23 21:54:29 +02:00
|
|
|
|
2021-06-29 15:51:11 +02:00
|
|
|
#endif // OS_WIN
|
|
|
|
#endif // !_POSIX_THREADS
|