From owner-freebsd-hackers@FreeBSD.ORG Wed Sep 15 14:02:15 2010 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id A2BCE106564A for ; Wed, 15 Sep 2010 14:02:15 +0000 (UTC) (envelope-from simon@comsys.ntu-kpi.kiev.ua) Received: from comsys.kpi.ua (comsys.kpi.ua [77.47.192.42]) by mx1.freebsd.org (Postfix) with ESMTP id 2E55E8FC14 for ; Wed, 15 Sep 2010 14:02:15 +0000 (UTC) Received: from pm513-1.comsys.kpi.ua ([10.18.52.101] helo=pm513-1.comsys.ntu-kpi.kiev.ua) by comsys.kpi.ua with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.63) (envelope-from ) id 1OvsHX-0003Jg-KI for freebsd-hackers@freebsd.org; Wed, 15 Sep 2010 16:44:15 +0300 Received: by pm513-1.comsys.ntu-kpi.kiev.ua (Postfix, from userid 1001) id 1E5B61CC1E; Wed, 15 Sep 2010 16:44:16 +0300 (EEST) Date: Wed, 15 Sep 2010 16:44:15 +0300 From: Andrey Simonenko To: freebsd-hackers@freebsd.org Message-ID: <20100915134415.GA23727@pm513-1.comsys.ntu-kpi.kiev.ua> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) X-Authenticated-User: simon@comsys.ntu-kpi.kiev.ua X-Authenticator: plain X-Sender-Verify: SUCCEEDED (sender exists & accepts mail) X-Exim-Version: 4.63 (build at 06-Jan-2007 23:14:37) X-Date: 2010-09-15 16:44:15 X-Connected-IP: 10.18.52.101:21881 X-Message-Linecount: 48 X-Body-Linecount: 36 X-Message-Size: 2282 X-Body-Size: 1769 Subject: Questions about mutex implementation in kern/kern_mutex.c X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Sep 2010 14:02:15 -0000 Hello, I have questions about mutex implementation in kern/kern_mutex.c and sys/mutex.h files (current versions of these files): 1. Is the following statement correct for a volatile pointer or integer variable: if a volatile variable is updated by the compare-and-set instruction (e.g. atomic_cmpset_ptr(&val, ...)), then the current value of such variable can be read without any special instruction (e.g. v = val)? I checked Assembler code for a function with "v = val" and "val = v" like statements generated for volatile variable and simple variable and found differences: on ia64 "v = val" was implemented by ld.acq and "val = v" was implemented by st.rel; on mips and sparc64 Assembler code can have different order of lines for volatile and simple variable (depends on the code of a function). 2. Let there is a default (sleep) mutex and adaptive mutexes is enabled. A thread tries to obtain lock quickly and fails, _mtx_lock_sleep() is called, it gets the address of the current mutex's owner thread and checks whether that owner thread is running (on another CPU). How does _mtx_lock_sleep() know that that thread still exists (lines 311-337 in kern_mutex.c)? When adaptive mutexes was implemented there was explicit locking around adaptive mutexes code. When turnstile in mutex code was implemented that's locking logic was changed. 3. Why there is no any memory barrier in mtx_init()? If another thread (on another CPU) finds that mutex is initialized using mtx_initialized() then it can mtx_lock() it and mtx_lock() it second time, as a result mtx_recurse field will be increased, but its value still can be uninitialized on architecture with relaxed memory ordering model. Thanks.