From owner-freebsd-hackers@freebsd.org Thu Apr 6 09:16:48 2017 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 82A09D3087F for ; Thu, 6 Apr 2017 09:16:48 +0000 (UTC) (envelope-from ablacktshirt@gmail.com) Received: from mail-pg0-x22f.google.com (mail-pg0-x22f.google.com [IPv6:2607:f8b0:400e:c05::22f]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4A303A3B for ; Thu, 6 Apr 2017 09:16:48 +0000 (UTC) (envelope-from ablacktshirt@gmail.com) Received: by mail-pg0-x22f.google.com with SMTP id x125so30745391pgb.0 for ; Thu, 06 Apr 2017 02:16:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-transfer-encoding; bh=eVO+UCjZhI5XR1rXBA3ZpDqXHsskPkTAZ6a/eefct7o=; b=u9GNaI94oeOqNcY6X0wfE1j+2dGq4xoAkBb2CgV45ftQu9yL3k594JJTXYjtQMdXg4 n7FU2Cvfgylwe2c7an4iV03CE8lo91Sdx8rMwa8OrPujiWfctwnQUTqX769gH7uBuvvc uVYVo7y2pg6AOljyMNwEBJWiz1is8aWC8JQWi45E/t6IkQiXhdqhI7GW1jPqZ/ydjZC2 tSRYCuH3TpRSxP8cOGYWLzje0TDez8IFfDqNYJSqaU9w+bSnKMWQu4vBdn/qShCVxeeU vIxInh5RkLLozul0ndEJNbJIbdzcLhM1EVF6ZcCkEA5mKSUc/5EZcZvnkGeZFDSdbzvj s3Ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-transfer-encoding; bh=eVO+UCjZhI5XR1rXBA3ZpDqXHsskPkTAZ6a/eefct7o=; b=IvOgF3V7JpktknGUYv5CpDzNMNatQ1cb2ZA224wgUGjhJ1EL4ZHQ96hZ97AZANRQpB 9tNvTeFpdatTNbMsBOB7OQl1NQI18lvphvaU7TUXx8ULIt7fldkjJpEBUR2obiv6GCZ9 Hx7BZ+6zqRgMClopMallpxi5y4QiE7nLUSK6uwBaC7dbLYar4CasB37OkxBxucrkN80c Pj/ZqRn3YmHtz6g2YRIMq0OKuAFOQzcmIlCaMZSMTUawRy4NwwvxMIYN/AtnkF3w3dra wHLBnYbE+Ak3geZpFyMn2lETfihuJp6/+yD4oiFwErtQHKcNdSPZhjP29nHPAMNbTV4J TPPw== X-Gm-Message-State: AFeK/H1j3WzviPjmA/scavM8/O5FNiM/zoq8IPeeW+351VvsA7B0wHl3UMVguKaBiYaOAA== X-Received: by 10.98.75.221 with SMTP id d90mr33538287pfj.107.1491470207574; Thu, 06 Apr 2017 02:16:47 -0700 (PDT) Received: from [192.168.2.211] ([116.56.129.146]) by smtp.gmail.com with ESMTPSA id x204sm2052082pgx.63.2017.04.06.02.16.45 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 06 Apr 2017 02:16:46 -0700 (PDT) To: freebsd-hackers@freebsd.org From: Yubin Ruan Subject: Understanding the FreeBSD locking mechanism Message-ID: Date: Thu, 6 Apr 2017 17:16:45 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 06 Apr 2017 09:16:48 -0000 Hi all freebsd hackers, I am reading the FreeBSD source code related to its locking mechanism. I have done some researches but still cannot understand some codes. Let's take `spinlock' for example. I know there are different kinds of mutex in FreeBSD: spin mutex and other kinds of mutex. I try to locate the source of spin mutex but what I find all look very weird to me. For example, the `spinlock_enter()` 1816 void 1817 spinlock_enter(void) 1818 { 1819 struct thread *td; 1820 register_t flags; 1821 1822 td = curthread; 1823 if (td->td_md.md_spinlock_count == 0) { 1824 flags = intr_disable(); 1825 td->td_md.md_spinlock_count = 1; 1826 td->td_md.md_saved_flags = flags; 1827 } else 1828 td->td_md.md_spinlock_count++; 1829 critical_enter(); 1830 } Does this function provides the ordinary "spinlock" functionality? There is no special "test-and-set" instruction, and neither any extra locking to protect internal data structure manipulation. Isn't this subjected to race condition? I also checked the `mtx_lock()`, but neither can't find a seemingly correct implementation. Do I miss anything? Which is the real implementation of the spin lock in FreeBSD? Thanks Yubin Ruan