From 01acc8152487cc5ea54150fec9be3709af6ea0d3 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Tue, 17 Sep 2013 00:07:29 +0200 Subject: [PATCH] Add pthread_cond_timedwait(3). --- libpthread/Makefile | 1 + libpthread/include/pthread.h | 4 +- libpthread/pthread_cond_timedwait.c++ | 57 +++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 libpthread/pthread_cond_timedwait.c++ diff --git a/libpthread/Makefile b/libpthread/Makefile index c8827fcd..9f66af48 100644 --- a/libpthread/Makefile +++ b/libpthread/Makefile @@ -13,6 +13,7 @@ CXXFLAGS:=$(CXXFLAGS) -Wall -Wextra -fno-exceptions -fno-rtti OBJS=\ pthread_cond_broadcast.o \ pthread_cond_signal.o \ +pthread_cond_timedwait.o \ pthread_cond_wait.o \ pthread_equal.o \ pthread_initialize.o \ diff --git a/libpthread/include/pthread.h b/libpthread/include/pthread.h index d9924a83..8895c833 100644 --- a/libpthread/include/pthread.h +++ b/libpthread/include/pthread.h @@ -192,7 +192,9 @@ int pthread_cond_broadcast(pthread_cond_t*); /* TODO: pthread_cond_destroy */ /* TODO: pthread_cond_init */ int pthread_cond_signal(pthread_cond_t*); -/* TODO: pthread_cond_timedwait */ +int pthread_cond_timedwait(pthread_cond_t* __restrict, + pthread_mutex_t* __restrict, + const struct timespec* __restrict); int pthread_cond_wait(pthread_cond_t* __restrict, pthread_mutex_t* __restrict); /* TODO: pthread_condattr_destroy */ /* TODO: pthread_condattr_getclock */ diff --git a/libpthread/pthread_cond_timedwait.c++ b/libpthread/pthread_cond_timedwait.c++ new file mode 100644 index 00000000..fa96da4f --- /dev/null +++ b/libpthread/pthread_cond_timedwait.c++ @@ -0,0 +1,57 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2014. + + This file is part of Sortix libpthread. + + Sortix libpthread is free software: you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by the + Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + Sortix libpthread is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with Sortix libpthread. If not, see . + + pthread_cond_timedwait.c++ + Waits on a condition or until a timeout happens. + +*******************************************************************************/ + +#include +#include +#include +#include +#include +#include + +extern "C" +int pthread_cond_timedwait(pthread_cond_t* restrict cond, + pthread_mutex_t* restrict mutex, + const struct timespec* restrict abstime) +{ + struct pthread_cond_elem elem; + elem.next = NULL; + elem.woken = 0; + if ( cond->last ) + cond->last->next = &elem; + if ( !cond->last ) + cond->first = &elem; + cond->last = &elem; + while ( !elem.woken ) + { + struct timespec now; + if ( clock_gettime(CLOCK_REALTIME, &now) < 0 ) + return errno; + if ( timespec_le(*abstime, now) ) + return errno = ETIMEDOUT; + pthread_mutex_unlock(mutex); + sched_yield(); + pthread_mutex_lock(mutex); + } + return 0; +}