[input] try to use CLOCK_MONOTONIC for condvar

By default, Pthread condition variables use the wretched
CLOCK_REALTIME for pthread_cond_timedwait(). We reaaaaaaly
want it to use CLOCK_MONOTONIC instead. Where supported,
use pthread_condvar_setclock() to initialize the input
condvar with CLOCK_MONOTONIC, since every other operation
we perform is clocked against it. Closes #2291.
pull/2294/head
nick black 3 years ago committed by nick black
parent a087960ab7
commit 3e0ce64b76

@ -1385,6 +1385,24 @@ getpipes(ipipe pipes[static 2]){
return 0;
}
// attempt to set up a pthread_condattr_t such that pthread_cond_timedwait()
// uses CLOCK_MONOTONIC rather than CLOCK_REALTIME. unfortunately, this isn't
// available on all operating systems. return success so long as we can
// initialize the condattr_t; it does not imply successful clock selection =\.
static int
prep_condattr(pthread_condattr_t* cattr){
int e;
if( (e = pthread_condattr_init(cattr)) ){
logerror("couldn't initialize condattr (%s)\n", strerror(e));
return -1;
}
// remember, return success even if this doesn't fly
if( (e = pthread_condattr_setclock(cattr, CLOCK_MONOTONIC)) ){
logwarn("warning: couldn't set CLOCK_MONOTONIC for condvar (%s)\n", strerror(e));
}
return 0;
}
static inline inputctx*
create_inputctx(tinfo* ti, FILE* infp, int lmargin, int tmargin, int rmargin,
int bmargin, ncsharedstats* stats, unsigned drain,
@ -1396,9 +1414,11 @@ create_inputctx(tinfo* ti, FILE* infp, int lmargin, int tmargin, int rmargin,
i->isize = BUFSIZ;
if( (i->inputs = malloc(sizeof(*i->inputs) * i->isize)) ){
if(pthread_mutex_init(&i->ilock, NULL) == 0){
if(pthread_cond_init(&i->icond, NULL) == 0){
pthread_condattr_t condattr;
if(prep_condattr(&condattr) == 0){
if(pthread_cond_init(&i->icond, &condattr) == 0){
if(pthread_mutex_init(&i->clock, NULL) == 0){
if(pthread_cond_init(&i->ccond, NULL) == 0){
if(pthread_cond_init(&i->ccond, &condattr) == 0){
if((i->stdinfd = fileno(infp)) >= 0){
if( (i->initdata = malloc(sizeof(*i->initdata))) ){
if(getpipes(i->readypipes) == 0){
@ -1443,6 +1463,8 @@ create_inputctx(tinfo* ti, FILE* infp, int lmargin, int tmargin, int rmargin,
}
pthread_cond_destroy(&i->icond);
}
pthread_condattr_destroy(&condattr);
}
pthread_mutex_destroy(&i->ilock);
}
free(i->inputs);

Loading…
Cancel
Save