Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 16 Sep 2010 23:24:29 -0400 (EDT)
From:      Benjamin Kaduk <kaduk@MIT.EDU>
To:        John Baldwin <jhb@freebsd.org>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: Questions about mutex implementation in kern/kern_mutex.c
Message-ID:  <alpine.GSO.1.10.1009162317430.9337@multics.mit.edu>
In-Reply-To: <201009161416.05759.jhb@freebsd.org>
References:  <20100915134415.GA23727@pm513-1.comsys.ntu-kpi.kiev.ua> <AANLkTimJV-oB_uTSbUTtbSrR5fXgWGk00dEV7L-Gobrf@mail.gmail.com> <20100916173307.GA1994@pm513-1.comsys.ntu-kpi.kiev.ua> <201009161416.05759.jhb@freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 16 Sep 2010, John Baldwin wrote:

> On Thursday, September 16, 2010 1:33:07 pm Andrey Simonenko wrote:
>
>> The mtx_owned(9) macro uses this property, mtx_owned() does not use anything
>> special to compare the value of m->mtx_lock (volatile) with current thread
>> pointer, all other functions that update m->mtx_lock of unowned mutex use
>> compare-and-set instruction.  Also I cannot find anything special in
>> generated Assembler code for volatile variables (except for ia64 where
>> acquire loads and release stores are used).
>
> No, mtx_owned() is just not harmed by the races it loses.  You can certainly
> read a stale value of mtx_lock in mtx_owned() if some other thread owns the
> lock or has just released the lock.  However, we don't care, because in both
> of those cases, mtx_owned() returns false.  What does matter is that
> mtx_owned() can only return true if we currently hold the mutex.  This works
> because 1) the same thread cannot call mtx_unlock() and mtx_owned() at the
> same time, and 2) even CPUs that hold writes in store buffers will snoop their
> store buffer for local reads on that CPU.  That is, a given CPU will never
> read a stale value of a memory word that is "older" than a write it has
> performed to that word.

Sorry for the naive question, but would you mind expounding a bit on what 
keeps the thread from migrating to a different CPU and getting a stale 
value there?  (I can imagine a couple possible mechanisms, but don't know 
enough to know which one(s) are the real ones.)

Thanks,

Ben Kaduk



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?alpine.GSO.1.10.1009162317430.9337>