From owner-freebsd-arch@FreeBSD.ORG Thu Sep 12 10:48:43 2013 Return-Path: Delivered-To: freebsd-arch@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id F05D9410 for ; Thu, 12 Sep 2013 10:48:43 +0000 (UTC) (envelope-from dkandula@gmail.com) Received: from mail-wg0-x22e.google.com (mail-wg0-x22e.google.com [IPv6:2a00:1450:400c:c00::22e]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 8C73A280C for ; Thu, 12 Sep 2013 10:48:43 +0000 (UTC) Received: by mail-wg0-f46.google.com with SMTP id k14so9020242wgh.25 for ; Thu, 12 Sep 2013 03:48:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=COD0z2wNo/BAOP8SWzX0ACIJEl7R5g+DCQoA/kNjtw4=; b=tuM+4WGJ0/XIn05txvoLtNfCprHbD5zaIXbn6Jc/PjoZse3nGBCg4M4l7rHmSqf3fQ 6qRTpBS8mnCiXm+Aj1EPY2klqK6yIi9hVDlQ0au4J/RWk/AiY1h6WZKGRdKVd7bQ1/Sb rDuL/veER9XpNsVUyc4J26eSnUvYESoa46B8igEtfVMiv2vLocpgxDlM+JoGQXw4TIjx mWeOwpxu9DxPFS0LJkbeiFppaaGciuHeWKS0fKIfktfAB+l9OV9hXTpJf34iVD378Nbq LffS2gxpWiYZDS69skxS1wC/euPEad1ESsJtASMR9d6PRvUNYBJZU679SjgfsENHUqlz vhrA== MIME-Version: 1.0 X-Received: by 10.194.219.1 with SMTP id pk1mr5517985wjc.36.1378982922076; Thu, 12 Sep 2013 03:48:42 -0700 (PDT) Received: by 10.194.38.167 with HTTP; Thu, 12 Sep 2013 03:48:42 -0700 (PDT) In-Reply-To: <523168EE.4070508@mu.org> References: <523168EE.4070508@mu.org> Date: Thu, 12 Sep 2013 06:48:42 -0400 Message-ID: Subject: Re: Why do we need to acquire the current thread's lock before context switching? From: Dheeraj Kandula To: Alfred Perlstein Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.14 Cc: freebsd-arch@freebsd.org 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: Thu, 12 Sep 2013 10:48:44 -0000 Thanks a lot Alfred for the clarification. So is the td_lock granular i.e. one separate lock for each thread but also used for protecting the scheduler variables or is it just one lock used by all threads and the scheduler as well. I will anyway go through the code that you suggested but just wanted to have a deeper understanding before I go about hunting in the code. Dheeraj On Thu, Sep 12, 2013 at 3:10 AM, Alfred Perlstein wrote: > On 9/11/13 2:39 PM, Dheeraj Kandula wrote: > >> 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 thre= ad >> in order to demand a lock for the thread which will basically protect th= e >> contents of the thread structure for the thread. >> >> Dheeraj >> >> > The thread lock also happens to protect various scheduler variables: > > struct mtx *volatile td_lock; /* replaces sched lock */ > > see sys/kern/sched_ule.c on how the thread lock td_lock is changed > depending on what the thread is doing. > > -- > Alfred Perlstein > >