From owner-freebsd-threads@FreeBSD.ORG Mon Jan 5 22:53:55 2004 Return-Path: Delivered-To: freebsd-threads@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 4AA7D16A4CE for ; Mon, 5 Jan 2004 22:53:55 -0800 (PST) Received: from bento.FreeBSD.org (bento.freebsd.org [216.136.204.23]) by mx1.FreeBSD.org (Postfix) with ESMTP id 278E443D48; Mon, 5 Jan 2004 22:53:54 -0800 (PST) (envelope-from davidxu@freebsd.org) Received: from freebsd.org (localhost [127.0.0.1]) by bento.FreeBSD.org (8.12.10/8.12.10) with ESMTP id i066rnfx042944; Mon, 5 Jan 2004 22:53:50 -0800 (PST) (envelope-from davidxu@freebsd.org) Message-ID: <3FFA5B61.8070109@freebsd.org> Date: Tue, 06 Jan 2004 14:53:21 +0800 From: David Xu User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.5b) Gecko/20030723 Thunderbird/0.1 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Daniel Eischen References: In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit cc: Earl Chew cc: freebsd-threads@freebsd.org Subject: Re: misc/24641: pthread_rwlock_rdlock can deadlock X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 06 Jan 2004 06:53:55 -0000 Daniel Eischen wrote: >It shouldn't fail; it should deadlock. > > > >>>It may seem trivial to implement this, but when you consider >>>that there may be a lot of readers then it is not so trivial. >>>In order to implement it, you need to know whether a thread >>>owns the rdlock and have a recurse count for it. >>> >>> >>Hmm... doesn't P2 already require you to support recursion? >> >> > >The implementation just keeps one count of total readers (i.e., >recursion by a group of readers); this is stored in the rwlock. >Recursive rdlocks by the same thread count as multiple readers. >There is no per-thread counter. For recursive _mutexes_ this >is simple because there can be only one owner so the one recurse >count stored in the mutex is sufficient. But for rwlocks there >can be lots of readers, and each reader may hold multiple rwlocks. >You either need a queue to hold the rwlock status (pointer to >rwlock and recurse count) for each owned rwlock in each thread, >or you need a queue of thread status (pointer to thread and >recurse count) for each owning thread in each rwlock. And >regardless of where you store the queue, you need to seach it >to see if the thread already owns the rdlock (and then again >on the rdunlock). > >Think about it. What if you have 100 threads rdlock recursing >on 1 rwlock? How do you tell if threads already own a rwlock? >And how many times have they recursed on the rwlock? And what >if threads want to own multiple rwlocks? > >Our current implementation is rather simple, but it does >avoid us from having to build in limits as to how many >rwlocks can be owned by a thread or how many threads can >own a rwlock. I'll add this to my TODO list for libkse >but it will come at a bit of a performance & storage >cost and there will have to be some built-in limits. > > These information should be written into manual, so nobody will repeatly ask this question. :-)