ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / VP_SDK / VP_Os / linux / vp_os_signal.c
1 /**
2  * @file signal.c
3  * @author aurelien.morelle@parrot.fr
4  * @date 2006/12/15
5  */
6
7 #include "VP_Os/vp_os_signal.h"
8
9 #ifndef __USE_GNU
10 #define __USE_GNU
11 #endif
12
13 #include <sys/time.h>
14 #include <errno.h>
15
16
17 void
18 vp_os_mutex_init(vp_os_mutex_t *mutex)
19 {
20   pthread_mutex_init((pthread_mutex_t *)mutex, NULL);
21 }
22
23
24 void
25 vp_os_mutex_destroy(vp_os_mutex_t *mutex)
26 {
27   pthread_mutex_destroy((pthread_mutex_t *)mutex);
28 }
29
30
31 void
32 vp_os_mutex_lock(vp_os_mutex_t *mutex)
33 {
34   pthread_mutex_lock((pthread_mutex_t *)mutex);
35 }
36
37 C_RESULT
38 vp_os_mutex_trylock(vp_os_mutex_t *mutex)
39 {
40   return pthread_mutex_trylock((pthread_mutex_t *)mutex) ? C_FAIL : C_OK;
41 }
42
43
44 void
45 vp_os_mutex_unlock(vp_os_mutex_t *mutex)
46 {
47   pthread_mutex_unlock((pthread_mutex_t *)mutex);
48 }
49
50
51 void
52 vp_os_cond_init(vp_os_cond_t *cond, vp_os_mutex_t *mutex)
53 {
54   pthread_cond_init(&cond->cond, NULL);
55   cond->mutex = mutex;
56 }
57
58
59 void
60 vp_os_cond_destroy(vp_os_cond_t *cond)
61 {
62   pthread_cond_destroy(&cond->cond);
63 }
64
65
66 void
67 vp_os_cond_wait(vp_os_cond_t *cond)
68 {
69   pthread_cond_wait(&cond->cond, (pthread_mutex_t *)cond->mutex);
70 }
71
72 #ifdef USE_ANDROID
73 #ifndef TIMEVAL_TO_TIMESPEC
74 #define TIMEVAL_TO_TIMESPEC(tv, ts) do {                            \
75     (ts)->tv_sec = (tv)->tv_sec;                                    \
76     (ts)->tv_nsec = (tv)->tv_usec * 1000;                           \
77 } while (FALSE)
78 #endif
79 #endif
80
81 C_RESULT
82 vp_os_cond_timed_wait(vp_os_cond_t *cond, uint32_t ms)
83 {
84   struct timespec ts;
85   struct timeval tv;
86   gettimeofday(&tv, NULL);
87   TIMEVAL_TO_TIMESPEC(&tv, &ts);
88   ts.tv_sec += ms/1000;
89   ts.tv_nsec += (ms%1000)*1000;
90   return ( pthread_cond_timedwait(&cond->cond, (pthread_mutex_t *)cond->mutex, &ts) == ETIMEDOUT ? FAIL : SUCCESS );
91 }
92
93
94 void
95 vp_os_cond_signal(vp_os_cond_t *cond)
96 {
97   pthread_cond_signal(&cond->cond);
98 }
99
100 void
101 vp_os_cond_broadcast(vp_os_cond_t *cond)
102 {
103         pthread_cond_broadcast(&cond->cond);
104 }