From owner-freebsd-threads@FreeBSD.ORG Wed Mar 29 09:10:37 2006 Return-Path: X-Original-To: threads@freebsd.org 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 7A6B016A41F; Wed, 29 Mar 2006 09:10:37 +0000 (UTC) (envelope-from des@des.no) Received: from tim.des.no (tim.des.no [194.63.250.121]) by mx1.FreeBSD.org (Postfix) with ESMTP id F316E43D68; Wed, 29 Mar 2006 09:10:36 +0000 (GMT) (envelope-from des@des.no) Received: from tim.des.no (localhost [127.0.0.1]) by spam.des.no (Postfix) with ESMTP id BCE1A2096; Wed, 29 Mar 2006 11:10:28 +0200 (CEST) X-Spam-Tests: AWL,BAYES_00,FORGED_RCVD_HELO X-Spam-Learn: ham X-Spam-Score: -2.4/3.0 X-Spam-Checker-Version: SpamAssassin 3.1.1 (2006-03-10) on tim.des.no Received: from xps.des.no (des.no [80.203.243.180]) by tim.des.no (Postfix) with ESMTP id 1B4BD2081; Wed, 29 Mar 2006 11:10:28 +0200 (CEST) Received: by xps.des.no (Postfix, from userid 1001) id E684B33C8D; Wed, 29 Mar 2006 11:10:27 +0200 (CEST) From: des@des.no (Dag-Erling =?iso-8859-1?Q?Sm=F8rgrav?=) To: David Xu References: <86slp1u4qb.fsf@xps.des.no> <442A466A.9040506@freebsd.org> Date: Wed, 29 Mar 2006 11:10:27 +0200 In-Reply-To: <442A466A.9040506@freebsd.org> (David Xu's message of "Wed, 29 Mar 2006 16:33:46 +0800") Message-ID: <861wwlehvg.fsf@xps.des.no> User-Agent: Gnus/5.110003 (No Gnus v0.3) Emacs/21.3 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Cc: threads@freebsd.org, jeff@freebsd.org Subject: Re: libthr cleanup X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 29 Mar 2006 09:10:37 -0000 David Xu writes: > When libthr was redesigned, things were not clear, I have figured out > all semantics needed to implement libthr, the above functions are the > interfaces current internally implemented by libthr, and functions in > umtx.h is not used because it can generate bloat code than the > versions in libthr, current libthr shared library only has 64K size. > I really dont need struct umtx at all, an integer is enough, basic idea > is using atomic operations to test in userland and wait in kernel. > the above functions should be changed to: > int _umtx_lock_timeout(umtx_t *, > const struct timespec * __restrict timeout); > int _umtx_unlock(umtx_t *mtx); > int _umtx_wait_timeout(umtx_t *, umtx_t expect, > const struct timespec * __restrict timeout); > int _umtx_wake(umtx_t *i, int number); > > the umtx_t could be an integer type, but to maintain binary > compatibility, it has to be a long integer type. To maintain compatibility and ensure type safety, it should be a struct umtx. In effect, the wait / wake operations implement a condition variable. You should not use the same struct (or type) to describe the condition variable as the one you use to describe a mutex. Condition variables are always used in conjunction with a mutex. The mutex must be passed to the wait function so it can be unlocked while the waiting thread waits. It must be held by the thread calling the wake method. Neither the existing interface nor the one you propose do this. > Well, I am not sure you know the history of umtx, the orignal work > only did a lock and unlock semantic, there is no general sleep and > wait semantic, orignal umtx code has obscure race, I think in early > days, valgrind suffered from this bug. I am not sure you understand > how a userland synchronization works, these two operations are used > to implement compare and wait for a integer to be changed, You cannot "wait for an integer to be changed" (at least not without using hardware debugging facilities), and that is not what UMTX_OP_WAIT does. It is a botched condition variable. It waits for another thread to perform an UMTX_OP_WAKE operation with the correct arguments; the fact that an integer has changed is incidental, and the test for that change could be implemented in userland: look up condition variables in any good CS textbook and you will see an example of this, likely in the guise of a sample message queue (or mailbox) implementation. I can understand wanting to move the check into the kernel to avoid spurious context switches, but it has to be done right. > you > should check source code before talking a lot, That is precisely what I have done. > saying that you doubt > the UMTX_OP_WAIT and UMTX_OP_WAKE's correctness is not professional, > there are users using libthr in daily work and stress testing had > been done. > I have put lots of work and time on libthr, I know the problems, > though the _umtx interface is a bit ulgly because it was unclear > when it was being designed, but I don't think it really hurt you or > other people, it can be fixed in few days, I just was hesitating to > add the new interfaces. The amount of work you have put into libthr and its importance to your self-esteem do not guarantee its correctness. What is unprofessional here is 1) the quality of your code and 2) your refusal to consider that I might have a point. DES --=20 Dag-Erling Sm=F8rgrav - des@des.no