2012-06-17 22:50:24 +02:00
|
|
|
/* cygwait.h
|
|
|
|
|
|
|
|
Copyright 2011, 2012 Red Hat, Inc.
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
|
|
|
#include "sigproc.h"
|
|
|
|
#include "cygwait.h"
|
|
|
|
#include "ntdll.h"
|
|
|
|
|
|
|
|
#define is_cw_cancel (mask & cw_cancel)
|
|
|
|
#define is_cw_cancel_self (mask & cw_cancel_self)
|
|
|
|
#define is_cw_sig (mask & cw_sig)
|
|
|
|
#define is_cw_sig_eintr (mask & cw_sig_eintr)
|
|
|
|
#define is_cw_sig_return (mask & cw_sig_return)
|
|
|
|
|
|
|
|
#define is_cw_sig_handle (mask & (is_cw_sig | is_cw_sig_eintr))
|
|
|
|
|
2012-07-22 00:58:20 +02:00
|
|
|
LARGE_INTEGER cw_nowait_storage;
|
|
|
|
|
2012-06-17 22:50:24 +02:00
|
|
|
DWORD
|
2012-08-15 21:07:42 +02:00
|
|
|
cygwait (HANDLE object, PLARGE_INTEGER timeout, unsigned mask)
|
2012-06-17 22:50:24 +02:00
|
|
|
{
|
|
|
|
DWORD res;
|
|
|
|
DWORD num = 0;
|
|
|
|
HANDLE wait_objects[4];
|
|
|
|
pthread_t thread = pthread::self ();
|
|
|
|
|
|
|
|
/* Do not change the wait order.
|
|
|
|
The object must have higher priority than the cancel event,
|
|
|
|
because WaitForMultipleObjects will return the smallest index
|
|
|
|
if both objects are signaled. */
|
|
|
|
if (object)
|
|
|
|
wait_objects[num++] = object;
|
|
|
|
|
2012-07-23 06:36:48 +02:00
|
|
|
set_signal_arrived thread_waiting (is_cw_sig_handle, wait_objects[num]);
|
2012-07-30 05:44:40 +02:00
|
|
|
debug_only_printf ("object %p, thread waiting %d, signal_arrived %p", object, (int) thread_waiting, _my_tls.signal_arrived);
|
2012-06-17 22:50:24 +02:00
|
|
|
DWORD sig_n;
|
2012-07-22 00:58:20 +02:00
|
|
|
if (!thread_waiting)
|
2012-06-17 22:50:24 +02:00
|
|
|
sig_n = WAIT_TIMEOUT + 1;
|
|
|
|
else
|
2012-07-22 00:58:20 +02:00
|
|
|
sig_n = WAIT_OBJECT_0 + num++;
|
2012-06-17 22:50:24 +02:00
|
|
|
|
|
|
|
DWORD cancel_n;
|
|
|
|
if (!is_cw_cancel || !pthread::is_good_object (&thread) ||
|
|
|
|
thread->cancelstate == PTHREAD_CANCEL_DISABLE)
|
|
|
|
cancel_n = WAIT_TIMEOUT + 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cancel_n = WAIT_OBJECT_0 + num++;
|
|
|
|
wait_objects[cancel_n] = thread->cancel_event;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD timeout_n;
|
|
|
|
if (!timeout)
|
|
|
|
timeout_n = WAIT_TIMEOUT + 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
res = WaitForMultipleObjects (num, wait_objects, FALSE, INFINITE);
|
2012-07-30 05:44:40 +02:00
|
|
|
debug_only_printf ("res %d", res);
|
2012-06-17 22:50:24 +02:00
|
|
|
if (res == cancel_n)
|
2012-06-25 18:28:50 +02:00
|
|
|
res = WAIT_CANCELED;
|
2012-06-17 22:50:24 +02:00
|
|
|
else if (res == timeout_n)
|
|
|
|
res = WAIT_TIMEOUT;
|
|
|
|
else if (res != sig_n)
|
|
|
|
/* all set */;
|
2012-08-15 20:50:44 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
_my_tls.lock ();
|
|
|
|
int sig = _my_tls.sig;
|
|
|
|
_my_tls.unlock ();
|
|
|
|
if (!sig)
|
|
|
|
continue;
|
|
|
|
if (is_cw_sig_eintr)
|
|
|
|
res = WAIT_SIGNALED; /* caller will deal with signals */
|
|
|
|
else if (_my_tls.call_signal_handler ())
|
|
|
|
continue;
|
|
|
|
}
|
2012-06-17 22:50:24 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout)
|
|
|
|
{
|
|
|
|
TIMER_BASIC_INFORMATION tbi;
|
|
|
|
|
|
|
|
NtQueryTimer (_my_tls.locals.cw_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 ? 0LL : tbi.TimeRemaining.QuadPart;
|
|
|
|
NtCancelTimer (_my_tls.locals.cw_timer, NULL);
|
|
|
|
}
|
|
|
|
|
2012-06-25 18:28:50 +02:00
|
|
|
if (res == WAIT_CANCELED && is_cw_cancel_self)
|
|
|
|
pthread::static_cancel_self ();
|
|
|
|
|
2012-06-17 22:50:24 +02:00
|
|
|
return res;
|
|
|
|
}
|