NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: misc/48671: Add description how to convert from tsleep(9)/wakeup(9) to condvar(9)

On 03/21/14 08:25, Izumi Tsutsui wrote:
Number:         48671
Category:       misc
Synopsis:       Add description how to convert from tsleep(9)/wakeup(9) to 

Good idea.

I've updated your patch a little.

Index: share/man/man9/ltsleep.9
RCS file: /cvsroot/src/share/man/man9/ltsleep.9,v
retrieving revision 1.14
diff -u -p -r1.14 ltsleep.9
--- share/man/man9/ltsleep.9    28 Jan 2012 13:26:12 -0000      1.14
+++ share/man/man9/ltsleep.9    21 Mar 2014 16:29:53 -0000
@@ -32,12 +32,15 @@
 .Nm ltsleep ,
+.Nm mtsleep ,
 .Nm tsleep ,
 .Nm wakeup
 .Nd process context sleep and wakeup
 .In sys/proc.h
 .Ft int
+.Fn "mtsleep" "wchan_t ident" "pri_t priority" "const char *wmesg" "int timo" 
"kmutex_t *mtx"
+.Ft int
 .Fn "tsleep" "wchan_t ident" "pri_t priority" "const char *wmesg" "int timo"
 .Ft void
 .Fn "wakeup" "wchan_t ident"
@@ -58,7 +61,9 @@
 These functions implement voluntary context switching.
 .Fn tsleep
-is used throughout the kernel whenever processing in the current context
+.Fn mtsleep
+are used throughout the kernel whenever processing in the current context
 can not continue for any of the following reasons:
 .Bl -bullet -offset indent
@@ -67,9 +72,6 @@ The current process needs to await the r
 The current process needs resources
 .Pq e.g., memory
 which are temporarily unavailable.
-The current process wants access to data-structures which are locked by
-other processes.
 The function
@@ -83,7 +85,9 @@ condition has cleared.
 .Fn tsleep
-function takes the following arguments:
+.Fn mtsleep
+functions take the following arguments:
 .Bl -tag -width priority
 .It Fa ident
 An identifier of the
@@ -129,31 +133,52 @@ will return
+.Fn mtsleep
+function takes an additional argument and flag:
+.Bl -tag -width priority
+.It Fa mtx
+.Xr mutex 9
+representing the lock protecting the data-structures. On entry
+.Fn mtsleep
+will release the lock and re-acquire the lock on return.
+.It Fa priority
+If the flag
+is OR'ed into
+.Fa priority
+.Fn mtsleep
+will not re-acquire the lock.
 .Fn wakeup
 function will mark all processes which are currently sleeping on the identifier
 .Fa ident
 as runnable.
 Eventually, each of the processes will resume execution in the kernel
 context, causing a return from
-.Fn tsleep .
+.Fn tsleep
+.Fn mtsleep .
 Note that processes returning from sleep should always re-evaluate the
 conditions that blocked them, since a call to
 .Fn wakeup
 merely signals a
 .Em possible
 change to the blocking conditions.
-For example, when two or more processes are waiting for an exclusive-access
-.Pq see Xr lock 9 ,
-only one of them will succeed in acquiring the lock when it is released.
-All others will have to go back to sleep and wait for the next opportunity.
 .Fn tsleep
-returns 0 if it returns as a result of a
+.Fn mtsleep
+return 0 if they return as a result of a
 .Fn wakeup .
 If a
 .Fn tsleep
-returns as a result of a signal, the return value is
+.Fn mtsleep
+return as a result of a signal, the return value is
 if the signal has the
@@ -164,13 +189,85 @@ and
 .Fn tsleep
+.Fn mtsleep
 returns because of a timeout it returns
+Note the conversion from tsleep/wakeup into
+.Xr condvar 9
+should not be done mechanically i.e.
+.Dq blindly .
+Code logic should be understood before changing, and it may also need to be
+revisited for the change.
+Please also read
+.Xr condvar 9
+man page.
+.Fn tsleep
+.Fn mtsleep ,
+.Fn wakeup
+pairs should generally be replaced by
+.Xr cv_wait 9 /
+.Xr cv_wait_sig 9 /
+.Xr cv_timedwait 9 /
+.Xr cv_timedwait_sig 9
+.Xr cv_signal 9 /
+.Xr cv_broadcast 9
+.Fn cv_wait*
+variant to use can be determinded from looking at the corresponding
+.Fn tsleep
+There are two arguments of interest:
+.Ar timo
+.Ar priority .
+.Ar priority
+value may have OR'ed the flag
+flag means that the blocking thread should be awoken on signal, and
+the sleep call should be replaced with
+.Xr cv_wait_sig 9 .
+.Ar timo
+value, if it is not zero, indicates how long to sleep, and
+the sleep call should be replaced with
+.Xr cv_timedwait 9 .
+If both the
+flag and a non-zero
+.Ar timo
+value are specified, then
+.Xr cv_timedwait_sig 9
+should be used.
+.Xr mutex 9
+(interlock) must be held across
+.Fn cv_wait
+.Fn cv_broadcast
+calls, in order to protect state.
+Most old code will require the addition of locking, whereas some will
+require amending to remove
 .Xr sigaction 2 ,
 .Xr condvar 9 ,
 .Xr hz 9 ,
-.Xr lock 9 ,
 .Xr mutex 9 ,
 .Xr rwlock 9

Home | Main Index | Thread Index | Old Index