From owner-cvs-all Wed Sep 30 18:55:13 1998 Return-Path: Received: (from daemon@localhost) by hub.freebsd.org (8.8.8/8.8.8) id SAA16917 for cvs-all-outgoing; Wed, 30 Sep 1998 18:55:13 -0700 (PDT) (envelope-from owner-cvs-all) Received: from cimlogic.com.au (cimlog.lnk.telstra.net [139.130.51.31]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id SAA16910; Wed, 30 Sep 1998 18:55:03 -0700 (PDT) (envelope-from jb@cimlogic.com.au) Received: (from jb@localhost) by cimlogic.com.au (8.9.1/8.9.1) id MAA07069; Thu, 1 Oct 1998 12:02:33 +1000 (EST) (envelope-from jb) From: John Birrell Message-Id: <199810010202.MAA07069@cimlogic.com.au> Subject: Re: cvs commit: src/lib/libc_r/uthread uthread_gc.c Makefile.inc uthread_init.c uthread_find_thread.c uthread_kern.c uthread_cre In-Reply-To: <19980930192221.S9697@pr.mcs.net> from Alex Nash at "Sep 30, 98 07:22:21 pm" To: nash@mcs.net (Alex Nash) Date: Thu, 1 Oct 1998 12:02:33 +1000 (EST) Cc: jb@FreeBSD.ORG, cvs-committers@FreeBSD.ORG, cvs-all@FreeBSD.ORG X-Mailer: ELM [version 2.4ME+ PL40 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-cvs-all@FreeBSD.ORG X-Loop: FreeBSD.org Precedence: bulk Alex Nash wrote: > On Tue, Sep 29, 1998 at 11:14:00PM -0700, John Birrell wrote: > > jb 1998/09/29 23:13:59 PDT > > > > Modified files: > > lib/libc/stdlib malloc.c > > Log: > > Delete the XXX comments that refer to spinlock recursion. > > What are the semantics that allow these functions to obtain the spinlock > and not release it before returning? If that were to happen, it would show up a bug in either the malloc/realloc/ free code or a function called from within that code. The check that phk has in his code simply allows him to find when his code is broken by other people's changes. If you ever see the recursion warning, someone has broken the code. So going to great pains to get the locking semantics right for broken code is a waste of time. > > > The malloc/free/ > > realloc functions check for recursion within the malloc code itself. In > > a thread-safe library, the single spinlock ensures that no two threads > > go inside the protected code at the same time. > > It doesn't prevent the *same* thread from being inside the protected > code more than once (e.g. due to a signal). The *same* thread will lock against itself. A deadlock. That's a bug. The behaviour of malloc/realloc/free is the same whether threaded or not. The thread implementation has to behave in such a way that it doesn't allow the same thread to go back into the malloc/realloc/free code if it has already taken the lock. The only time this would occur is if the thread kernel was trying to go in there. I've moved that code so that it doesn't need to do that anymore. > > > The thread implementation > > is responsible for ensuring that the spinlock does in fact protect malloc. > > There was a window of opportunity in which this was not the case. I'll fix > > that with a commit RSN. > > Because our spinlock allows a single thread to recursively obtain the > lock without counting, it's conceivable that the following scenario > might occur: It doesn't. A thread can't tell from the spinlock value if _it_ locked the lock or another thread did. All that it knows is that the lock is locked. The thread implementation has to remember what it has locked in order to avoid deadlocks. This means that there is very little performance hit in protecting code with spinlocks. The expectation is that the spinlock will only be locked for a short time. Locks with longer duration require mutex and condition variable pairs so that a blocked thread is put to sleep until the mutex becomes available. -- John Birrell - jb@cimlogic.com.au; jb@freebsd.org http://www.cimlogic.com.au/ CIMlogic Pty Ltd, GPO Box 117A, Melbourne Vic 3001, Australia +61 418 353 137