From owner-freebsd-bugs Mon Oct 19 11:40:10 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id LAA05064 for freebsd-bugs-outgoing; Mon, 19 Oct 1998 11:40:10 -0700 (PDT) (envelope-from owner-freebsd-bugs@FreeBSD.ORG) Received: from freefall.freebsd.org (freefall.FreeBSD.ORG [204.216.27.21]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id LAA05048 for ; Mon, 19 Oct 1998 11:40:04 -0700 (PDT) (envelope-from gnats@FreeBSD.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.8.8/8.8.5) id LAA29325; Mon, 19 Oct 1998 11:40:01 -0700 (PDT) Received: (from nobody@localhost) by hub.freebsd.org (8.8.8/8.8.8) id LAA03985; Mon, 19 Oct 1998 11:30:17 -0700 (PDT) (envelope-from nobody) Message-Id: <199810191830.LAA03985@hub.freebsd.org> Date: Mon, 19 Oct 1998 11:30:17 -0700 (PDT) From: info@highwind.com To: freebsd-gnats-submit@FreeBSD.ORG X-Send-Pr-Version: www-1.0 Subject: kern/8375: pthread_cond_wait() spins the CPU Sender: owner-freebsd-bugs@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org >Number: 8375 >Category: kern >Synopsis: pthread_cond_wait() spins the CPU >Confidential: no >Severity: serious >Priority: high >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Mon Oct 19 11:40:00 PDT 1998 >Last-Modified: >Originator: Robert Fleischman >Organization: HighWind Software, Inc. >Release: 3.0 with LATEST libc_r >Environment: % uname -a FreeBSD zonda.highwind.com 3.0-19980831-SNAP FreeBSD 3.0-19980831-SNAP #0: Mon Aug 31 14:03:19 GMT 1998 root@make.ican.net:/usr/src/sys/compile/GENERIC i386 >Description: If a number of threads are waiting for a condition, the application spins the CPU at 100% and eventually hangs the application. I've included a test program below which illustrates the problem. >How-To-Repeat: /* Illustration of FreeBSD pthread_cond_wait() bug This program sets up a conditional wait and fires off a dozen threads that simply wait for the condition. Once the threads are started, the main thread loops signalling the condition once a second. Normally, this should result in "Signalling" and "Got Condition" being printed once a second. However, because of some bugs in FreeBSD, the pthread_cond_wait() spins the CPU and no progress is made. g++ -o condWaitBug -D_REENTRANT -D_THREAD_SAFE -g -Wall condWaitBug.C -pthread */ #include #include #include #include #include pthread_mutex_t lock; pthread_cond_t condition; static void *condThread(void *) { // Wait until we are signalled, then print. while (true) { assert(!::pthread_cond_wait(&condition, &lock)); ::printf("Got Condition!\n"); } } int main(int, char **) { // Initialize Lock pthread_mutexattr_t lock_attr; assert(!::pthread_mutexattr_init(&lock_attr)); assert(!::pthread_mutex_init(&lock, &lock_attr)); assert(!::pthread_mutexattr_destroy(&lock_attr)); // Initialize Condition pthread_condattr_t cond_attr; assert(!::pthread_condattr_init(&cond_attr)); assert(!::pthread_cond_init(&condition, &cond_attr)); assert(!::pthread_condattr_destroy(&cond_attr)); // Lock the lock assert(!::pthread_mutex_lock(&lock)); // Spawn off a dozen threads to get signalled for (int j = 0; j < 12; ++j) { pthread_t tid; pthread_attr_t attr; assert(!::pthread_attr_init(&attr)); assert(!::pthread_create(&tid, &attr, condThread, 0)); assert(!::pthread_attr_destroy(&attr)); } // Sleep for 3 seconds to make sure the threads started up. timeval timeout; timeout.tv_sec = 3; timeout.tv_usec = 0; ::select(0, 0, 0, 0, &timeout); for (int k = 0; k < 60; ++k) { ::printf("Signalling\n"); ::pthread_cond_signal(&condition); // Sleep for a second timeout.tv_sec = 1; timeout.tv_usec = 0; ::select(0, 0, 0, 0, &timeout); } return EXIT_SUCCESS; } >Fix: >Audit-Trail: >Unformatted: To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-bugs" in the body of the message