ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / Examples / Multiplatform / Protocol / 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
38 void
39 vp_os_mutex_unlock(vp_os_mutex_t *mutex)
40 {
41   pthread_mutex_unlock((pthread_mutex_t *)mutex);
42 }
43
44
45 void
46 vp_os_cond_init(vp_os_cond_t *cond, vp_os_mutex_t *mutex)
47 {
48   pthread_cond_init(&cond->cond, NULL);
49   cond->mutex = mutex;
50 }
51
52
53 void
54 vp_os_cond_destroy(vp_os_cond_t *cond)
55 {
56   pthread_cond_destroy(&cond->cond);
57 }
58
59
60 void
61 vp_os_cond_wait(vp_os_cond_t *cond)
62 {
63   pthread_cond_wait(&cond->cond, (pthread_mutex_t *)cond->mutex);
64 }
65
66
67 C_RESULT
68 vp_os_cond_timed_wait(vp_os_cond_t *cond, uint32_t ms)
69 {
70   struct timespec ts;
71   struct timeval tv;
72   gettimeofday(&tv, NULL);
73   TIMEVAL_TO_TIMESPEC(&tv, &ts);
74   ts.tv_sec += ms/1000;
75   ts.tv_nsec += (ms%1000)*1000;
76   return ( pthread_cond_timedwait(&cond->cond, (pthread_mutex_t *)cond->mutex, &ts) == ETIMEDOUT ? FAIL : SUCCESS );
77 }
78
79
80 void
81 vp_os_cond_signal(vp_os_cond_t *cond)
82 {
83   pthread_cond_signal(&cond->cond);
84 }
85