Re: Why mtx_sleep returning EWOUNDBLOCK?

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Mon, 02 May 2022 21:05:57 UTC
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