Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 11 Sep 2013 17:39:03 -0400
From:      Dheeraj Kandula <dkandula@gmail.com>
To:        freebsd-arch@freebsd.org
Subject:   Why do we need to acquire the current thread's lock before context switching?
Message-ID:  <CA%2BqNgxSVkSi88UC3gmfwigmP0UCO6dz%2B_Zxhf_=URK7p4c-Ghg@mail.gmail.com>

next in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CA%2BqNgxSVkSi88UC3gmfwigmP0UCO6dz%2B_Zxhf_=URK7p4c-Ghg>