• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

RakSleep.cpp

Go to the documentation of this file.
00001 #if defined(_XBOX) || defined(X360)
00002                             
00003 #elif defined(_WIN32)
00004 #include "WindowsIncludes.h" // Sleep
00005 #elif defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00006                                                 
00007 #else
00008 #include <pthread.h>
00009 #include <time.h>
00010 #include <sys/time.h>
00011 #endif
00012 
00013 
00014 #include "RakSleep.h"
00015 
00016 void RakSleep(unsigned int ms)
00017 {
00018 #ifdef _WIN32
00019         Sleep(ms);
00020 #elif defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00021                                                                                             
00022 #else
00023         //Single thread sleep code thanks to Furquan Shaikh, http://somethingswhichidintknow.blogspot.com/2009/09/sleep-in-pthread.html
00024         //Modified slightly from the original
00025         pthread_mutex_t fakeMutex = PTHREAD_MUTEX_INITIALIZER;
00026         pthread_cond_t fakeCond = PTHREAD_COND_INITIALIZER;
00027         struct timespec timeToWait;
00028         struct timeval now;
00029         int rt;
00030 
00031         gettimeofday(&now,NULL);
00032 
00033         long seconds = ms/1000;
00034         long nanoseconds = (ms - seconds * 1000) * 1000000;
00035         timeToWait.tv_sec = now.tv_sec + seconds;
00036         timeToWait.tv_nsec = now.tv_usec*1000 + nanoseconds;
00037         
00038         if (timeToWait.tv_nsec >= 1000000000)
00039         {
00040                 timeToWait.tv_nsec -= 1000000000;
00041                 timeToWait.tv_sec++;
00042         }
00043 
00044         pthread_mutex_lock(&fakeMutex);
00045         rt = pthread_cond_timedwait(&fakeCond, &fakeMutex, &timeToWait);
00046         pthread_mutex_unlock(&fakeMutex);
00047 #endif
00048 }

Generated on Thu Sep 30 2010 01:27:26 for RakNet by  doxygen 1.7.1