Working on switching to std::string.

This commit is contained in:
Ben Vanik
2014-08-16 02:30:23 -07:00
parent 01f0b14250
commit a4dfc23abc
34 changed files with 211 additions and 250 deletions

View File

@@ -27,6 +27,7 @@
}],
['OS == "linux"', {
'sources': [
'threading_posix.cc',
],
}],
['OS == "mac"', {

View File

@@ -23,4 +23,20 @@ std::wstring to_wstring(const std::string& source) {
return converter.from_bytes(source);
}
std::string::size_type find_first_of_case(const std::string& target,
const std::string& search) {
const char* str = target.c_str();
while (*str) {
if (!strncasecmp(str, search.c_str(), search.size())) {
break;
}
str++;
}
if (*str) {
return str - target.c_str();
} else {
return std::string::npos;
}
}
} // namespace poly

View File

@@ -12,13 +12,21 @@
#include <string>
#include <poly/config.h>
#include <poly/platform.h>
#if XE_LIKE_WIN32
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#endif // XE_LIKE_WIN32
namespace poly {
std::string to_string(const std::wstring& source);
std::wstring to_wstring(const std::string& source);
std::string::size_type find_first_of_case(const std::string& target,
const std::string& search);
} // namespace poly
#endif // POLY_STRING_H_

View File

@@ -13,6 +13,7 @@
#include <chrono>
#include <cstdint>
#include <string>
#include <thread>
#include <poly/config.h>
@@ -28,6 +29,8 @@ uint32_t current_thread_id();
// Sets the current thread name.
void set_name(const std::string& name);
// Sets the target thread name.
void set_name(std::thread::native_handle_type handle, const std::string& name);
// Yields the current thread to the scheduler. Maybe.
void Yield();

View File

@@ -25,11 +25,11 @@ uint32_t current_thread_id() {
}
void set_name(const std::string& name) {
#if XE_LIKE_OSX
pthread_setname_np(name.c_str());
#else
pthread_setname_np(pthread_self(), name.c_str());
#endif // XE_LIKE_OSX
}
void set_name(std::thread::native_handle_type handle, const std::string& name) {
// ?
}
void Yield() { pthread_yield_np(); }

View File

@@ -0,0 +1,42 @@
/**
******************************************************************************
* 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 {
//uint64_t ticks() { return mach_absolute_time(); }
// uint32_t current_thread_id() {
// mach_port_t tid = pthread_mach_thread_np(pthread_self());
// return static_cast<uint32_t>(tid);
// }
void set_name(const std::string& name) {
pthread_setname_np(pthread_self(), name.c_str());
}
void set_name(std::thread::native_handle_type handle, const std::string& name) {
pthread_setname_np(pthread_self(), name.c_str());
}
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

View File

@@ -57,6 +57,10 @@ void set_name(const std::string& name) {
set_name(static_cast<DWORD>(-1), name);
}
void set_name(std::thread::native_handle_type handle, const std::string& name) {
set_name(GetThreadId(handle), name);
}
void Yield() { SwitchToThread(); }
void Sleep(std::chrono::microseconds duration) {