Add pthread_cond_timedwait(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2013-09-17 00:07:29 +02:00
parent 8f12a5f6f9
commit 01acc81524
3 changed files with 61 additions and 1 deletions

View File

@ -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 \

View File

@ -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 */

View File

@ -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 <http://www.gnu.org/licenses/>.
pthread_cond_timedwait.c++
Waits on a condition or until a timeout happens.
*******************************************************************************/
#include <errno.h>
#include <pthread.h>
#include <sched.h>
#include <time.h>
#include <timespec.h>
#include <unistd.h>
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;
}