]> git.mxchange.org Git - simgear.git/blob - simgear/threads/SGThread.cxx
27f560822610593df7c70dec98a5a5cb319d6c4c
[simgear.git] / simgear / threads / SGThread.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4
5 #include <simgear/compiler.h>
6 #include <sys/time.h>
7
8 #include "SGThread.hxx"
9
10 void*
11 start_handler( void* arg )
12 {
13     SGThread* thr = static_cast<SGThread*>(arg);
14     thr->run();
15     return 0;
16 }
17
18 bool
19 SGMutex::trylock()
20 {
21     int status = pthread_mutex_lock( &mutex );
22     if (status == EBUSY)
23     {
24         return false;
25     }
26     assert( status == 0 );
27     return true;
28 }
29
30 bool
31 SGCondition::wait( SGMutex& mutex, unsigned long ms )
32 {
33     struct timeval now;
34     ::gettimeofday( &now, 0 );
35
36     // Wait time is now + ms milliseconds
37     unsigned int sec = ms / 1000;
38     unsigned int nsec = (ms % 1000) * 1000;
39     struct timespec abstime;
40     abstime.tv_sec = now.tv_sec + sec;
41     abstime.tv_nsec = now.tv_usec*1000 + nsec;
42
43     int status = pthread_cond_timedwait( &cond, &mutex.mutex, &abstime );
44     if (status == ETIMEDOUT)
45     {
46         return false;
47     }
48
49     assert( status == 0 );
50     return true;
51 }
52