From owner-freebsd-arch@FreeBSD.ORG Wed Sep 11 21:39:05 2013 Return-Path: Delivered-To: freebsd-arch@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id 6A7ED266 for ; Wed, 11 Sep 2013 21:39:05 +0000 (UTC) (envelope-from dkandula@gmail.com) Received: from mail-wg0-x232.google.com (mail-wg0-x232.google.com [IPv6:2a00:1450:400c:c00::232]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 0962C2A5F for ; Wed, 11 Sep 2013 21:39:04 +0000 (UTC) Received: by mail-wg0-f50.google.com with SMTP id j13so8493348wgh.29 for ; Wed, 11 Sep 2013 14:39:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=PtY6xIm59GVaVvvUXwEuG8d/LB1MzYwapg9H03jtcMc=; b=XIg/RuwkguFkTgkzblg4lBMipPe5S76hyKq+tTrOHpU5+MzvybhEtMpq6qhfmf/0kn sMNRqbq/dCT2fiyUmsk/TRe6bhHBN5qskjvXIftbz5UH37lXuD5NFI21MaIQeNH+8rKD 0Z0Ytz1GpFv5tm8MoE56VvwzPAezJftRmcWwibKA5uryXCvrTL7/ml42n4d+z5DK3Ky2 VbJfT4TME3kepU9OACCs4GnSb/9ZGYiv9AxQ32/nV4GTaEY3fr3hzM+CwcM3ICGH6ui1 LnzB6ifpo6BcIjlwGuAUAugXmFgLDOxxVOdcMxKzJNwXc+UMnqDfG9Dx/U6v/1n0ZLS6 vnrA== MIME-Version: 1.0 X-Received: by 10.180.13.174 with SMTP id i14mr19163241wic.49.1378935543574; Wed, 11 Sep 2013 14:39:03 -0700 (PDT) Received: by 10.194.38.167 with HTTP; Wed, 11 Sep 2013 14:39:03 -0700 (PDT) Date: Wed, 11 Sep 2013 17:39:03 -0400 Message-ID: Subject: Why do we need to acquire the current thread's lock before context switching? From: Dheeraj Kandula To: freebsd-arch@freebsd.org Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.14 X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 Sep 2013 21:39:05 -0000 Hey All, When the current thread is being context switched with a newly selected thread, why is the current thread's lock acquired before context switch =96 mi_switch() is invoked after thread_lock(td) is called. A thread at any time runs only on one of the cores of a CPU. Hence when it is being context switched it is added either to the real time runq or the timeshare runq or the idle runq with the lock still held or it is added to the sleep queue or the blocked queue. So this happens atomically even without the lock. Isn't it? Am I missing something here? I don't see any contention for the thread in order to demand a lock for the thread which will basically protect the contents of the thread structure for the thread. Dheeraj