Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 2 May 2022 14:05:57 -0700
From:      John Baldwin <jhb@FreeBSD.org>
To:        Farhan Khan <farhan@farhan.codes>, freebsd-drivers@freebsd.org
Subject:   Re: Why mtx_sleep returning EWOUNDBLOCK?
Message-ID:  <200b4f6d-b533-042c-a411-1c100ed0b48b@FreeBSD.org>
In-Reply-To: <706b2fc4-ce74-8716-d5c6-092d69c9c0fc@farhan.codes>
References:  <706b2fc4-ce74-8716-d5c6-092d69c9c0fc@farhan.codes>

next in thread | previous in thread | raw e-mail | index | archive | help
On 3/13/22 1:02 PM, Farhan Khan wrote:
> Hi all,
> 
> Summary: I am running mtx_sleep() but getting an EWOUNDBLOCK response
> code and do not understand why.
> 
> I am looking at port code from the OpenBSD side to FreeBSD. I am
> assuming that tsleep_nsec()'s equivalent on FreeBSD is mtx_sleep(). To
> that end, I am running mtx_sleep as this:
> 
> mtx_lock(&(sc)->sc_mtx);
> error = mtx_sleep(sc, &sc->sc_mtx, 0 , "athnfw", hz);
> mtx_unlock(&(sc)->sc_mtx);
> 
> However, the error code returns a EWOUNDBLOCK. I would expect this to be
> 0. From reading the man page for sleep(9), this means "A non-zero
> timeout was specified and the timeout". But if I slept for 1 hz, isn't
> that exactly what I want and thus it should return 0 (no error)?
> 
> The OpenBSD line in question is:
> 
> error = tsleep_nsec(&usc->wait_msg_id, 0, "athnfw", SEC_TO_NSEC(1));
> 
> Perhaps there is something I am not understanding here? Please advise.
> Thanks!

error == 0 means you were awakened by an explicit wakeup() rather than
a timeout (EWOULDBLOCK).  If we returned 0 for timeouts the caller would
have no way to determine if a timeout had occurred or not.  If you as
the caller don't view timeouts as fatal, you can explicitly map
EWOULDBLOCK to 0.

-- 
John Baldwin



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200b4f6d-b533-042c-a411-1c100ed0b48b>