]> git.mxchange.org Git - simgear.git/blob - simgear/threads/SGThread.cxx
Attempt to resolve ambiguity between #include <config.h> for simgear vs.
[simgear.git] / simgear / threads / SGThread.cxx
1 #include <simgear/compiler.h>
2 #include <sys/time.h>
3
4 #include "SGThread.hxx"
5
6 void*
7 start_handler( void* arg )
8 {
9     SGThread* thr = static_cast<SGThread*>(arg);
10     thr->run();
11     return 0;
12 }
13
14 void
15 SGThread::set_cancel( cancel_t mode )
16 {
17     switch (mode)
18     {
19     case CANCEL_DISABLE:
20         pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, 0 );
21         break;
22     case CANCEL_DEFERRED:
23         pthread_setcanceltype( PTHREAD_CANCEL_DEFERRED, 0 );
24         pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, 0 );
25         break;
26     case CANCEL_IMMEDIATE:
27         pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, 0 );
28         pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, 0 );
29         break;
30     default:
31         break;
32     }
33 }
34
35 bool
36 SGMutex::trylock()
37 {
38     int status = pthread_mutex_lock( &mutex );
39     if (status == EBUSY)
40     {
41         return false;
42     }
43     assert( status == 0 );
44     return true;
45 }
46
47 bool
48 SGCondition::wait( SGMutex& mutex, unsigned long ms )
49 {
50     struct timeval now;
51     ::gettimeofday( &now, 0 );
52
53     // Wait time is now + ms milliseconds
54     unsigned int sec = ms / 1000;
55     unsigned int nsec = (ms % 1000) * 1000;
56     struct timespec abstime;
57     abstime.tv_sec = now.tv_sec + sec;
58     abstime.tv_nsec = now.tv_usec*1000 + nsec;
59
60     int status = pthread_cond_timedwait( &cond, &mutex.mutex, &abstime );
61     if (status == ETIMEDOUT)
62     {
63         return false;
64     }
65
66     assert( status == 0 );
67     return true;
68 }
69