Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 19 Oct 1998 18:25:49 -0400 (EDT)
From:      Alfred Perlstein <bright@hotjobs.com>
To:        HighWind Software Information <info@highwind.com>
Cc:        John Birrell <jb@cimlogic.com.au>, current@FreeBSD.ORG
Subject:   Re: Another Serious libc_r problem
Message-ID:  <Pine.BSF.4.05.9810191818360.11526-100000@porkfriedrice.ny.genx.net>
In-Reply-To: <199810192045.QAA21744@highwind.com>

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


hrm, you are right, i'm looking at the source and seeing if i can grasp
what to do.

In fact running gdb/ddd on both programs you sent seem to cause deadlock
in different parts of the scheduler.

Do you have time at all to compile your libc_r with -g?

i've eadited my makefile in /usr/src/lib/libc_r to have a line with:
# Uncomment this if you want libc_r to contain debug information for
# thread locking.
CFLAGS+=-D_LOCK_DEBUG -g    

I'm also interested in the effect of a pthread_cond_signal after a
pthread_cond_broadcast, but i have to take care of some important things,
i'll look more later tonight.

I'll have to take a look at this, I'm also forwarding to 
John Birrell <jb@cimlogic.com.au> about this, as it seems to be mostly his
code.

I think something about scheduling a thread to run causes it to be ran
with the SPINLOCK still in place creating deadlock later on.

I think queing/scheduling might need a arg list of internal values to
lock/unlock on entry/exit.

I apologize for discounting your argument earlier, i am new at threads
though.

Alfred Perlstein - Programmer, HotJobs Inc. - www.hotjobs.com
-- There are operating systems, and then there's FreeBSD.
-- http://www.freebsd.org/                        3.0-current

On Mon, 19 Oct 1998, HighWind Software Information wrote:

> 
>    I believe your test program is in error.  Your threads are
>    not ever going to awake from the pthread_cond_wait() statement
>    because the mutex cannot be acquired.  Your main thread locks
>    the mutex and never unlocks it.
> 
> I claim that doesn't matter. WHY should the CPU spin if I signal
> twice on a lock I have? That is PERFECTLY valid.
> 
> Let's say I want to say that TWO events are ready to be handled.
> That is perfectly fine.
> 
> ---
> 
> Simply to placate your claim, here is another version that locks and
> unlocks the mutex in proper fashion. I simply had to insert an
> additional "sleep" to get the bug to appear.
> 
> In both cases, spinning the CPU at 100% and allowing the program to 
> hang and make no forward progress is a SERIOUS bug.
> 
> I claim that this new version is 100% valid. Does proper locking, and
> also works on other O/S's. On FreeBSD it does not work at all.
> 
> -Rob
> 
> 
/*   
   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 <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

pthread_mutex_t lock;
pthread_cond_t condition;

static void *condThread(void *)
{
    // Wait until we are signalled, then print.
    while (true) {
	// Be sure to do proper locking and unlocking
	assert(!::pthread_mutex_lock(&lock));	
	assert(!::pthread_cond_wait(&condition, &lock));
	::printf("Got Condition!\n");
	assert(!::pthread_mutex_unlock(&lock));	
    }
}

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));

    // 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) {
	// Signal while locked
	assert(!::pthread_mutex_lock(&lock));	
	::printf("Signalling\n");
	assert(!::pthread_cond_signal(&condition));

	// Sleep for 1 second
	timeout.tv_sec = 1;
	timeout.tv_usec = 0;
	::select(0, 0, 0, 0, &timeout);

	::printf("Signalled again\n");
	assert(!::pthread_cond_signal(&condition));

	assert(!::pthread_mutex_unlock(&lock));	

	// Sleep for 1 second
	timeout.tv_sec = 1;
	timeout.tv_usec = 0;
	::select(0, 0, 0, 0, &timeout);
    }

    return EXIT_SUCCESS;
}



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.05.9810191818360.11526-100000>