Skip to content

Commit b39ef27

Browse files
committed
port: linux: implement signals API
Signed-off-by: Mike Szczys <[email protected]>
1 parent ec188c1 commit b39ef27

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

port/linux/golioth_sys_linux.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,83 @@ int golioth_sys_sem_get_fd(golioth_sys_sem_t sem)
183183
return SEM_TO_FD(sem);
184184
}
185185

186+
/*--------------------------------------------------
187+
* Signals
188+
*------------------------------------------------*/
189+
190+
struct condition_and_mutex
191+
{
192+
pthread_cond_t cond;
193+
pthread_mutex_t lock;
194+
};
195+
196+
golioth_sys_signal_t golioth_sys_signal_create(void)
197+
{
198+
struct condition_and_mutex *sig = golioth_sys_malloc(sizeof(struct condition_and_mutex));
199+
pthread_cond_init(&sig->cond, NULL);
200+
pthread_mutex_init(&sig->lock, NULL);
201+
return (golioth_sys_signal_t) sig;
202+
}
203+
204+
bool golioth_sys_signal_poll(golioth_sys_signal_t sig, int32_t ms_to_wait)
205+
{
206+
if (!sig)
207+
{
208+
return false;
209+
}
210+
211+
int ret = 0;
212+
struct condition_and_mutex *signal = sig;
213+
struct timespec abstime;
214+
215+
pthread_mutex_lock(&signal->lock);
216+
217+
if (ms_to_wait < 0)
218+
{
219+
ret = (0 == pthread_cond_wait(&signal->cond, &signal->lock));
220+
goto signal_poll_finish;
221+
}
222+
223+
clock_gettime(CLOCK_REALTIME, &abstime);
224+
225+
if (ms_to_wait)
226+
{
227+
abstime.tv_sec += ms_to_wait / 1000;
228+
abstime.tv_nsec += (ms_to_wait % 1000) * 1000000;
229+
}
230+
231+
ret = (0 == pthread_cond_timedwait(&signal->cond, &signal->lock, &abstime));
232+
233+
signal_poll_finish:
234+
pthread_mutex_unlock(&signal->lock);
235+
return ret;
236+
}
237+
238+
bool golioth_sys_signal_raise(golioth_sys_signal_t sig)
239+
{
240+
if (!sig)
241+
{
242+
return false;
243+
}
244+
245+
struct condition_and_mutex *signal = sig;
246+
return (0 == pthread_cond_signal(&signal->cond));
247+
}
248+
249+
void golioth_sys_signal_reset(golioth_sys_signal_t sig)
250+
{
251+
/* There is nothing to reset */
252+
return;
253+
}
254+
255+
void golioth_sys_signal_destroy(golioth_sys_signal_t sig)
256+
{
257+
struct condition_and_mutex *signal = sig;
258+
pthread_cond_destroy(&signal->cond);
259+
pthread_mutex_destroy(&signal->lock);
260+
golioth_sys_free(sig);
261+
}
262+
186263
/*--------------------------------------------------
187264
* Software Timers
188265
*------------------------------------------------*/

0 commit comments

Comments
 (0)