2014-07-11 08:51:28 +02:00
|
|
|
/**
|
|
|
|
|
******************************************************************************
|
|
|
|
|
* Xenia : Xbox 360 Emulator Research Project *
|
|
|
|
|
******************************************************************************
|
|
|
|
|
* Copyright 2014 Ben Vanik. All rights reserved. *
|
|
|
|
|
* Released under the BSD license - see LICENSE in the root for more details. *
|
|
|
|
|
******************************************************************************
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef POLY_THREADING_H_
|
|
|
|
|
#define POLY_THREADING_H_
|
|
|
|
|
|
2014-12-21 07:17:57 +01:00
|
|
|
#include <atomic>
|
2014-07-11 08:51:28 +02:00
|
|
|
#include <chrono>
|
2014-12-21 07:17:57 +01:00
|
|
|
#include <condition_variable>
|
2014-07-11 08:51:28 +02:00
|
|
|
#include <cstdint>
|
2014-12-21 07:17:57 +01:00
|
|
|
#include <mutex>
|
2014-08-16 10:36:45 +02:00
|
|
|
#include <string>
|
2014-08-16 11:30:23 +02:00
|
|
|
#include <thread>
|
2014-07-11 08:51:28 +02:00
|
|
|
|
2014-07-12 03:03:35 +02:00
|
|
|
|
2014-07-11 08:51:28 +02:00
|
|
|
namespace poly {
|
|
|
|
|
namespace threading {
|
|
|
|
|
|
2014-12-21 07:17:57 +01:00
|
|
|
class Fence {
|
|
|
|
|
public:
|
|
|
|
|
Fence() : signaled_(false) {}
|
|
|
|
|
void Signal() {
|
|
|
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
|
|
|
|
signaled_.store(true);
|
|
|
|
|
cond_.notify_all();
|
|
|
|
|
}
|
|
|
|
|
void Wait() {
|
|
|
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
|
|
|
|
while (!signaled_.load()) {
|
|
|
|
|
cond_.wait(lock);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::mutex mutex_;
|
|
|
|
|
std::condition_variable cond_;
|
|
|
|
|
std::atomic<bool> signaled_;
|
|
|
|
|
};
|
|
|
|
|
|
2014-07-18 05:43:27 +02:00
|
|
|
// Gets the current high-performance tick count.
|
2014-07-13 06:52:33 +02:00
|
|
|
uint64_t ticks();
|
2015-02-21 20:19:00 +01:00
|
|
|
uint64_t ticks_per_second();
|
2014-07-13 06:52:33 +02:00
|
|
|
|
2014-10-29 06:10:40 +01:00
|
|
|
// TODO(benvanik): processor info API.
|
|
|
|
|
|
2014-07-11 08:51:28 +02:00
|
|
|
// Gets a stable thread-specific ID, but may not be. Use for informative
|
|
|
|
|
// purposes only.
|
|
|
|
|
uint32_t current_thread_id();
|
|
|
|
|
|
2014-08-16 10:36:45 +02:00
|
|
|
// Sets the current thread name.
|
|
|
|
|
void set_name(const std::string& name);
|
2014-08-16 11:30:23 +02:00
|
|
|
// Sets the target thread name.
|
|
|
|
|
void set_name(std::thread::native_handle_type handle, const std::string& name);
|
2014-08-16 10:36:45 +02:00
|
|
|
|
2014-07-11 08:51:28 +02:00
|
|
|
// Yields the current thread to the scheduler. Maybe.
|
2014-12-21 07:17:57 +01:00
|
|
|
void MaybeYield();
|
2014-07-11 08:51:28 +02:00
|
|
|
|
|
|
|
|
// Sleeps the current thread for at least as long as the given duration.
|
|
|
|
|
void Sleep(std::chrono::microseconds duration);
|
|
|
|
|
template <typename Rep, typename Period>
|
|
|
|
|
void Sleep(std::chrono::duration<Rep, Period> duration) {
|
|
|
|
|
Sleep(std::chrono::duration_cast<std::chrono::microseconds>(duration));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace threading
|
|
|
|
|
} // namespace poly
|
|
|
|
|
|
|
|
|
|
#endif // POLY_THREADING_H_
|