Moving threading utils to poly.

This commit is contained in:
Ben Vanik
2014-07-10 23:51:28 -07:00
parent 9031d5f4a4
commit f24b45a07c
9 changed files with 119 additions and 14 deletions

View File

@@ -12,6 +12,7 @@
#include <poly/cxx_compat.h>
#include <poly/math.h>
#include <poly/threading.h>
namespace poly {} // namespace poly

View File

@@ -6,6 +6,7 @@
'poly-private.h',
'poly.cc',
'poly.h',
'threading.h',
],
'conditions': [
@@ -19,10 +20,12 @@
}],
['OS == "mac"', {
'sources': [
'threading_mac.cc',
],
}],
['OS == "win"', {
'sources': [
'threading_win.cc',
],
}],
],

36
src/poly/threading.h Normal file
View File

@@ -0,0 +1,36 @@
/**
******************************************************************************
* 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_
#include <chrono>
#include <cstdint>
namespace poly {
namespace threading {
// Gets a stable thread-specific ID, but may not be. Use for informative
// purposes only.
uint32_t current_thread_id();
// Yields the current thread to the scheduler. Maybe.
void Yield();
// 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_

32
src/poly/threading_mac.cc Normal file
View File

@@ -0,0 +1,32 @@
/**
******************************************************************************
* 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. *
******************************************************************************
*/
#include <poly/threading.h>
#include <pthread.h>
#include <time.h>
namespace poly {
namespace threading {
uint32_t current_thread_id() {
mach_port_t tid = pthread_mach_thread_np(pthread_self());
return static_cast<uint32_t>(tid);
}
void Yield() { pthread_yield_np(); }
void Sleep(std::chrono::microseconds duration) {
timespec rqtp = {duration.count() / 1000000, duration.count() % 1000};
nanosleep(&rqtp, nullptr);
// TODO(benvanik): spin while rmtp >0?
}
} // namespace threading
} // namespace poly

30
src/poly/threading_win.cc Normal file
View File

@@ -0,0 +1,30 @@
/**
******************************************************************************
* 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. *
******************************************************************************
*/
#include <poly/threading.h>
namespace poly {
namespace threading {
uint32_t current_thread_id() {
return static_cast<uint32_t>(GetCurrentThreadId());
}
void Yield() { SwitchToThread(); }
void Sleep(std::chrono::microseconds duration) {
if (duration.count() < 100) {
SwitchToThread();
} else {
Sleep(duration.count() / 1000);
}
}
} // namespace threading
} // namespace poly