SLikeNet  0.1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RakSleep.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Oculus VR, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7  * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8  *
9  *
10  * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11  *
12  * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13  * license found in the license.txt file in the root directory of this source tree.
14  */
15 
16 #if defined(_WIN32)
17 #include "slikenet/WindowsIncludes.h" // Sleep
18 
19 
20 
21 
22 
23 
24 
25 #else
26 #include <pthread.h>
27 #include <time.h>
28 #include <sys/time.h>
29 pthread_mutex_t fakeMutex = PTHREAD_MUTEX_INITIALIZER;
30 pthread_cond_t fakeCond = PTHREAD_COND_INITIALIZER;
31 #endif
32 
33 #include "slikenet/sleep.h"
34 
35 
36 #if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT)
37 #include "ThreadEmulation.h"
38 using namespace ThreadEmulation;
39 #endif
40 
41 void RakSleep(unsigned int ms)
42 {
43 #ifdef _WIN32
44  Sleep(ms);
45 
46 
47 
48 
49 
50 
51 
52 #else
53  //Single thread sleep code thanks to Furquan Shaikh, http://somethingswhichidintknow.blogspot.com/2009/09/sleep-in-pthread.html
54  //Modified slightly from the original
55  struct timespec timeToWait;
56  struct timeval now;
57  int rt;
58 
59  gettimeofday(&now,NULL);
60 
61  long seconds = ms/1000;
62  long nanoseconds = (ms - seconds * 1000) * 1000000;
63  timeToWait.tv_sec = now.tv_sec + seconds;
64  timeToWait.tv_nsec = now.tv_usec*1000 + nanoseconds;
65 
66  if (timeToWait.tv_nsec >= 1000000000)
67  {
68  timeToWait.tv_nsec -= 1000000000;
69  timeToWait.tv_sec++;
70  }
71 
72  pthread_mutex_lock(&fakeMutex);
73  rt = pthread_cond_timedwait(&fakeCond, &fakeMutex, &timeToWait);
74  pthread_mutex_unlock(&fakeMutex);
75 #endif
76 }