From owner-freebsd-current@FreeBSD.ORG Sun Dec 4 03:34:16 2005 Return-Path: X-Original-To: current@freebsd.org Delivered-To: freebsd-current@FreeBSD.ORG Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id E05EB16A420; Sun, 4 Dec 2005 03:34:16 +0000 (GMT) (envelope-from davidxu@freebsd.org) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id 9ADCD43D53; Sun, 4 Dec 2005 03:34:16 +0000 (GMT) (envelope-from davidxu@freebsd.org) Received: from [127.0.0.1] (davidxu@localhost [127.0.0.1]) by freefall.freebsd.org (8.13.3/8.13.3) with ESMTP id jB43YEI7049088; Sun, 4 Dec 2005 03:34:15 GMT (envelope-from davidxu@freebsd.org) Message-ID: <439263BA.1090404@freebsd.org> Date: Sun, 04 Dec 2005 11:34:18 +0800 From: David Xu User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.7.12) Gecko/20050928 X-Accept-Language: en-us, en MIME-Version: 1.0 To: David Xu References: <0B746373-8C29-4ADF-9218-311AE08F3834@canonware.com> <4391569A.7080808@freebsd.org> <43924917.3070506@freebsd.org> <43925A0D.8070906@freebsd.org> In-Reply-To: <43925A0D.8070906@freebsd.org> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Cc: Jason Evans , current@freebsd.org Subject: Re: New libc malloc patch X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 04 Dec 2005 03:34:17 -0000 David Xu wrote: > Here is sample code to implement a mutex by using umtx syscalls: > ... > void > unlock_mtx(struct umtx *mtx) > { > volatile uintptr_t *m = (volatile uintptr_t *)mtx; > > for (;;) { > if (atomic_load_acq_ptr(m) == LCK_UNLOCKED) > err(1, "unlock a unlocked mutex\n"); > if (atomic_load_acq_ptr(m) == LCK_LOCKED) { > if (atomic_cmpset_acq_ptr(m, LCK_LOCKED, LCK_UNLOCKED)) > return; > } > if (atomic_load_acq_ptr(m) == LCK_CONTENDED) { > atomic_store_rel_ptr(m, LCK_UNLOCKED); > _umtx_op((struct umtx *)m, UMTX_OP_WAKE, 1, NULL, NULL); OOP, should be: _umtx_op((struct umtx *)m, UMTX_OP_WAKE, INT_MAX, NULL, NULL); This line is not very optimal if there are lots of thread waiting there. :-) There is optimal version using transaction id: http://www.dragonflybsd.org/cvsweb/src/lib/libthread_xu/thread/thr_umtx.c?rev=1.2&content-type=text/x-cvsweb-markup Though, libthr in freebsd does not use these semantices, instead they are implemented in kernel. David Xu