Skip to content

Commit

Permalink
Cygwin: cygwait: Make cygwait() reentrant
Browse files Browse the repository at this point in the history
To allow cygwait() to be called in the signal handler, a locally
created timer is used instead of _cygtls::locals.cw_timer if it is
in use.

Co-Authored-By: Corinna Vinschen <[email protected]>
Signed-off-by: Takashi Yano <[email protected]>
  • Loading branch information
2 people authored and jeremyd2019 committed Jan 20, 2025
1 parent 8b6f316 commit 96aae8a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
2 changes: 2 additions & 0 deletions winsup/cygwin/cygtls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ _cygtls::init_thread (void *x, DWORD (*func) (void *, void *))
initialized = CYGTLS_INITIALIZED;
errno_addr = &(local_clib._errno);
locals.cw_timer = NULL;
locals.cw_timer_inuse = false;
locals.pathbufs.clear ();

if ((void *) func == (void *) cygthread::stub
Expand All @@ -85,6 +86,7 @@ _cygtls::fixup_after_fork ()
signal_arrived = NULL;
locals.select.sockevt = NULL;
locals.cw_timer = NULL;
locals.cw_timer_inuse = false;
locals.pathbufs.clear ();
wq.thread_ev = NULL;
}
Expand Down
23 changes: 16 additions & 7 deletions winsup/cygwin/cygwait.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,21 @@ cygwait (HANDLE object, PLARGE_INTEGER timeout, unsigned mask)
}

DWORD timeout_n;
HANDLE *wait_timer = &_my_tls.locals.cw_timer;
HANDLE local_wait_timer = NULL;
if (!timeout)
timeout_n = WAIT_TIMEOUT + 1;
else
{
if (_my_tls.locals.cw_timer_inuse)
wait_timer = &local_wait_timer;
else
_my_tls.locals.cw_timer_inuse = true;
timeout_n = WAIT_OBJECT_0 + num++;
if (!_my_tls.locals.cw_timer)
NtCreateTimer (&_my_tls.locals.cw_timer, TIMER_ALL_ACCESS, NULL,
NotificationTimer);
NtSetTimer (_my_tls.locals.cw_timer, timeout, NULL, NULL, FALSE, 0, NULL);
wait_objects[timeout_n] = _my_tls.locals.cw_timer;
if (!*wait_timer)
NtCreateTimer (wait_timer, TIMER_ALL_ACCESS, NULL, NotificationTimer);
NtSetTimer (*wait_timer, timeout, NULL, NULL, FALSE, 0, NULL);
wait_objects[timeout_n] = *wait_timer;
}

while (1)
Expand Down Expand Up @@ -100,15 +105,19 @@ cygwait (HANDLE object, PLARGE_INTEGER timeout, unsigned mask)
{
TIMER_BASIC_INFORMATION tbi;

NtQueryTimer (_my_tls.locals.cw_timer, TimerBasicInformation, &tbi,
NtQueryTimer (*wait_timer, TimerBasicInformation, &tbi,
sizeof tbi, NULL);
/* if timer expired, TimeRemaining is negative and represents the
system uptime when signalled */
if (timeout->QuadPart < 0LL) {
timeout->QuadPart = tbi.SignalState || tbi.TimeRemaining.QuadPart < 0LL
? 0LL : tbi.TimeRemaining.QuadPart;
}
NtCancelTimer (_my_tls.locals.cw_timer, NULL);
NtCancelTimer (*wait_timer, NULL);
if (local_wait_timer)
NtClose(local_wait_timer);
else
_my_tls.locals.cw_timer_inuse = false;
}

if (res == WAIT_CANCELED && is_cw_cancel_self)
Expand Down
3 changes: 2 additions & 1 deletion winsup/cygwin/local_includes/cygtls.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ struct _local_storage

/* thread.cc */
HANDLE cw_timer;
bool cw_timer_inuse;

tls_pathbuf pathbufs;
char ttybuf[32];
Expand Down Expand Up @@ -180,7 +181,7 @@ class _cygtls
siginfo_t *sigwait_info;
HANDLE signal_arrived;
bool will_wait_for_signal;
#if 0
#if 1
long __align; /* Needed to align context to 16 byte. */
#endif
/* context MUST be aligned to 16 byte, otherwise RtlCaptureContext fails.
Expand Down
21 changes: 15 additions & 6 deletions winsup/cygwin/select.cc
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,18 @@ next_while:;
to create the timer once per thread. Since WFMO checks the handles
in order, we append the timer as last object, otherwise it's preferred
over actual events on the descriptors. */
HANDLE &wait_timer = _my_tls.locals.cw_timer;
HANDLE *wait_timer = &_my_tls.locals.cw_timer;
HANDLE local_wait_timer = NULL;
if (us > 0LL)
{
NTSTATUS status;
if (!wait_timer)
if (_my_tls.locals.cw_timer_inuse)
wait_timer = &local_wait_timer;
else
_my_tls.locals.cw_timer_inuse = true;
if (!*wait_timer)
{
status = NtCreateTimer (&wait_timer, TIMER_ALL_ACCESS, NULL,
status = NtCreateTimer (wait_timer, TIMER_ALL_ACCESS, NULL,
NotificationTimer);
if (!NT_SUCCESS (status))
{
Expand All @@ -400,15 +405,15 @@ next_while:;
}
}
LARGE_INTEGER ms_clock_ticks = { .QuadPart = -us * 10 };
status = NtSetTimer (wait_timer, &ms_clock_ticks, NULL, NULL, FALSE,
status = NtSetTimer (*wait_timer, &ms_clock_ticks, NULL, NULL, FALSE,
0, NULL);
if (!NT_SUCCESS (status))
{
select_printf ("%y = NtSetTimer (%D)\n",
status, ms_clock_ticks.QuadPart);
return select_error;
}
w4[m] = wait_timer;
w4[m] = *wait_timer;
timer_idx = m++;
}

Expand All @@ -430,7 +435,11 @@ next_while:;
if (timer_idx)
{
BOOLEAN current_state;
NtCancelTimer (wait_timer, &current_state);
NtCancelTimer (*wait_timer, &current_state);
if (local_wait_timer)
NtClose (local_wait_timer);
else
_my_tls.locals.cw_timer_inuse = false;
}

wait_states res;
Expand Down

0 comments on commit 96aae8a

Please sign in to comment.