2017-02-06 22:43:55 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00: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 22:43:55 +00: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 19:54:29 +00:00
|
|
|
#if defined(OS_WIN)
|
2021-06-29 13:51:11 +00: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 19:54:29 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
#include "port/win/win_thread.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
2022-10-24 23:56:01 +00:00
|
|
|
#include <process.h> // __beginthreadex
|
2017-05-05 23:09:24 +00:00
|
|
|
#include <windows.h>
|
2017-02-06 22:43:55 +00:00
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <system_error>
|
|
|
|
#include <thread>
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2017-02-06 22:43:55 +00:00
|
|
|
namespace port {
|
|
|
|
|
|
|
|
struct WindowsThread::Data {
|
|
|
|
std::function<void()> func_;
|
2022-10-24 23:56:01 +00:00
|
|
|
uintptr_t handle_;
|
2017-02-06 22:43:55 +00:00
|
|
|
|
2022-10-24 23:56:01 +00:00
|
|
|
Data(std::function<void()>&& func) : func_(std::move(func)), handle_(0) {}
|
2017-02-06 22:43:55 +00:00
|
|
|
|
|
|
|
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 21:41:35 +00:00
|
|
|
data_ = std::make_shared<Data>(std::move(func));
|
2018-11-09 19:17:34 +00:00
|
|
|
// We create another instance of std::shared_ptr to get an additional ref
|
2017-12-07 21:41:35 +00: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
|
2022-10-24 23:56:01 +00:00
|
|
|
std::unique_ptr<std::shared_ptr<Data>> th_data(
|
|
|
|
new std::shared_ptr<Data>(data_));
|
2017-02-06 22:43:55 +00:00
|
|
|
|
|
|
|
data_->handle_ = _beginthreadex(NULL,
|
2022-10-24 23:56:01 +00:00
|
|
|
0, // stack size
|
|
|
|
&Data::ThreadProc, th_data.get(),
|
|
|
|
0, // init flag
|
|
|
|
&th_id_);
|
2017-02-06 22:43:55 +00:00
|
|
|
|
|
|
|
if (data_->handle_ == 0) {
|
2022-10-24 23:56:01 +00:00
|
|
|
throw std::system_error(
|
|
|
|
std::make_error_code(std::errc::resource_unavailable_try_again),
|
|
|
|
"Unable to create a thread");
|
2017-02-06 22:43:55 +00:00
|
|
|
}
|
2017-12-07 21:41:35 +00:00
|
|
|
th_data.release();
|
2017-02-06 22:43:55 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 23:56:01 +00:00
|
|
|
WindowsThread::WindowsThread() : data_(nullptr), th_id_(0) {}
|
2017-02-06 22:43:55 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 23:56:01 +00:00
|
|
|
WindowsThread::WindowsThread(WindowsThread&& o) noexcept : WindowsThread() {
|
2017-02-06 22:43:55 +00:00
|
|
|
*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;
|
|
|
|
}
|
|
|
|
|
2022-10-24 23:56:01 +00:00
|
|
|
bool WindowsThread::joinable() const { return (data_ && data_->handle_ != 0); }
|
2017-02-06 22:43:55 +00:00
|
|
|
|
|
|
|
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);
|
2022-10-24 23:56:01 +00:00
|
|
|
throw std::system_error(std::make_error_code(std::errc::invalid_argument),
|
|
|
|
"Thread is no longer joinable");
|
2017-02-06 22:43:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (GetThreadId(GetCurrentThread()) == th_id_) {
|
|
|
|
assert(false);
|
|
|
|
throw std::system_error(
|
2022-10-24 23:56:01 +00:00
|
|
|
std::make_error_code(std::errc::resource_deadlock_would_occur),
|
|
|
|
"Can not join itself");
|
2017-02-06 22:43:55 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 23:56:01 +00:00
|
|
|
auto ret =
|
|
|
|
WaitForSingleObject(reinterpret_cast<HANDLE>(data_->handle_), INFINITE);
|
2017-02-06 22:43:55 +00:00
|
|
|
if (ret != WAIT_OBJECT_0) {
|
|
|
|
auto lastError = GetLastError();
|
|
|
|
assert(false);
|
2022-10-24 23:56:01 +00:00
|
|
|
throw std::system_error(static_cast<int>(lastError), std::system_category(),
|
|
|
|
"WaitForSingleObjectFailed: thread join");
|
2017-02-06 22:43:55 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 05:40:16 +00:00
|
|
|
BOOL rc
|
|
|
|
#if defined(_MSC_VER)
|
2022-10-24 23:56:01 +00:00
|
|
|
= FALSE;
|
2019-11-27 05:40:16 +00:00
|
|
|
#else
|
2022-10-24 23:56:01 +00:00
|
|
|
__attribute__((__unused__));
|
2019-11-27 05:40:16 +00:00
|
|
|
#endif
|
2018-03-21 01:33:44 +00:00
|
|
|
rc = CloseHandle(reinterpret_cast<HANDLE>(data_->handle_));
|
|
|
|
assert(rc != 0);
|
2017-02-06 22:43:55 +00:00
|
|
|
data_->handle_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WindowsThread::detach() {
|
|
|
|
if (!joinable()) {
|
|
|
|
assert(false);
|
2022-10-24 23:56:01 +00:00
|
|
|
throw std::system_error(std::make_error_code(std::errc::invalid_argument),
|
|
|
|
"Thread is no longer available");
|
2017-02-06 22:43:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ret = CloseHandle(reinterpret_cast<HANDLE>(data_->handle_));
|
|
|
|
data_->handle_ = 0;
|
|
|
|
|
2018-03-21 01:33:44 +00:00
|
|
|
return (ret != 0);
|
2017-02-06 22:43:55 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 23:56:01 +00:00
|
|
|
void WindowsThread::swap(WindowsThread& o) {
|
2017-02-06 22:43:55 +00:00
|
|
|
data_.swap(o.data_);
|
|
|
|
std::swap(th_id_, o.th_id_);
|
|
|
|
}
|
|
|
|
|
2022-10-24 23:56:01 +00:00
|
|
|
unsigned int __stdcall WindowsThread::Data::ThreadProc(void* arg) {
|
2017-12-07 21:41:35 +00:00
|
|
|
auto ptr = reinterpret_cast<std::shared_ptr<Data>*>(arg);
|
|
|
|
std::unique_ptr<std::shared_ptr<Data>> data(ptr);
|
|
|
|
(*data)->func_();
|
2017-02-06 22:43:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-10-24 23:56:01 +00:00
|
|
|
} // namespace port
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2020-09-23 19:54:29 +00:00
|
|
|
|
2021-06-29 13:51:11 +00:00
|
|
|
#endif // !_POSIX_THREADS
|
2021-10-27 20:23:45 +00:00
|
|
|
#endif // OS_WIN
|