Threading primitives separated.
[strongswan.git] / src / libstrongswan / threading / condvar.c
1 /*
2 * Copyright (C) 2008-2009 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #define _GNU_SOURCE
18 #include <pthread.h>
19 #include <stdint.h>
20 #include <time.h>
21 #include <errno.h>
22
23 #include <threading.h>
24 #include <library.h>
25 #include <debug.h>
26
27 #include "condvar.h"
28 #include "mutex.h"
29
30 /**
31 * Implementation of condvar_t.wait.
32 */
33 static void _wait(private_condvar_t *this, private_mutex_t *mutex)
34 {
35 if (mutex->recursive)
36 {
37 private_r_mutex_t* recursive = (private_r_mutex_t*)mutex;
38
39 /* mutex owner gets cleared during condvar wait */
40 recursive->thread = 0;
41 pthread_cond_wait(&this->condvar, &mutex->mutex);
42 recursive->thread = pthread_self();
43 }
44 else
45 {
46 pthread_cond_wait(&this->condvar, &mutex->mutex);
47 }
48 }
49
50 /**
51 * Implementation of condvar_t.timed_wait_abs.
52 */
53 static bool timed_wait_abs(private_condvar_t *this, private_mutex_t *mutex,
54 timeval_t time)
55 {
56 struct timespec ts;
57 bool timed_out;
58
59 ts.tv_sec = time.tv_sec;
60 ts.tv_nsec = time.tv_usec * 1000;
61
62 if (mutex->recursive)
63 {
64 private_r_mutex_t* recursive = (private_r_mutex_t*)mutex;
65
66 recursive->thread = 0;
67 timed_out = pthread_cond_timedwait(&this->condvar, &mutex->mutex,
68 &ts) == ETIMEDOUT;
69 recursive->thread = pthread_self();
70 }
71 else
72 {
73 timed_out = pthread_cond_timedwait(&this->condvar, &mutex->mutex,
74 &ts) == ETIMEDOUT;
75 }
76 return timed_out;
77 }
78
79 /**
80 * Implementation of condvar_t.timed_wait.
81 */
82 static bool timed_wait(private_condvar_t *this, private_mutex_t *mutex,
83 u_int timeout)
84 {
85 timeval_t tv;
86 u_int s, ms;
87
88 time_monotonic(&tv);
89
90 s = timeout / 1000;
91 ms = timeout % 1000;
92
93 tv.tv_sec += s;
94 tv.tv_usec += ms * 1000;
95
96 if (tv.tv_usec > 1000000 /* 1s */)
97 {
98 tv.tv_usec -= 1000000;
99 tv.tv_sec++;
100 }
101 return timed_wait_abs(this, mutex, tv);
102 }
103
104 /**
105 * Implementation of condvar_t.signal.
106 */
107 static void _signal(private_condvar_t *this)
108 {
109 pthread_cond_signal(&this->condvar);
110 }
111
112 /**
113 * Implementation of condvar_t.broadcast.
114 */
115 static void broadcast(private_condvar_t *this)
116 {
117 pthread_cond_broadcast(&this->condvar);
118 }
119
120 /**
121 * Implementation of condvar_t.destroy
122 */
123 static void condvar_destroy(private_condvar_t *this)
124 {
125 pthread_cond_destroy(&this->condvar);
126 free(this);
127 }
128
129 /*
130 * see header file
131 */
132 condvar_t *condvar_create(condvar_type_t type)
133 {
134 switch (type)
135 {
136 case CONDVAR_TYPE_DEFAULT:
137 default:
138 {
139 pthread_condattr_t condattr;
140 private_condvar_t *this = malloc_thing(private_condvar_t);
141
142 this->public.wait = (void(*)(condvar_t*, mutex_t *mutex))_wait;
143 this->public.timed_wait = (bool(*)(condvar_t*, mutex_t *mutex, u_int timeout))timed_wait;
144 this->public.timed_wait_abs = (bool(*)(condvar_t*, mutex_t *mutex, timeval_t time))timed_wait_abs;
145 this->public.signal = (void(*)(condvar_t*))_signal;
146 this->public.broadcast = (void(*)(condvar_t*))broadcast;
147 this->public.destroy = (void(*)(condvar_t*))condvar_destroy;
148
149 pthread_condattr_init(&condattr);
150 #ifdef HAVE_CONDATTR_CLOCK_MONOTONIC
151 pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC);
152 #endif
153 pthread_cond_init(&this->condvar, &condattr);
154 pthread_condattr_destroy(&condattr);
155
156 return &this->public;
157 }
158 }
159 }
160