>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     a...@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     r...@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:
_______________________________________________
freebsd-bugs@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-bugs
To unsubscribe, send any mail to "freebsd-bugs-unsubscr...@freebsd.org"

Reply via email to