Re: initialize a mutex into locked state?

From: Peter Zijlstra
Date: Fri Jun 17 2016 - 10:19:25 EST


On Fri, Jun 17, 2016 at 10:14:10AM -0400, Oleg Drokin wrote:
>
> On Jun 17, 2016, at 4:25 AM, Peter Zijlstra wrote:
>
> > On Wed, Jun 15, 2016 at 02:23:35PM -0400, Oleg Drokin wrote:
> >> Hello!
> >>
> >> To my surprise I found out that it's not possible to initialise a mutex into
> >> a locked state.
> >> I discussed it with Arjan and apparently there's no fundamental reason
> >> not to allow this.
> >
> > There is. A mutex _must_ have an owner. If you can initialize it in
> > locked state, you could do so statically, ie. outside of the context of
> > a task.
>
> What's wrong with disallowing only static initializers, but allowing dynamic ones?
> Then there is a clear owner.

At which point, what wrong with the simple:

mutex_init(&m);
mutex_lock(&m);

Sequence? Its obvious, has clear semantics and doesn't extend the API.