From 993a5736f98f5e7ee2fc31668d46dbdb90ebccb3 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Mon, 7 Jul 2014 15:13:17 +0200 Subject: [PATCH] Add pthread_condattr_getclock(3) and pthread_condattr_setclock(3). --- libpthread/Makefile | 2 ++ libpthread/include/__/pthread.h | 4 +++ libpthread/include/pthread.h | 7 +++--- libpthread/pthread_cond_init.c++ | 1 + libpthread/pthread_cond_timedwait.c++ | 2 +- libpthread/pthread_condattr_getclock.c++ | 32 ++++++++++++++++++++++++ libpthread/pthread_condattr_init.c++ | 1 + libpthread/pthread_condattr_setclock.c++ | 31 +++++++++++++++++++++++ 8 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 libpthread/pthread_condattr_getclock.c++ create mode 100644 libpthread/pthread_condattr_setclock.c++ diff --git a/libpthread/Makefile b/libpthread/Makefile index 913ef94d..43860bd8 100644 --- a/libpthread/Makefile +++ b/libpthread/Makefile @@ -12,7 +12,9 @@ CXXFLAGS:=$(CXXFLAGS) -Wall -Wextra -fno-exceptions -fno-rtti OBJS=\ pthread_condattr_destroy.o \ +pthread_condattr_getclock.o \ pthread_condattr_init.o \ +pthread_condattr_setclock.o \ pthread_cond_broadcast.o \ pthread_cond_destroy.o \ pthread_cond_init.o \ diff --git a/libpthread/include/__/pthread.h b/libpthread/include/__/pthread.h index 595f0e7b..a3a5e227 100644 --- a/libpthread/include/__/pthread.h +++ b/libpthread/include/__/pthread.h @@ -44,22 +44,26 @@ typedef struct { struct pthread_cond_elem* first; struct pthread_cond_elem* last; + __clock_t clock; } __pthread_cond_t; #else typedef struct { void* __pthread_first; void* __pthread_last; + __clock_t __pthread_clock; } __pthread_cond_t; #endif #if defined(__is_sortix_libpthread) typedef struct { + __clock_t clock; } __pthread_condattr_t; #else typedef struct { + __clock_t __pthread_clock; } __pthread_condattr_t; #endif diff --git a/libpthread/include/pthread.h b/libpthread/include/pthread.h index c86b5ac5..fe52e916 100644 --- a/libpthread/include/pthread.h +++ b/libpthread/include/pthread.h @@ -150,7 +150,7 @@ struct pthread_cond_elem }; #endif -#define PTHREAD_COND_INITIALIZER { NULL, NULL } +#define PTHREAD_COND_INITIALIZER { NULL, NULL, CLOCK_REALTIME } #define PTHREAD_MUTEX_INITIALIZER { 0, PTHREAD_MUTEX_DEFAULT, 0, 0 } #define PTHREAD_RWLOCK_INITIALIZER 0 @@ -198,10 +198,11 @@ int pthread_cond_timedwait(pthread_cond_t* __restrict, const struct timespec* __restrict); int pthread_cond_wait(pthread_cond_t* __restrict, pthread_mutex_t* __restrict); int pthread_condattr_destroy(pthread_condattr_t*); -/* TODO: pthread_condattr_getclock */ +int pthread_condattr_getclock(const pthread_condattr_t* __restrict, + clockid_t* __restrict); /* TODO: pthread_condattr_getpshared */ int pthread_condattr_init(pthread_condattr_t*); -/* TODO: pthread_condattr_setclock */ +int pthread_condattr_setclock(pthread_condattr_t*, clockid_t); /* TODO: pthread_condattr_setpshared */ /* TODO: pthread_create */ /* TODO: pthread_detach */ diff --git a/libpthread/pthread_cond_init.c++ b/libpthread/pthread_cond_init.c++ index 1d137987..37d4b5ce 100644 --- a/libpthread/pthread_cond_init.c++ +++ b/libpthread/pthread_cond_init.c++ @@ -37,6 +37,7 @@ int pthread_cond_init(pthread_cond_t* restrict cond, cond->first = NULL; cond->last = NULL; + cond->clock = attr->clock; return 0; } diff --git a/libpthread/pthread_cond_timedwait.c++ b/libpthread/pthread_cond_timedwait.c++ index fa96da4f..b7de24c8 100644 --- a/libpthread/pthread_cond_timedwait.c++ +++ b/libpthread/pthread_cond_timedwait.c++ @@ -45,7 +45,7 @@ int pthread_cond_timedwait(pthread_cond_t* restrict cond, while ( !elem.woken ) { struct timespec now; - if ( clock_gettime(CLOCK_REALTIME, &now) < 0 ) + if ( clock_gettime(cond->clock, &now) < 0 ) return errno; if ( timespec_le(*abstime, now) ) return errno = ETIMEDOUT; diff --git a/libpthread/pthread_condattr_getclock.c++ b/libpthread/pthread_condattr_getclock.c++ new file mode 100644 index 00000000..fc167a7c --- /dev/null +++ b/libpthread/pthread_condattr_getclock.c++ @@ -0,0 +1,32 @@ +/******************************************************************************* + + 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_condattr_getclock.c++ + Gets the requested clock in a cond attribute object. + +*******************************************************************************/ + +#include + +extern "C" +int pthread_condattr_getclock(const pthread_condattr_t* restrict attr, + clockid_t* restrict clock) +{ + return *clock = attr->clock, 0; +} diff --git a/libpthread/pthread_condattr_init.c++ b/libpthread/pthread_condattr_init.c++ index 2ac61275..f2ba725a 100644 --- a/libpthread/pthread_condattr_init.c++ +++ b/libpthread/pthread_condattr_init.c++ @@ -28,5 +28,6 @@ extern "C" int pthread_condattr_init(pthread_condattr_t* attr) { memset(attr, 0, sizeof(*attr)); + attr->clock = CLOCK_REALTIME; return 0; } diff --git a/libpthread/pthread_condattr_setclock.c++ b/libpthread/pthread_condattr_setclock.c++ new file mode 100644 index 00000000..c2442528 --- /dev/null +++ b/libpthread/pthread_condattr_setclock.c++ @@ -0,0 +1,31 @@ +/******************************************************************************* + + 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_condattr_setclock.c++ + Sets the requested clock in a cond attribute object. + +*******************************************************************************/ + +#include + +extern "C" +int pthread_condattr_setclock(pthread_condattr_t* attr, clockid_t clock) +{ + return attr->clock = clock, 0; +}