Cygwin: posix timers: reimplement using OS timer
- Rename files timer.* to posix_timer.*. - Reimplement using an OS timer rather than a handcrafted wait loop. - Use a Slim R/W Lock for synchronization. - Drop timer chaining. It doesn't server a purpose since all timers are local only. - Rename ttstart to itimer_tracker to better reflect its purpose. It's not the anchor for a timer chain anymore anyway. - Drop fixup_timers_after_fork. Everything is process-local, nothing gets inherited. - Rename timer_tracker::disarm_event to disarm_overrun_event for better readability. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
This commit is contained in:
parent
4c50dc94c3
commit
229ea3f23c
@ -354,6 +354,7 @@ DLL_OFILES:= \
|
|||||||
pinfo.o \
|
pinfo.o \
|
||||||
poll.o \
|
poll.o \
|
||||||
posix_ipc.o \
|
posix_ipc.o \
|
||||||
|
posix_timer.o \
|
||||||
pseudo-reloc.o \
|
pseudo-reloc.o \
|
||||||
pthread.o \
|
pthread.o \
|
||||||
quotactl.o \
|
quotactl.o \
|
||||||
@ -395,7 +396,6 @@ DLL_OFILES:= \
|
|||||||
syslog.o \
|
syslog.o \
|
||||||
termios.o \
|
termios.o \
|
||||||
thread.o \
|
thread.o \
|
||||||
timer.o \
|
|
||||||
timerfd.o \
|
timerfd.o \
|
||||||
times.o \
|
times.o \
|
||||||
tls_pbuf.o \
|
tls_pbuf.o \
|
||||||
|
@ -34,7 +34,8 @@ enum cygheap_types
|
|||||||
HEAP_2_DLL,
|
HEAP_2_DLL,
|
||||||
HEAP_MMAP,
|
HEAP_MMAP,
|
||||||
HEAP_2_MAX = 200,
|
HEAP_2_MAX = 200,
|
||||||
HEAP_3_FHANDLER
|
HEAP_3_FHANDLER,
|
||||||
|
HEAP_3_TIMER
|
||||||
};
|
};
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
@ -27,7 +27,7 @@ details. */
|
|||||||
#include "child_info.h"
|
#include "child_info.h"
|
||||||
#include "ntdll.h"
|
#include "ntdll.h"
|
||||||
#include "exception.h"
|
#include "exception.h"
|
||||||
#include "timer.h"
|
#include "posix_timer.h"
|
||||||
|
|
||||||
/* Definitions for code simplification */
|
/* Definitions for code simplification */
|
||||||
#ifdef __x86_64__
|
#ifdef __x86_64__
|
||||||
@ -1483,7 +1483,7 @@ sigpacket::process ()
|
|||||||
if (handler == SIG_IGN)
|
if (handler == SIG_IGN)
|
||||||
{
|
{
|
||||||
if (si.si_code == SI_TIMER)
|
if (si.si_code == SI_TIMER)
|
||||||
((timer_tracker *) si.si_tid)->disarm_event ();
|
((timer_tracker *) si.si_tid)->disarm_overrun_event ();
|
||||||
sigproc_printf ("signal %d ignored", si.si_signo);
|
sigproc_printf ("signal %d ignored", si.si_signo);
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
@ -1508,7 +1508,7 @@ sigpacket::process ()
|
|||||||
|| si.si_signo == SIGURG)
|
|| si.si_signo == SIGURG)
|
||||||
{
|
{
|
||||||
if (si.si_code == SI_TIMER)
|
if (si.si_code == SI_TIMER)
|
||||||
((timer_tracker *) si.si_tid)->disarm_event ();
|
((timer_tracker *) si.si_tid)->disarm_overrun_event ();
|
||||||
sigproc_printf ("signal %d default is currently ignore", si.si_signo);
|
sigproc_printf ("signal %d default is currently ignore", si.si_signo);
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
@ -1637,7 +1637,7 @@ _cygtls::call_signal_handler ()
|
|||||||
{
|
{
|
||||||
timer_tracker *tt = (timer_tracker *)
|
timer_tracker *tt = (timer_tracker *)
|
||||||
infodata.si_tid;
|
infodata.si_tid;
|
||||||
infodata.si_overrun = tt->disarm_event ();
|
infodata.si_overrun = tt->disarm_overrun_event ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Save information locally on stack to pass to handler. */
|
/* Save information locally on stack to pass to handler. */
|
||||||
|
@ -22,7 +22,6 @@ details. */
|
|||||||
#include "tls_pbuf.h"
|
#include "tls_pbuf.h"
|
||||||
#include "dll_init.h"
|
#include "dll_init.h"
|
||||||
#include "cygmalloc.h"
|
#include "cygmalloc.h"
|
||||||
#include "timer.h"
|
|
||||||
#include "ntdll.h"
|
#include "ntdll.h"
|
||||||
|
|
||||||
#define NPIDS_HELD 4
|
#define NPIDS_HELD 4
|
||||||
@ -196,7 +195,6 @@ frok::child (volatile char * volatile here)
|
|||||||
ForceCloseHandle1 (fork_info->forker_finished, forker_finished);
|
ForceCloseHandle1 (fork_info->forker_finished, forker_finished);
|
||||||
|
|
||||||
pthread::atforkchild ();
|
pthread::atforkchild ();
|
||||||
fixup_timers_after_fork ();
|
|
||||||
cygbench ("fork-child");
|
cygbench ("fork-child");
|
||||||
ld_preload ();
|
ld_preload ();
|
||||||
fixup_hooks_after_fork ();
|
fixup_hooks_after_fork ();
|
||||||
|
@ -14,65 +14,37 @@ details. */
|
|||||||
#include "fhandler.h"
|
#include "fhandler.h"
|
||||||
#include "dtable.h"
|
#include "dtable.h"
|
||||||
#include "cygheap.h"
|
#include "cygheap.h"
|
||||||
#include "timer.h"
|
#include "posix_timer.h"
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
|
|
||||||
#define EVENT_DISARMED 0
|
#define OVR_EVENT_DISARMED 0
|
||||||
#define EVENT_ARMED -1
|
#define OVR_EVENT_ARMED 1
|
||||||
#define EVENT_LOCK 1
|
|
||||||
|
|
||||||
/* Must not be NO_COPY to avoid memory leak after fork. */
|
timer_tracker NO_COPY itimer_tracker (CLOCK_REALTIME, NULL);
|
||||||
timer_tracker ttstart (CLOCK_REALTIME, NULL);
|
|
||||||
|
|
||||||
class lock_timer_tracker
|
|
||||||
{
|
|
||||||
static muto protect;
|
|
||||||
public:
|
|
||||||
lock_timer_tracker ();
|
|
||||||
~lock_timer_tracker ();
|
|
||||||
};
|
|
||||||
|
|
||||||
muto NO_COPY lock_timer_tracker::protect;
|
|
||||||
|
|
||||||
lock_timer_tracker::lock_timer_tracker ()
|
|
||||||
{
|
|
||||||
protect.init ("timer_protect")->acquire ();
|
|
||||||
}
|
|
||||||
|
|
||||||
lock_timer_tracker::~lock_timer_tracker ()
|
|
||||||
{
|
|
||||||
protect.release ();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
bool
|
||||||
timer_tracker::cancel ()
|
timer_tracker::cancel ()
|
||||||
{
|
{
|
||||||
if (!hcancel)
|
DWORD res;
|
||||||
return false;
|
|
||||||
|
|
||||||
SetEvent (hcancel);
|
if (!cancel_evt)
|
||||||
DWORD res = WaitForSingleObject (syncthread, INFINITE);
|
return false;
|
||||||
if (res != WAIT_OBJECT_0)
|
SetEvent (cancel_evt);
|
||||||
system_printf ("WFSO returned unexpected value %u, %E", res);
|
if (sync_thr)
|
||||||
|
{
|
||||||
|
res = WaitForSingleObject (sync_thr, INFINITE);
|
||||||
|
if (res != WAIT_OBJECT_0)
|
||||||
|
system_printf ("WFSO returned unexpected value %u, %E", res);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
timer_tracker::~timer_tracker ()
|
|
||||||
{
|
|
||||||
if (cancel ())
|
|
||||||
{
|
|
||||||
CloseHandle (hcancel);
|
|
||||||
#ifdef DEBUGGING
|
|
||||||
hcancel = NULL;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
if (syncthread)
|
|
||||||
CloseHandle (syncthread);
|
|
||||||
magic = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
timer_tracker::timer_tracker (clockid_t c, const sigevent *e)
|
timer_tracker::timer_tracker (clockid_t c, const sigevent *e)
|
||||||
|
: magic (TT_MAGIC), clock_id (c), timer (NULL), cancel_evt (NULL),
|
||||||
|
sync_thr (NULL), interval (0), exp_ts (0), overrun_event_running (0),
|
||||||
|
overrun_count_curr (0), overrun_count (0)
|
||||||
{
|
{
|
||||||
|
srwlock = SRWLOCK_INIT;
|
||||||
if (e != NULL)
|
if (e != NULL)
|
||||||
evp = *e;
|
evp = *e;
|
||||||
else
|
else
|
||||||
@ -81,18 +53,17 @@ timer_tracker::timer_tracker (clockid_t c, const sigevent *e)
|
|||||||
evp.sigev_signo = SIGALRM;
|
evp.sigev_signo = SIGALRM;
|
||||||
evp.sigev_value.sival_ptr = this;
|
evp.sigev_value.sival_ptr = this;
|
||||||
}
|
}
|
||||||
clock_id = c;
|
}
|
||||||
magic = TT_MAGIC;
|
|
||||||
hcancel = NULL;
|
timer_tracker::~timer_tracker ()
|
||||||
event_running = EVENT_DISARMED;
|
{
|
||||||
overrun_count_curr = 0;
|
AcquireSRWLockExclusive (&srwlock);
|
||||||
overrun_count = 0;
|
cancel ();
|
||||||
if (this != &ttstart)
|
NtClose (cancel_evt);
|
||||||
{
|
NtClose (sync_thr);
|
||||||
lock_timer_tracker here;
|
NtClose (timer);
|
||||||
next = ttstart.next;
|
magic = 0;
|
||||||
ttstart.next = this;
|
ReleaseSRWLockExclusive (&srwlock);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int64_t
|
static inline int64_t
|
||||||
@ -105,29 +76,26 @@ timespec_to_us (const timespec& ts)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Returns 0 if arming successful, -1 if a signal is already queued.
|
/* Returns 0 if arming successful, -1 if a signal is already queued.
|
||||||
If so, it also increments overrun_count. */
|
If so, it also increments overrun_count. Only call under lock! */
|
||||||
LONG
|
LONG
|
||||||
timer_tracker::arm_event ()
|
timer_tracker::arm_overrun_event ()
|
||||||
{
|
{
|
||||||
LONG ret;
|
LONG ret;
|
||||||
|
|
||||||
while ((ret = InterlockedCompareExchange (&event_running, EVENT_ARMED,
|
ret = InterlockedExchange (&overrun_event_running, OVR_EVENT_ARMED);
|
||||||
EVENT_DISARMED)) == EVENT_LOCK)
|
if (ret == OVR_EVENT_ARMED)
|
||||||
yield ();
|
ret = InterlockedIncrement64 (&overrun_count);
|
||||||
if (ret == EVENT_ARMED)
|
|
||||||
InterlockedIncrement64 (&overrun_count);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
LONG
|
LONG
|
||||||
timer_tracker::disarm_event ()
|
timer_tracker::disarm_overrun_event ()
|
||||||
{
|
{
|
||||||
LONG ret;
|
LONG ret;
|
||||||
|
|
||||||
while ((ret = InterlockedCompareExchange (&event_running, EVENT_LOCK,
|
AcquireSRWLockExclusive (&srwlock);
|
||||||
EVENT_ARMED)) == EVENT_LOCK)
|
ret = InterlockedExchange (&overrun_event_running, OVR_EVENT_DISARMED);
|
||||||
yield ();
|
if (ret == OVR_EVENT_ARMED)
|
||||||
if (ret == EVENT_ARMED)
|
|
||||||
{
|
{
|
||||||
LONG64 ov_cnt;
|
LONG64 ov_cnt;
|
||||||
|
|
||||||
@ -138,8 +106,8 @@ timer_tracker::disarm_event ()
|
|||||||
overrun_count_curr = ov_cnt;
|
overrun_count_curr = ov_cnt;
|
||||||
ret = overrun_count_curr;
|
ret = overrun_count_curr;
|
||||||
InterlockedExchange64 (&overrun_count, 0);
|
InterlockedExchange64 (&overrun_count, 0);
|
||||||
InterlockedExchange (&event_running, EVENT_DISARMED);
|
|
||||||
}
|
}
|
||||||
|
ReleaseSRWLockExclusive (&srwlock);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,57 +119,69 @@ notify_thread_wrapper (void *arg)
|
|||||||
void * (*notify_func) (void *) = (void * (*) (void *))
|
void * (*notify_func) (void *) = (void * (*) (void *))
|
||||||
evt->sigev_notify_function;
|
evt->sigev_notify_function;
|
||||||
|
|
||||||
tt->disarm_event ();
|
tt->disarm_overrun_event ();
|
||||||
return notify_func (evt->sigev_value.sival_ptr);
|
return notify_func (evt->sigev_value.sival_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
DWORD
|
DWORD
|
||||||
timer_tracker::thread_func ()
|
timer_tracker::thread_func ()
|
||||||
{
|
{
|
||||||
int64_t now;
|
HANDLE w4[2] = { timer, cancel_evt };
|
||||||
int64_t cur_sleepto_us = sleepto_us;
|
|
||||||
|
debug_printf ("%p timer armed", this);
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
int64_t sleep_us;
|
switch (WaitForMultipleObjects (2, w4, FALSE, INFINITE))
|
||||||
LONG sleep_ms;
|
|
||||||
/* Account for delays in starting thread
|
|
||||||
and sending the signal */
|
|
||||||
now = get_clock (clock_id)->usecs ();
|
|
||||||
sleep_us = cur_sleepto_us - now;
|
|
||||||
if (sleep_us > 0)
|
|
||||||
{
|
{
|
||||||
sleepto_us = cur_sleepto_us;
|
|
||||||
sleep_ms = (sleep_us + (USPERSEC / MSPERSEC) - 1)
|
|
||||||
/ (USPERSEC / MSPERSEC);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int64_t num_intervals = (now - cur_sleepto_us) / interval_us;
|
|
||||||
InterlockedAdd64 (&overrun_count, num_intervals);
|
|
||||||
cur_sleepto_us += num_intervals * interval_us;
|
|
||||||
sleepto_us = cur_sleepto_us;
|
|
||||||
sleep_ms = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
debug_printf ("%p waiting for %u ms", this, sleep_ms);
|
|
||||||
switch (WaitForSingleObject (hcancel, sleep_ms))
|
|
||||||
{
|
|
||||||
case WAIT_TIMEOUT:
|
|
||||||
debug_printf ("timed out");
|
|
||||||
break;
|
|
||||||
case WAIT_OBJECT_0:
|
case WAIT_OBJECT_0:
|
||||||
debug_printf ("%p cancelled", this);
|
debug_printf ("%p timer expired", this);
|
||||||
|
break;
|
||||||
|
case WAIT_OBJECT_0 + 1:
|
||||||
|
debug_printf ("%p timer disarmed, %E", this);
|
||||||
goto out;
|
goto out;
|
||||||
default:
|
default:
|
||||||
debug_printf ("%p wait failed, %E", this);
|
debug_printf ("%p wait failed, %E", this);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
AcquireSRWLockExclusive (&srwlock);
|
||||||
|
/* Make sure we haven't been abandoned and/or disarmed in the meantime */
|
||||||
|
if (exp_ts == 0 && interval == 0)
|
||||||
|
{
|
||||||
|
ReleaseSRWLockExclusive (&srwlock);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
if (interval)
|
||||||
|
{
|
||||||
|
/* Compute expiration count. */
|
||||||
|
LONG64 now = get_clock_now ();
|
||||||
|
LONG64 ts = get_exp_ts ();
|
||||||
|
LONG64 exp_cnt;
|
||||||
|
|
||||||
|
/* Make concessions for unexact realtime clock */
|
||||||
|
if (ts > now)
|
||||||
|
ts = now - 1;
|
||||||
|
exp_cnt = (now - ts) / interval;
|
||||||
|
InterlockedAdd64 (&overrun_count, exp_cnt);
|
||||||
|
ts += interval * exp_cnt;
|
||||||
|
set_exp_ts (ts);
|
||||||
|
/* NtSetTimer allows periods of up to 24 days only. If the time
|
||||||
|
is longer, we set the timer up as one-shot timer for each
|
||||||
|
interval. Restart timer here with new due time. */
|
||||||
|
if (interval > INT_MAX * (NS100PERSEC / MSPERSEC))
|
||||||
|
{
|
||||||
|
/* TODO: CLOCK_REALTIME_ALARM / CLOCK_BOOTTIME_ALARM
|
||||||
|
See comment in arm_timer */
|
||||||
|
BOOL Resume = FALSE;
|
||||||
|
LARGE_INTEGER DueTime = { QuadPart: -interval };
|
||||||
|
|
||||||
|
NtSetTimer (timer, &DueTime, NULL, NULL, Resume, 0, NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
switch (evp.sigev_notify)
|
switch (evp.sigev_notify)
|
||||||
{
|
{
|
||||||
case SIGEV_SIGNAL:
|
case SIGEV_SIGNAL:
|
||||||
{
|
{
|
||||||
if (arm_event ())
|
if (arm_overrun_event ())
|
||||||
{
|
{
|
||||||
debug_printf ("%p timer signal already queued", this);
|
debug_printf ("%p timer signal already queued", this);
|
||||||
break;
|
break;
|
||||||
@ -217,7 +197,7 @@ timer_tracker::thread_func ()
|
|||||||
}
|
}
|
||||||
case SIGEV_THREAD:
|
case SIGEV_THREAD:
|
||||||
{
|
{
|
||||||
if (arm_event ())
|
if (arm_overrun_event ())
|
||||||
{
|
{
|
||||||
debug_printf ("%p timer thread already queued", this);
|
debug_printf ("%p timer thread already queued", this);
|
||||||
break;
|
break;
|
||||||
@ -243,10 +223,16 @@ timer_tracker::thread_func ()
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!interval_us)
|
/* one-shot timer? */
|
||||||
break;
|
if (!interval)
|
||||||
|
{
|
||||||
cur_sleepto_us = sleepto_us + interval_us;
|
memset (&time_spec, 0, sizeof time_spec);
|
||||||
|
exp_ts = 0;
|
||||||
|
overrun_event_running = OVR_EVENT_DISARMED;
|
||||||
|
ReleaseSRWLockExclusive (&srwlock);
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
ReleaseSRWLockExclusive (&srwlock);
|
||||||
debug_printf ("looping");
|
debug_printf ("looping");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -262,58 +248,166 @@ timer_thread (VOID *x)
|
|||||||
return tt->thread_func ();
|
return tt->thread_func ();
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool
|
int
|
||||||
timespec_bad (const timespec& t)
|
timer_tracker::gettime (itimerspec *curr_value, bool lock)
|
||||||
{
|
{
|
||||||
if (t.tv_nsec < 0 || t.tv_nsec >= NSPERSEC || t.tv_sec < 0)
|
if (lock)
|
||||||
{
|
{
|
||||||
set_errno (EINVAL);
|
AcquireSRWLockExclusive (&srwlock);
|
||||||
return true;
|
if (!is_timer_tracker ())
|
||||||
|
{
|
||||||
|
ReleaseSRWLockExclusive (&srwlock);
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
if (!cancel_evt)
|
||||||
|
memset (curr_value, 0, sizeof (*curr_value));
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LONG64 next_relative_exp = get_exp_ts () - get_clock_now ();
|
||||||
|
curr_value->it_value.tv_sec = next_relative_exp / NS100PERSEC;
|
||||||
|
next_relative_exp -= curr_value->it_value.tv_sec * NS100PERSEC;
|
||||||
|
curr_value->it_value.tv_nsec = next_relative_exp
|
||||||
|
* (NSPERSEC / NS100PERSEC);
|
||||||
|
curr_value->it_interval = time_spec.it_interval;
|
||||||
|
}
|
||||||
|
if (lock)
|
||||||
|
ReleaseSRWLockExclusive (&srwlock);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
timer_tracker::settime (int in_flags, const itimerspec *value, itimerspec *ovalue)
|
timer_tracker::settime (int flags, const itimerspec *new_value,
|
||||||
|
itimerspec *old_value)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
|
|
||||||
__try
|
__try
|
||||||
{
|
{
|
||||||
if (!value)
|
if (!new_value || !valid_timespec (new_value->it_value)
|
||||||
|
|| !valid_timespec (new_value->it_interval))
|
||||||
{
|
{
|
||||||
set_errno (EINVAL);
|
ret = -EINVAL;
|
||||||
__leave;
|
__leave;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (timespec_bad (value->it_value) || timespec_bad (value->it_interval))
|
AcquireSRWLockExclusive (&srwlock);
|
||||||
__leave;
|
if (!is_timer_tracker ())
|
||||||
|
{
|
||||||
|
ReleaseSRWLockExclusive (&srwlock);
|
||||||
|
ret = -EINVAL;
|
||||||
|
__leave;
|
||||||
|
}
|
||||||
|
|
||||||
lock_timer_tracker here;
|
if (old_value)
|
||||||
cancel ();
|
gettime (old_value, false);
|
||||||
|
|
||||||
if (ovalue)
|
if (!new_value->it_value.tv_sec && !new_value->it_value.tv_nsec)
|
||||||
gettime (ovalue);
|
{
|
||||||
|
cancel ();
|
||||||
if (!value->it_value.tv_sec && !value->it_value.tv_nsec)
|
memset (&time_spec, 0, sizeof time_spec);
|
||||||
interval_us = sleepto_us = 0;
|
interval = 0;
|
||||||
|
exp_ts = 0;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
interval_us = timespec_to_us (value->it_interval);
|
LONG64 ts;
|
||||||
sleepto_us = timespec_to_us (value->it_value);
|
LARGE_INTEGER DueTime;
|
||||||
if (!(in_flags & TIMER_ABSTIME))
|
BOOLEAN Resume;
|
||||||
sleepto_us += get_clock (clock_id)->usecs ();
|
LONG Period;
|
||||||
it_interval = value->it_interval;
|
NTSTATUS status;
|
||||||
if (!hcancel)
|
|
||||||
hcancel = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
|
if (!timer)
|
||||||
|
{
|
||||||
|
OBJECT_ATTRIBUTES attr;
|
||||||
|
|
||||||
|
InitializeObjectAttributes (&attr, NULL, 0, NULL,
|
||||||
|
sec_none_nih.lpSecurityDescriptor);
|
||||||
|
status = NtCreateEvent (&cancel_evt, EVENT_ALL_ACCESS, &attr,
|
||||||
|
NotificationEvent, FALSE);
|
||||||
|
if (!NT_SUCCESS (status))
|
||||||
|
{
|
||||||
|
ret = -geterrno_from_nt_status (status);
|
||||||
|
__leave;
|
||||||
|
}
|
||||||
|
status = NtCreateEvent (&sync_thr, EVENT_ALL_ACCESS, &attr,
|
||||||
|
NotificationEvent, FALSE);
|
||||||
|
if (!NT_SUCCESS (status))
|
||||||
|
{
|
||||||
|
NtClose (cancel_evt);
|
||||||
|
cancel_evt = NULL;
|
||||||
|
ret = -geterrno_from_nt_status (status);
|
||||||
|
__leave;
|
||||||
|
}
|
||||||
|
status = NtCreateTimer (&timer, TIMER_ALL_ACCESS, &attr,
|
||||||
|
SynchronizationTimer);
|
||||||
|
if (!NT_SUCCESS (status))
|
||||||
|
{
|
||||||
|
NtClose (cancel_evt);
|
||||||
|
NtClose (sync_thr);
|
||||||
|
cancel_evt = sync_thr = NULL;
|
||||||
|
ret = -geterrno_from_nt_status (status);
|
||||||
|
__leave;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ResetEvent (cancel_evt);
|
||||||
|
ResetEvent (sync_thr);
|
||||||
|
NtCancelTimer (timer, NULL);
|
||||||
|
/* Convert incoming itimerspec into 100ns interval and timestamp */
|
||||||
|
interval = new_value->it_interval.tv_sec * NS100PERSEC
|
||||||
|
+ (new_value->it_interval.tv_nsec
|
||||||
|
+ (NSPERSEC / NS100PERSEC) - 1)
|
||||||
|
/ (NSPERSEC / NS100PERSEC);
|
||||||
|
ts = new_value->it_value.tv_sec * NS100PERSEC
|
||||||
|
+ (new_value->it_value.tv_nsec + (NSPERSEC / NS100PERSEC) - 1)
|
||||||
|
/ (NSPERSEC / NS100PERSEC);
|
||||||
|
if (flags & TIMER_ABSTIME)
|
||||||
|
{
|
||||||
|
if (clock_id == CLOCK_REALTIME)
|
||||||
|
DueTime.QuadPart = ts + FACTOR;
|
||||||
|
else /* non-REALTIME clocks require relative DueTime. */
|
||||||
|
{
|
||||||
|
DueTime.QuadPart = get_clock_now () - ts;
|
||||||
|
/* If the timestamp was earlier than now, compute number
|
||||||
|
of expirations and offset DueTime to expire immediately. */
|
||||||
|
if (DueTime.QuadPart >= 0)
|
||||||
|
DueTime.QuadPart = -1LL;
|
||||||
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
ResetEvent (hcancel);
|
{
|
||||||
if (!syncthread)
|
/* Keep relative timestamps relative for the timer, but store the
|
||||||
syncthread = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
|
expiry timestamp absolute for the timer thread. */
|
||||||
|
DueTime.QuadPart = -ts;
|
||||||
|
ts += get_clock_now ();
|
||||||
|
}
|
||||||
|
set_exp_ts (ts);
|
||||||
|
time_spec = *new_value;
|
||||||
|
overrun_count_curr = 0;
|
||||||
|
overrun_count = 0;
|
||||||
|
overrun_event_running = OVR_EVENT_DISARMED;
|
||||||
|
/* TODO: CLOCK_REALTIME_ALARM / CLOCK_BOOTTIME_ALARM
|
||||||
|
Note: Advanced Power Settings -> Sleep -> Allow Wake Timers
|
||||||
|
since W10 1709 */
|
||||||
|
Resume = FALSE;
|
||||||
|
if (interval > INT_MAX * (NS100PERSEC / MSPERSEC))
|
||||||
|
Period = 0;
|
||||||
else
|
else
|
||||||
ResetEvent (syncthread);
|
Period = (interval + (NS100PERSEC / MSPERSEC) - 1)
|
||||||
new cygthread (timer_thread, this, "itimer", syncthread);
|
/ (NS100PERSEC / MSPERSEC);
|
||||||
|
status = NtSetTimer (timer, &DueTime, NULL, NULL, Resume, Period,
|
||||||
|
NULL);
|
||||||
|
if (!NT_SUCCESS (status))
|
||||||
|
{
|
||||||
|
memset (&time_spec, 0, sizeof time_spec);
|
||||||
|
interval = 0;
|
||||||
|
exp_ts = 0;
|
||||||
|
ret = -geterrno_from_nt_status (status);
|
||||||
|
__leave;
|
||||||
|
}
|
||||||
|
new cygthread (timer_thread, this, "itimer", sync_thr);
|
||||||
}
|
}
|
||||||
|
ReleaseSRWLockExclusive (&srwlock);
|
||||||
ret = 0;
|
ret = 0;
|
||||||
}
|
}
|
||||||
__except (EFAULT) {}
|
__except (EFAULT) {}
|
||||||
@ -321,78 +415,12 @@ timer_tracker::settime (int in_flags, const itimerspec *value, itimerspec *ovalu
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
/* The timers are stored on the cygheap. */
|
||||||
timer_tracker::gettime (itimerspec *ovalue)
|
#define cnew(name, ...) \
|
||||||
{
|
({ \
|
||||||
if (!hcancel)
|
void* ptr = (void*) ccalloc (HEAP_3_TIMER, 1, sizeof (name)); \
|
||||||
memset (ovalue, 0, sizeof (*ovalue));
|
ptr ? new (ptr) name (__VA_ARGS__) : NULL; \
|
||||||
else
|
})
|
||||||
{
|
|
||||||
ovalue->it_interval = it_interval;
|
|
||||||
int64_t now = get_clock (clock_id)->usecs ();
|
|
||||||
int64_t left_us = sleepto_us - now;
|
|
||||||
if (left_us < 0)
|
|
||||||
left_us = 0;
|
|
||||||
ovalue->it_value.tv_sec = left_us / USPERSEC;
|
|
||||||
ovalue->it_value.tv_nsec = (left_us % USPERSEC) * (NSPERSEC/USPERSEC);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
timer_tracker::clean_and_unhook ()
|
|
||||||
{
|
|
||||||
for (timer_tracker *tt = &ttstart; tt->next != NULL; tt = tt->next)
|
|
||||||
if (tt->next == this)
|
|
||||||
{
|
|
||||||
tt->next = this->next;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
timer_tracker::fixup_after_fork ()
|
|
||||||
{
|
|
||||||
ttstart.hcancel = ttstart.syncthread = NULL;
|
|
||||||
ttstart.event_running = EVENT_DISARMED;
|
|
||||||
ttstart.overrun_count_curr = 0;
|
|
||||||
ttstart.overrun_count = 0;
|
|
||||||
for (timer_tracker *tt = &ttstart; tt->next != NULL; /* nothing */)
|
|
||||||
{
|
|
||||||
timer_tracker *deleteme = tt->next;
|
|
||||||
tt->next = deleteme->next;
|
|
||||||
deleteme->hcancel = deleteme->syncthread = NULL;
|
|
||||||
delete deleteme;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
fixup_timers_after_fork ()
|
|
||||||
{
|
|
||||||
timer_tracker::fixup_after_fork ();
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" int
|
|
||||||
timer_gettime (timer_t timerid, struct itimerspec *ovalue)
|
|
||||||
{
|
|
||||||
int ret = -1;
|
|
||||||
|
|
||||||
__try
|
|
||||||
{
|
|
||||||
timer_tracker *tt = (timer_tracker *) timerid;
|
|
||||||
if (!tt->is_timer_tracker ())
|
|
||||||
{
|
|
||||||
set_errno (EINVAL);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
tt->gettime (ovalue);
|
|
||||||
ret = 0;
|
|
||||||
}
|
|
||||||
__except (EFAULT) {}
|
|
||||||
__endtry
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" int
|
extern "C" int
|
||||||
timer_create (clockid_t clock_id, struct sigevent *__restrict evp,
|
timer_create (clockid_t clock_id, struct sigevent *__restrict evp,
|
||||||
@ -414,7 +442,7 @@ timer_create (clockid_t clock_id, struct sigevent *__restrict evp,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
*timerid = (timer_t) new timer_tracker (clock_id, evp);
|
*timerid = (timer_t) cnew (timer_tracker, clock_id, evp);
|
||||||
ret = 0;
|
ret = 0;
|
||||||
}
|
}
|
||||||
__except (EFAULT) {}
|
__except (EFAULT) {}
|
||||||
@ -422,6 +450,29 @@ timer_create (clockid_t clock_id, struct sigevent *__restrict evp,
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern "C" int
|
||||||
|
timer_gettime (timer_t timerid, struct itimerspec *ovalue)
|
||||||
|
{
|
||||||
|
int ret = -1;
|
||||||
|
|
||||||
|
__try
|
||||||
|
{
|
||||||
|
timer_tracker *tt = (timer_tracker *) timerid;
|
||||||
|
if (!tt->is_timer_tracker ())
|
||||||
|
{
|
||||||
|
set_errno (EINVAL);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = tt->gettime (ovalue, true);
|
||||||
|
if (ret < 0)
|
||||||
|
set_errno (-ret);
|
||||||
|
}
|
||||||
|
__except (EFAULT) {}
|
||||||
|
__endtry
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
extern "C" int
|
extern "C" int
|
||||||
timer_settime (timer_t timerid, int flags,
|
timer_settime (timer_t timerid, int flags,
|
||||||
const struct itimerspec *__restrict value,
|
const struct itimerspec *__restrict value,
|
||||||
@ -438,6 +489,8 @@ timer_settime (timer_t timerid, int flags,
|
|||||||
__leave;
|
__leave;
|
||||||
}
|
}
|
||||||
ret = tt->settime (flags, value, ovalue);
|
ret = tt->settime (flags, value, ovalue);
|
||||||
|
if (ret < 0)
|
||||||
|
set_errno (-ret);
|
||||||
}
|
}
|
||||||
__except (EFAULT) {}
|
__except (EFAULT) {}
|
||||||
__endtry
|
__endtry
|
||||||
@ -476,20 +529,12 @@ timer_delete (timer_t timerid)
|
|||||||
__try
|
__try
|
||||||
{
|
{
|
||||||
timer_tracker *in_tt = (timer_tracker *) timerid;
|
timer_tracker *in_tt = (timer_tracker *) timerid;
|
||||||
if (!in_tt->is_timer_tracker ())
|
if (!in_tt->is_timer_tracker () || in_tt == &itimer_tracker)
|
||||||
{
|
{
|
||||||
set_errno (EINVAL);
|
set_errno (EINVAL);
|
||||||
__leave;
|
__leave;
|
||||||
}
|
}
|
||||||
|
delete in_tt;
|
||||||
lock_timer_tracker here;
|
|
||||||
if (in_tt->clean_and_unhook () == 0)
|
|
||||||
{
|
|
||||||
delete in_tt;
|
|
||||||
ret = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
set_errno (EINVAL);
|
|
||||||
}
|
}
|
||||||
__except (EFAULT) {}
|
__except (EFAULT) {}
|
||||||
__endtry
|
__endtry
|
||||||
@ -513,7 +558,8 @@ setitimer (int which, const struct itimerval *__restrict value,
|
|||||||
spec_value.it_interval.tv_nsec = value->it_interval.tv_usec * 1000;
|
spec_value.it_interval.tv_nsec = value->it_interval.tv_usec * 1000;
|
||||||
spec_value.it_value.tv_sec = value->it_value.tv_sec;
|
spec_value.it_value.tv_sec = value->it_value.tv_sec;
|
||||||
spec_value.it_value.tv_nsec = value->it_value.tv_usec * 1000;
|
spec_value.it_value.tv_nsec = value->it_value.tv_usec * 1000;
|
||||||
ret = timer_settime ((timer_t) &ttstart, 0, &spec_value, &spec_ovalue);
|
ret = timer_settime ((timer_t) &itimer_tracker, 0,
|
||||||
|
&spec_value, &spec_ovalue);
|
||||||
if (ret)
|
if (ret)
|
||||||
ret = -1;
|
ret = -1;
|
||||||
else if (ovalue)
|
else if (ovalue)
|
||||||
@ -541,7 +587,7 @@ getitimer (int which, struct itimerval *ovalue)
|
|||||||
__try
|
__try
|
||||||
{
|
{
|
||||||
struct itimerspec spec_ovalue;
|
struct itimerspec spec_ovalue;
|
||||||
ret = timer_gettime ((timer_t) &ttstart, &spec_ovalue);
|
ret = timer_gettime ((timer_t) &itimer_tracker, &spec_ovalue);
|
||||||
if (!ret)
|
if (!ret)
|
||||||
{
|
{
|
||||||
ovalue->it_interval.tv_sec = spec_ovalue.it_interval.tv_sec;
|
ovalue->it_interval.tv_sec = spec_ovalue.it_interval.tv_sec;
|
||||||
@ -567,7 +613,7 @@ alarm (unsigned int seconds)
|
|||||||
if (seconds > (CLOCK_DELAY_MAX / 1000 - 1))
|
if (seconds > (CLOCK_DELAY_MAX / 1000 - 1))
|
||||||
seconds = (CLOCK_DELAY_MAX / 1000 - 1);
|
seconds = (CLOCK_DELAY_MAX / 1000 - 1);
|
||||||
newt.it_value.tv_sec = seconds;
|
newt.it_value.tv_sec = seconds;
|
||||||
timer_settime ((timer_t) &ttstart, 0, &newt, &oldt);
|
timer_settime ((timer_t) &itimer_tracker, 0, &newt, &oldt);
|
||||||
int ret = oldt.it_value.tv_sec + (oldt.it_value.tv_nsec > 0);
|
int ret = oldt.it_value.tv_sec + (oldt.it_value.tv_nsec > 0);
|
||||||
syscall_printf ("%d = alarm(%u)", ret, seconds);
|
syscall_printf ("%d = alarm(%u)", ret, seconds);
|
||||||
return ret;
|
return ret;
|
||||||
@ -589,7 +635,7 @@ ualarm (useconds_t value, useconds_t interval)
|
|||||||
timer.it_interval.tv_sec = interval / USPERSEC;
|
timer.it_interval.tv_sec = interval / USPERSEC;
|
||||||
timer.it_interval.tv_nsec = (interval % USPERSEC) * (NSPERSEC/USPERSEC);
|
timer.it_interval.tv_nsec = (interval % USPERSEC) * (NSPERSEC/USPERSEC);
|
||||||
}
|
}
|
||||||
timer_settime ((timer_t) &ttstart, 0, &timer, &otimer);
|
timer_settime ((timer_t) &itimer_tracker, 0, &timer, &otimer);
|
||||||
useconds_t ret = otimer.it_value.tv_sec * USPERSEC
|
useconds_t ret = otimer.it_value.tv_sec * USPERSEC
|
||||||
+ (otimer.it_value.tv_nsec + (NSPERSEC/USPERSEC) - 1)
|
+ (otimer.it_value.tv_nsec + (NSPERSEC/USPERSEC) - 1)
|
||||||
/ (NSPERSEC/USPERSEC);
|
/ (NSPERSEC/USPERSEC);
|
@ -13,38 +13,43 @@ details. */
|
|||||||
class timer_tracker
|
class timer_tracker
|
||||||
{
|
{
|
||||||
unsigned magic;
|
unsigned magic;
|
||||||
timer_tracker *next;
|
SRWLOCK srwlock;
|
||||||
|
|
||||||
clockid_t clock_id;
|
clockid_t clock_id;
|
||||||
sigevent evp;
|
sigevent evp;
|
||||||
timespec it_interval;
|
struct itimerspec time_spec;
|
||||||
HANDLE hcancel;
|
HANDLE timer;
|
||||||
HANDLE syncthread;
|
HANDLE cancel_evt;
|
||||||
int64_t interval_us;
|
HANDLE sync_thr;
|
||||||
int64_t sleepto_us;
|
LONG64 interval;
|
||||||
LONG event_running;
|
LONG64 exp_ts;
|
||||||
|
LONG overrun_event_running;
|
||||||
LONG overrun_count_curr;
|
LONG overrun_count_curr;
|
||||||
LONG64 overrun_count;
|
LONG64 overrun_count;
|
||||||
|
|
||||||
bool cancel ();
|
bool cancel ();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
void *operator new (size_t, void *p) __attribute__ ((nothrow)) {return p;}
|
||||||
|
void operator delete (void *p) { cfree (p); }
|
||||||
timer_tracker (clockid_t, const sigevent *);
|
timer_tracker (clockid_t, const sigevent *);
|
||||||
~timer_tracker ();
|
~timer_tracker ();
|
||||||
inline bool is_timer_tracker () const { return magic == TT_MAGIC; }
|
inline bool is_timer_tracker () const { return magic == TT_MAGIC; }
|
||||||
inline sigevent_t *sigevt () { return &evp; }
|
inline sigevent_t *sigevt () { return &evp; }
|
||||||
inline int getoverrun () const { return overrun_count_curr; }
|
inline int getoverrun () const { return overrun_count_curr; }
|
||||||
|
|
||||||
void gettime (itimerspec *);
|
LONG64 get_clock_now () const { return get_clock (clock_id)->n100secs (); }
|
||||||
|
LONG64 get_exp_ts () const { return exp_ts; }
|
||||||
|
LONG64 get_interval () const { return interval; }
|
||||||
|
void set_exp_ts (LONG64 ts) { exp_ts = ts; }
|
||||||
|
|
||||||
|
LONG arm_overrun_event ();
|
||||||
|
LONG disarm_overrun_event ();
|
||||||
|
|
||||||
|
int gettime (itimerspec *, bool);
|
||||||
int settime (int, const itimerspec *, itimerspec *);
|
int settime (int, const itimerspec *, itimerspec *);
|
||||||
int clean_and_unhook ();
|
|
||||||
LONG arm_event ();
|
|
||||||
LONG disarm_event ();
|
|
||||||
|
|
||||||
DWORD thread_func ();
|
DWORD thread_func ();
|
||||||
static void fixup_after_fork ();
|
static void fixup_after_fork ();
|
||||||
};
|
};
|
||||||
|
|
||||||
extern void fixup_timers_after_fork ();
|
|
||||||
|
|
||||||
#endif /* __TIMER_H__ */
|
#endif /* __TIMER_H__ */
|
@ -21,7 +21,7 @@ details. */
|
|||||||
#include "dtable.h"
|
#include "dtable.h"
|
||||||
#include "cygheap.h"
|
#include "cygheap.h"
|
||||||
#include "cygwait.h"
|
#include "cygwait.h"
|
||||||
#include "timer.h"
|
#include "posix_timer.h"
|
||||||
|
|
||||||
#define _SA_NORESTART 0x8000
|
#define _SA_NORESTART 0x8000
|
||||||
|
|
||||||
@ -619,7 +619,7 @@ sigwait_common (const sigset_t *set, siginfo_t *info, PLARGE_INTEGER waittime)
|
|||||||
{
|
{
|
||||||
timer_tracker *tt = (timer_tracker *)
|
timer_tracker *tt = (timer_tracker *)
|
||||||
_my_tls.infodata.si_tid;
|
_my_tls.infodata.si_tid;
|
||||||
_my_tls.infodata.si_overrun = tt->disarm_event ();
|
_my_tls.infodata.si_overrun = tt->disarm_overrun_event ();
|
||||||
}
|
}
|
||||||
if (info)
|
if (info)
|
||||||
*info = _my_tls.infodata;
|
*info = _my_tls.infodata;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user