From owner-freebsd-hackers@freebsd.org Thu Apr 6 09:31:54 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 BDE20D30FBE for ; Thu, 6 Apr 2017 09:31:54 +0000 (UTC) (envelope-from ed@nuxi.nl) Received: from mail-yb0-x22d.google.com (mail-yb0-x22d.google.com [IPv6:2607:f8b0:4002:c09::22d]) (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 8852B287 for ; Thu, 6 Apr 2017 09:31:54 +0000 (UTC) (envelope-from ed@nuxi.nl) Received: by mail-yb0-x22d.google.com with SMTP id i124so9087440ybc.3 for ; Thu, 06 Apr 2017 02:31:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nuxi-nl.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=eRhB0bem/7gJB/0kiC6FRHJ8I8FzCMOr2/pcdmoYpFQ=; b=LOBhCltmBRh46EkaCf0Af51cgBPPhQhboi1K5lbr1TIANJsyMvthHb9KZwgn0Q4HJU dUhSGLd6Kbm3hlepY0E7pjMTEq4RyoWyOUPj4I6Ap7ssPgkRTjxZmK4mbQ3+UHVrpw06 PJkUOl6/YneWGqCrdlx2YQYf5lXqUvDk77/VYG2DU6t3syCjGDyACobWrXJOREuaUGvC nbw7ACTqA9vGgvxdmob2kj3mMIv/TJjeZu1JUx7puFu3URIy7GVfbXHrSHNDxVeMCozm xIa1gaO5OreYIzDUFo9d+ZNH7bcHVyOya8nhdknLlT5rihx98tBQfJuf2WwM5V6++LhA zZaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=eRhB0bem/7gJB/0kiC6FRHJ8I8FzCMOr2/pcdmoYpFQ=; b=cEIbFvDwwJHOrWT13E0V38M4470gMw6eEwGnObgd6BtdLLjd7t/jM6WXXy6MpFAfWd e66V49o1WxQIMWfjFuit6Ibb+0W33IDr4NM8+3OwuFlsLfp6XFEUctovUnqPyf7J8DQu vuHkyTskWY470lnJIgyf7WcNSFWhMEuYXfW5j/Lu16je0yA0L8UEmc8Wnhh98txI3kOd THQbwPyKPUapew0v8hxyno07/QQY8+Dxr/rNTb4sDxsaqvRSXoRupll4vn6sast96A0f xImrC6FF88zeRav8IHUGn1c6Y2h0fuj9WODnAoQFD9u8zY2Qd6YLwyXzZYxRituDRL5D sqRw== X-Gm-Message-State: AFeK/H361btvKHMoNQsj0qlsAKYiX8ysyo6w71QTibRkBvkadgkZ3Zp8w9zqPm4WFYIUb2zMvRWnGP1SGMcGpg== X-Received: by 10.37.230.143 with SMTP id d137mr21197755ybh.168.1491471113353; Thu, 06 Apr 2017 02:31:53 -0700 (PDT) MIME-Version: 1.0 Received: by 10.129.51.198 with HTTP; Thu, 6 Apr 2017 02:31:23 -0700 (PDT) In-Reply-To: References: From: Ed Schouten Date: Thu, 6 Apr 2017 11:31:23 +0200 Message-ID: Subject: Re: Understanding the FreeBSD locking mechanism To: Yubin Ruan Cc: FreeBSD Hackers Content-Type: text/plain; charset=UTF-8 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:31:54 -0000 Hi Yubin, 2017-04-06 11:16 GMT+02:00 Yubin Ruan : > 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? Locking a spinlock is done through macro mtx_lock_spin(), which expands to __mtx_lock_spin() in sys/sys/mutex.h. That macro first calls into the function you looked at, spinlock_enter(), to disable interrupts. It then calls into the _mtx_obtain_lock_fetch() to do the test-and-set operation you were looking for. Best regards, -- Ed Schouten Nuxi, 's-Hertogenbosch, the Netherlands KvK-nr.: 62051717