Skip to content

Commit 9ba5b22

Browse files
committed
Unblock stalled i/o signals on windows
1 parent aca4214 commit 9ba5b22

File tree

4 files changed

+43
-50
lines changed

4 files changed

+43
-50
lines changed

libc/intrin/mmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ static void *__mmap_impl(char *addr, size_t size, int prot, int flags, int fd,
660660
if (!IsWindows()) {
661661
struct Map *deleted = 0;
662662
__maps_lock();
663-
if (IsWindows() || fixedmode)
663+
if (fixedmode)
664664
if (__muntrack(res.addr, size, &deleted, 0, 0))
665665
STRACE("memtrack compromised by hole punch oom");
666666
__maps_insert(map);

libc/intrin/sig.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,8 @@ textwindows void __sig_generate(int sig, int sic) {
492492
__sig_terminate(sig);
493493
}
494494
if (atomic_load_explicit(__sig.process, memory_order_acquire) &
495-
(1ull << (sig - 1))) {
495+
(1ull << (sig - 1)))
496496
return;
497-
}
498497
_pthread_lock();
499498
for (e = dll_first(_pthread_list); e; e = dll_next(_pthread_list, e)) {
500499
pt = POSIXTHREAD_CONTAINER(e);
@@ -503,9 +502,8 @@ textwindows void __sig_generate(int sig, int sic) {
503502
continue;
504503
// we don't want to signal a thread that isn't running
505504
if (atomic_load_explicit(&pt->pt_status, memory_order_acquire) >=
506-
kPosixThreadTerminated) {
505+
kPosixThreadTerminated)
507506
continue;
508-
}
509507
// choose this thread if it isn't masking sig
510508
if (!(atomic_load_explicit(&pt->tib->tib_sigmask, memory_order_acquire) &
511509
(1ull << (sig - 1)))) {
@@ -756,11 +754,26 @@ HAIRY static uint32_t __sig_worker(void *arg) {
756754
__sig_generate(sig, SI_KERNEL);
757755
}
758756

757+
// unblock stalled i/o signals in threads
758+
_pthread_lock();
759+
for (struct Dll *e = dll_first(_pthread_list); e;
760+
e = dll_next(_pthread_list, e)) {
761+
struct PosixThread *pt = POSIXTHREAD_CONTAINER(e);
762+
if (atomic_load_explicit(&pt->pt_status, memory_order_acquire) >=
763+
kPosixThreadTerminated)
764+
break;
765+
if (atomic_load_explicit(&pt->pt_blocker, memory_order_acquire) &&
766+
(atomic_load_explicit(&pt->tib->tib_sigpending,
767+
memory_order_acquire) &
768+
~atomic_load_explicit(&pt->pt_blkmask, memory_order_acquire)))
769+
__sig_wake(pt, 0);
770+
}
771+
_pthread_unlock();
772+
759773
// unblock stalled asynchronous signals in threads
760-
struct PosixThread *mark;
761774
for (;;) {
762775
sigset_t pending, mask;
763-
mark = 0;
776+
struct PosixThread *mark = 0;
764777
_pthread_lock();
765778
for (struct Dll *e = dll_first(_pthread_list); e;
766779
e = dll_next(_pthread_list, e)) {
@@ -790,6 +803,7 @@ HAIRY static uint32_t __sig_worker(void *arg) {
790803
pending &= ~(1ull << (sig - 1));
791804
__sig_killer(mark, sig, SI_KERNEL);
792805
}
806+
_pthread_unref(mark);
793807
}
794808

795809
// wait until next scheduler quantum

test/posix/sa_resethand2_test.c

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
1-
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
2-
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
3-
╞══════════════════════════════════════════════════════════════════════════════╡
4-
│ Copyright 2023 Justine Alexandra Roberts Tunney │
5-
│ │
6-
│ Permission to use, copy, modify, and/or distribute this software for │
7-
│ any purpose with or without fee is hereby granted, provided that the │
8-
│ above copyright notice and this permission notice appear in all copies. │
9-
│ │
10-
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
11-
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
12-
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
13-
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
14-
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
15-
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
16-
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
17-
│ PERFORMANCE OF THIS SOFTWARE. │
18-
╚─────────────────────────────────────────────────────────────────────────────*/
19-
#include "libc/calls/calls.h"
20-
#include "libc/calls/struct/sigaction.h"
21-
#include "libc/calls/struct/sigset.h"
22-
#include "libc/dce.h"
23-
#include "libc/sysv/consts/sa.h"
24-
#include "libc/sysv/consts/sig.h"
1+
// Copyright 2024 Justine Alexandra Roberts Tunney
2+
//
3+
// Permission to use, copy, modify, and/or distribute this software for
4+
// any purpose with or without fee is hereby granted, provided that the
5+
// above copyright notice and this permission notice appear in all copies.
6+
//
7+
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
8+
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
9+
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
10+
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
11+
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
12+
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
13+
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14+
// PERFORMANCE OF THIS SOFTWARE.
15+
16+
#include <signal.h>
17+
#include <unistd.h>
2518

2619
volatile int handler_invoked;
2720

@@ -33,24 +26,17 @@ int main() {
3326
sigset_t mask, oldmask;
3427
struct sigaction sa, current_sa;
3528

36-
if (IsWindows()) {
37-
// TODO(jart): support non-fatal signals between processes
38-
return 0;
39-
}
40-
4129
sa.sa_handler = signal_handler;
4230
sa.sa_flags = SA_RESETHAND;
4331
sigemptyset(&sa.sa_mask);
4432

45-
if (sigaction(SIGINT, &sa, 0) == -1) {
33+
if (sigaction(SIGINT, &sa, 0) == -1)
4634
return 1;
47-
}
4835

4936
sigemptyset(&mask);
5037
sigaddset(&mask, SIGINT);
51-
if (sigprocmask(SIG_BLOCK, &mask, &oldmask) == -1) {
38+
if (sigprocmask(SIG_BLOCK, &mask, &oldmask) == -1)
5239
return 2;
53-
}
5440

5541
int pid = fork();
5642
if (pid == -1) {
@@ -60,15 +46,12 @@ int main() {
6046
return 0;
6147
} else {
6248
sigsuspend(&oldmask);
63-
if (!handler_invoked) {
49+
if (!handler_invoked)
6450
return 4;
65-
}
66-
if (sigaction(SIGINT, 0, &current_sa) == -1) {
51+
if (sigaction(SIGINT, 0, &current_sa) == -1)
6752
return 5;
68-
}
69-
if (current_sa.sa_handler != SIG_DFL) {
53+
if (current_sa.sa_handler != SIG_DFL)
7054
return 6;
71-
}
7255
return 0;
7356
}
7457
}

test/posix/signal_latency_test.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,6 @@ int main() {
133133
if (IsOpenbsd())
134134
return 0;
135135

136-
// TODO(jart): Why is this test flaky on Windows?
137-
if (IsWindows())
138-
return 0;
139-
140136
// Block SIGUSR1 and SIGUSR2 in main thread
141137
sigset_t block_set;
142138
sigemptyset(&block_set);

0 commit comments

Comments
 (0)