Implement base::this_thread::yield & sleep_for().

This commit is contained in:
David Capello 2012-03-22 15:04:08 -03:00
parent c2874a063c
commit fffc32548a
3 changed files with 38 additions and 5 deletions

View File

@ -1,6 +1,18 @@
# ASEPRITE
# Copyright (C) 2001-2012 David Capello
CHECK_C_SOURCE_COMPILES("
#include <unistd.h>
int main() {
sched_yield();
return 0;
}
" HAVE_SCHED_YIELD)
if(HAVE_SCHED_YIELD)
add_definitions(-DHAVE_SCHED_YIELD)
endif()
add_library(base-lib
convert_to.cpp
errno_string.cpp

View File

@ -15,6 +15,12 @@
#include <pthread.h> // Use pthread library in Unix-like systems
#endif
#if defined(HAVE_SCHED_YIELD) && defined(_POSIX_PRIORITY_SCHEDULING)
#include <unistd.h>
#elif !defined(WIN32)
#include <sys/time.h>
#endif
namespace {
#ifdef WIN32
@ -116,17 +122,32 @@ void base::thread::details::thread_proxy(void* data)
void base::this_thread::yield()
{
#ifdef WIN32
::Sleep(0);
#elif defined(HAVE_SCHED_YIELD) && defined(_POSIX_PRIORITY_SCHEDULING)
sched_yield();
#else
// TODO
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
select(0, NULL, NULL, NULL, &timeout);
#endif
}
void base::this_thread::sleep_for(int milliseconds)
void base::this_thread::sleep_for(double seconds)
{
#ifdef WIN32
::Sleep(milliseconds);
::Sleep(seconds * 1000.0);
#else
// TODO
usleep(seconds * 1000000.0);
#endif
}

View File

@ -89,7 +89,7 @@ namespace base { // Based on C++0x threads lib
namespace this_thread
{
void yield();
void sleep_for(int milliseconds);
void sleep_for(double seconds);
}
// This class joins the thread in its destructor.