From b3a20a96eb2b91a5b0b8a8fb506e20a2fb50ebe8 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 30 Dec 2020 00:10:11 -0500 Subject: libthread: add threadmaybackground Programs that want to background themselves now need to define threadmaybackground returning 1. This avoids a confusing (to people and debuggers) extra parent process for all the threaded programs that will never want to background themselves. --- man/man3/thread.3 | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) (limited to 'man/man3/thread.3') diff --git a/man/man3/thread.3 b/man/man3/thread.3 index 6c6b4602..02e4c868 100644 --- a/man/man3/thread.3 +++ b/man/man3/thread.3 @@ -33,6 +33,7 @@ threadintgrp, threadkill, threadkillgrp, threadmain, +threadmaybackground, threadnotify, threadid, threadpid, @@ -80,6 +81,7 @@ struct Alt { .ft L .ta \w'\fLChannel* 'u +4n +4n +4n +4n void threadmain(int argc, char *argv[]) +int threadmaybackground(void) int mainstacksize int proccreate(void (*fn)(void*), void *arg, uint stacksize) int threadcreate(void (*fn)(void*), void *arg, uint stacksize) @@ -171,7 +173,7 @@ initialized to the desired value .BR 1024 ). When using the .I pthread -library, +library, .B mainstacksize is ignored, as is the stack size argument to .BR proccreate : @@ -185,7 +187,7 @@ executes .I fn(arg) on a stack of size .IR stacksize . -Thread stacks are allocated in shared memory, making it valid to pass +Thread stacks are allocated in shared memory, making it valid to pass pointers to stack variables between threads and procs. .I Proccreate creates a new proc, and inside that proc creates @@ -207,7 +209,7 @@ returning the id of the created thread. .\" in .\" .IR rforkflag .) .\" .I Proccreate -.\" is identical to +.\" is identical to .\" .I procrfork .\" with .\" .I rforkflag @@ -238,6 +240,14 @@ When the last thread in .IR threadmain 's proc exits, the program will appear to its parent to have exited. The remaining procs will still run together, but as a background program. +This functionality can only be relied upon if the program defines a function +.I threadmaybackground +returning a non-zero result. +Programs that do not define such a +.I threadmaybackground +will crash instead should the last thread in +.IR threadmain 's +proc exit leaving behind other running procs. .PP The threads in a proc are coroutines, scheduled nonpreemptively in a round-robin fashion. @@ -341,18 +351,18 @@ Also for debugging, threads have a string state associated with them. .I Threadsetstate sets the state string. -There is no +There is no .IR threadgetstate ; since the thread scheduler resets the state to .B Running -every time it runs the thread, +every time it runs the thread, it is only useful for debuggers to inspect the state. .PP .I Threaddata returns a pointer to a per-thread pointer that may be modified by threaded programs for per-thread storage. -Similarly, +Similarly, .I procdata returns a pointer to a per-proc pointer. .PP @@ -398,11 +408,11 @@ response. .I Threadexecl and .I threadexec -will duplicate +will duplicate (see .MR dup (3) ) the three file descriptors in -.I fd +.I fd onto standard input, output, and error for the external program and then close them in the calling thread. Beware of code that sets @@ -467,9 +477,9 @@ operation blocks until the corresponding operation occurs and .IR "vice versa" . .IR Chancreate -allocates a new channel +allocates a new channel for messages of size -.I elsize +.I elsize and with a buffer holding .I nel messages. @@ -645,7 +655,7 @@ from the main proc before any other procs have been created. To create new processes, use .IR proccreate . .\" .PP -.\" It is safe to use +.\" It is safe to use .\" .IR rfork .\" (see .\" .IR fork (3)) @@ -663,7 +673,7 @@ To create new processes, use .\" .BR RFCENVG. .\" (To create new processes, use .\" .I proccreate -.\" and +.\" and .\" .IR procrfork .) .\" As mentioned above, .\" the thread library depends on all procs being in the -- cgit v1.2.3