Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 9 Nov 2011 14:09:09 GMT
From:      Armin Gruner <ag-freebsd@muc.de>
To:        freebsd-gnats-submit@FreeBSD.org
Subject:   misc/162403: regression in FreeBSD/9 regarding pthread timeouts in kernel context
Message-ID:  <201111091409.pA9E99BK077145@red.freebsd.org>
Resent-Message-ID: <201111091410.pA9EA9f1084089@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         162403
>Category:       misc
>Synopsis:       regression in FreeBSD/9 regarding pthread timeouts in kernel context
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Nov 09 14:10:09 UTC 2011
>Closed-Date:
>Last-Modified:
>Originator:     Armin Gruner
>Release:        FreeBSD 9.0-RC1
>Organization:
muc.de
>Environment:
FreeBSD ag.techsat.com 9.0-RC1 FreeBSD 9.0-RC1 #1 r226966M: Mon Oct 31 15:11:15 CET 2011     ag@ag.techsat.com:/usr/obj/usr/src/sys/GENERIC  i386

>Description:
pthread operations with a given timeout don't work under FreeBSD 9,
if the process is running in realtime priority context. The timeout
is not honoured at all.
>How-To-Repeat:
Compile the given code snippet with  cc -pthread.

It has two threads, one waiting for a condition to fire up.
The producer delays initial signaling of the condition when started;
thus the consumer should timeout after five seconds.
Afterwards, it will be signaled every three seconds.

The call to pthread_cond_timedwait() properly times out if the process is not running with RT prio:

[ag@ag 1024]$ uname -a                                                                      FreeBSD ag.techsat.com 9.0-RC1 FreeBSD 9.0-RC1 #1 r226966M: Mon Oct 31 15:11:15 CET 2011    ag@ag:/usr/obj/usr/src/sys/GENERIC  i386

[ag@ag 1017]$ ./tt
tt: pthread_cond_timedwait: Operation timed out
WOKE UP
WOKE UP

It does not honour the timeout if running with realtime priority:

[ag@ag 1020]$ SU rtprio 31 ./tt                                                              ~/work/c
WOKE UP
WOKE UP

Under FreeBSD/8, both user and kernel pthread contexts works as expected:

$ uname -a
FreeBSD release-pc-freebsd 8.2-RELEASE FreeBSD 8.2-RELEASE #0: Fri Feb 18 02:24:46 UTC 2011     root@almeida.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  i386

$ ./tt
tt: pthread_cond_timedwait: Operation timed out
WOKE UP
WOKE UP

[ag@release-pc-freebsd 369]$ SU rtprio 31 ./tt
tt: pthread_cond_timedwait: Operation timed out
WOKE UP
WOKE UP

>Fix:


Patch attached with submission follows:

#include <stdio.h>
#include <time.h>
#include <pthread.h>

pthread_mutex_t mtx;
pthread_cond_t  cnd;

void *
loop(void *dummy)
{
    int rc;
    struct timespec abstime;

    while (1)
    {
        clock_gettime(CLOCK_REALTIME, &abstime);
        abstime.tv_sec += 5;

        rc = pthread_mutex_lock(&mtx);
        if (rc != 0)
            warnc(rc, "pthread_mutex_lock");

        rc = pthread_cond_timedwait(&cnd, &mtx, &abstime);
        if (rc != 0)
            warnc(rc, "pthread_cond_timedwait");
        else
            fprintf(stderr, "WOKE UP\n");

        rc = pthread_mutex_unlock(&mtx);
        if (rc != 0)
            warnc(rc, "pthread_mutex_unlock");
    }

    return NULL;
}

main()
{
    int rc;
    pthread_t t1;
    struct sched_param sp;

    rc = pthread_mutex_init(&mtx, NULL);
    if (rc != 0)
        warnc(rc, "pthread_mutex_init");

    rc = pthread_cond_init(&cnd, NULL);
    if (rc != 0)
        warnc(rc, "pthread_cond_init");

    pthread_create(&t1, NULL, loop, NULL);

    sleep(8);

    while (1)
    {
        rc = pthread_mutex_lock(&mtx);
        if (rc != 0)
            warnc(rc, "pthread_mutex_lock");

        rc = pthread_cond_signal(&cnd);
        if (rc != 0)
            warnc(rc, "pthread_cond_signal");

        rc = pthread_mutex_unlock(&mtx);
        if (rc != 0)
            warnc(rc, "pthread_mutex_unlock");

        sleep(3);
    }
}


>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201111091409.pA9E99BK077145>