Perform some code maintenance

- Change IDT code so kprintf() isn't mandatory dependency
- Document current intentions around pthread_cancel()
- Make _npassert() an _unassert() in MODE=tiny
This commit is contained in:
Justine Tunney 2022-10-09 13:00:46 -07:00
parent 4a6fd3d910
commit 9b7c8db846
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
7 changed files with 85 additions and 49 deletions

View file

@ -16,9 +16,52 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/errno.h"
#include "libc/intrin/kprintf.h"
#include "libc/runtime/runtime.h"
#include "libc/thread/thread.h"
/**
* Cancels thread.
*
* This function currently isn't supported. In order to support this
* function we'd need to redesign the system call interface, and add
* bloat and complexity to every function that can return EINTR. You
* might want to consider using `nsync_note` instead, which provides
* much better cancellations because posix cancellations is a broken
* design. If you need to cancel i/o operations, try doing this:
*
* _Thread_local bool gotusr1;
* void OnUsr1(int sig) { gotusr1 = true; }
* struct sigaction sa = {.sa_handler = OnUsr1};
* sigaction(SIGUSR1, &sa, 0);
* tkill(pthread_getunique_np(thread), SIGUSR1);
*
* The above code should successfully cancel a thread's blocking io
* operations in most cases, e.g.
*
* void *MyThread(void *arg) {
* sigset_t ss;
* sigfillset(&ss);
* sigdelset(&ss, SIGUSR1);
* sigprocmask(SIG_SETMASK, &ss, 0);
* while (!gotusr1) {
* char buf[512];
* ssize_t rc = read(0, buf, sizeof(buf));
* if (rc == -1 && errno == EINTR) continue;
* write(1, buf, rc);
* }
* return 0;
* }
*
* This has the same correctness issue as glibc, but it's usually
* "good enough" if you only need cancellations to perform things
* like server shutdown and socket options like `SO_RCVTIMEO` can
* ensure it's even safer, since it can't possibly block forever.
*
* @see https://sourceware.org/bugzilla/show_bug.cgi?id=12683
*/
int pthread_cancel(pthread_t thread) {
return ESRCH;
kprintf("error: pthread_cancel() is unsupported, please read the "
"cosmopolitan libc documentation for further details\n");
_Exit(1);
}