diff options
author | Christopher Faylor <me@cgf.cx> | 2004-01-19 05:38:24 +0000 |
---|---|---|
committer | Christopher Faylor <me@cgf.cx> | 2004-01-19 05:38:24 +0000 |
commit | 80ecc90fa20aabe654054de81e038bf81107480f (patch) | |
tree | 68b96d23891249974b5f19feb9fcc799de0cddc8 | |
parent | d6572226d967a23e01a50ea63856d13ee9b9464e (diff) | |
download | newlib-80ecc90fa20aabe654054de81e038bf81107480f.zip newlib-80ecc90fa20aabe654054de81e038bf81107480f.tar.gz newlib-80ecc90fa20aabe654054de81e038bf81107480f.tar.bz2 |
* cygwin/include/signal.h: Add copyright notice.
* cygwin.din: Make clock SIGFE. Add clock_gettime, sigwaitinfo, timer_create,
timer_delete, timer_settime.
* include/cygwin/version.h: Reflect above additions.
* fork.cc (fork_child): Call fixup_timers_after_fork.
* signal.cc (sigwait): Remove unused variable.
* timer.cc: New file.
(clock_gettime): Define new function.
(timer_tracker): Define new struct used by timer functions.
(timer_tracker::timer_tracker): New function.
(to_us): New function.
(timer_thread): New function.
(timer_tracker::settime): New function.
(timer_create): New function.
(timer_settime): New function.
(timer_delete): New function.
(fixup_timers_after_fork): New function.
* cygthread.cc: Bump thread count.
-rw-r--r-- | winsup/cygwin/ChangeLog | 22 | ||||
-rw-r--r-- | winsup/cygwin/Makefile.in | 2 | ||||
-rw-r--r-- | winsup/cygwin/cygthread.cc | 2 | ||||
-rw-r--r-- | winsup/cygwin/cygthread.h | 2 | ||||
-rw-r--r-- | winsup/cygwin/cygwin.din | 9 | ||||
-rw-r--r-- | winsup/cygwin/exceptions.cc | 57 | ||||
-rw-r--r-- | winsup/cygwin/fork.cc | 2 | ||||
-rw-r--r-- | winsup/cygwin/include/cygwin/signal.h | 33 | ||||
-rw-r--r-- | winsup/cygwin/include/cygwin/version.h | 4 | ||||
-rw-r--r-- | winsup/cygwin/signal.cc | 12 | ||||
-rw-r--r-- | winsup/cygwin/timer.cc | 275 | ||||
-rw-r--r-- | winsup/cygwin/times.cc | 27 |
12 files changed, 400 insertions, 47 deletions
diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index 9c8ad9d..b810018 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,25 @@ +2004-01-19 Christopher Faylor <cgf@redhat.com> + + * cygwin/include/signal.h: Add copyright notice. + + * cygwin.din: Make clock SIGFE. Add clock_gettime, sigwaitinfo, + timer_create, timer_delete, timer_settime. + * include/cygwin/version.h: Reflect above additions. + * fork.cc (fork_child): Call fixup_timers_after_fork. + * signal.cc (sigwait): Remove unused variable. + * timer.cc: New file. + (clock_gettime): Define new function. + (timer_tracker): Define new struct used by timer functions. + (timer_tracker::timer_tracker): New function. + (to_us): New function. + (timer_thread): New function. + (timer_tracker::settime): New function. + (timer_create): New function. + (timer_settime): New function. + (timer_delete): New function. + (fixup_timers_after_fork): New function. + * cygthread.cc: Bump thread count. + 2004-01-17 Christopher Faylor <cgf@redhat.com> * signal.cc (sigwaitinfo): Define new function. diff --git a/winsup/cygwin/Makefile.in b/winsup/cygwin/Makefile.in index cf15b32..5cd0938 100644 --- a/winsup/cygwin/Makefile.in +++ b/winsup/cygwin/Makefile.in @@ -134,7 +134,7 @@ DLL_OFILES:=assert.o autoload.o bsdlib.o cxx.o cygheap.o cygthread.o cygtls.o \ resource.o scandir.o sched.o sec_acl.o sec_helper.o security.o \ select.o sem.o shared.o shm.o sigfe.o signal.o sigproc.o smallprint.o \ spawn.o strace.o strsep.o sync.o syscalls.o sysconf.o syslog.o \ - termios.o thread.o times.o tty.o uinfo.o uname.o v8_regexp.o \ + termios.o thread.o timer.o times.o tty.o uinfo.o uname.o v8_regexp.o \ v8_regerror.o v8_regsub.o wait.o wincap.o window.o \ $(EXTRA_DLL_OFILES) $(EXTRA_OFILES) $(MALLOC_OFILES) $(MT_SAFE_OBJECTS) diff --git a/winsup/cygwin/cygthread.cc b/winsup/cygwin/cygthread.cc index e830196..309bd51 100644 --- a/winsup/cygwin/cygthread.cc +++ b/winsup/cygwin/cygthread.cc @@ -20,7 +20,7 @@ details. */ #undef CloseHandle -static cygthread NO_COPY threads[18]; +static cygthread NO_COPY threads[32]; #define NTHREADS (sizeof (threads) / sizeof (threads[0])) DWORD NO_COPY cygthread::main_thread_id; diff --git a/winsup/cygwin/cygthread.h b/winsup/cygwin/cygthread.h index 76604b4..9d10bb2 100644 --- a/winsup/cygwin/cygthread.h +++ b/winsup/cygwin/cygthread.h @@ -1,6 +1,6 @@ /* cygthread.h - Copyright 1998, 1999, 2000, 2001, 2002 Red Hat, Inc. + Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004 Red Hat, Inc. This software is a copyrighted work licensed under the terms of the Cygwin license. Please consult the file "CYGWIN_LICENSE" for diff --git a/winsup/cygwin/cygwin.din b/winsup/cygwin/cygwin.din index 1f3fe08..7d4c352 100644 --- a/winsup/cygwin/cygwin.din +++ b/winsup/cygwin/cygwin.din @@ -246,8 +246,9 @@ _chroot = chroot SIGFE cleanup_glue NOSIGFE clearerr NOSIGFE _clearerr = clearerr NOSIGFE -clock NOSIGFE -_clock = clock NOSIGFE +clock SIGFE +_clock = clock SIGFE +clock_gettime SIGFE close SIGFE _close = close SIGFE closedir SIGFE @@ -1234,6 +1235,7 @@ sigpending SIGFE sigprocmask SIGFE sigsuspend SIGFE sigwait SIGFE +sigwaitinfo SIGFE sin NOSIGFE _sin = sin NOSIGFE sincos NOSIGFE @@ -1407,6 +1409,9 @@ tgamma NOSIGFE tgammaf NOSIGFE time SIGFE _time = time SIGFE +timer_create SIGFE +timer_delete SIGFE +timer_settime SIGFE times SIGFE _times = times SIGFE timezone SIGFE diff --git a/winsup/cygwin/exceptions.cc b/winsup/cygwin/exceptions.cc index cdb20f2..1e9745b 100644 --- a/winsup/cygwin/exceptions.cc +++ b/winsup/cygwin/exceptions.cc @@ -935,40 +935,52 @@ sigpacket::process () } int rc = 1; - bool insigwait_mask = tls ? sigismember (&tls->sigwait_mask, si.si_signo) : false; - bool special_case = ISSTATE (myself, PID_STOPPED) || main_vfork->pid; - bool masked = sigismember (mask, si.si_signo); - if (si.si_signo != SIGKILL && si.si_signo != SIGSTOP - && (special_case || main_vfork->pid || masked || insigwait_mask - || (tls && sigismember (&tls->sigmask, si.si_signo)))) - { - sigproc_printf ("signal %d blocked", si.si_signo); - if ((!special_case && !masked) - && (insigwait_mask || (tls = _threadinfo::find_tls (si.si_signo)) != NULL)) - goto thread_specific; - rc = -1; - goto done; - } - - /* Clear pending SIGCONT on stop signals */ - if (si.si_signo == SIGSTOP || si.si_signo == SIGTSTP || si.si_signo == SIGTTIN || si.si_signo == SIGTTOU) - sig_clear (SIGCONT); sigproc_printf ("signal %d processing", si.si_signo); struct sigaction thissig = global_sigs[si.si_signo]; - void *handler; - handler = (void *) thissig.sa_handler; myself->rusage_self.ru_nsignals++; if (si.si_signo == SIGKILL) goto exit_sig; + if ( si.si_signo == SIGSTOP) + { + sig_clear (SIGCONT); + goto stop; + } + + bool masked; + bool special_case; + bool insigwait_mask; + insigwait_mask = masked = false; + if (special_case = (main_vfork->pid || ISSTATE (myself, PID_STOPPED))) + /* nothing to do */; + else if (tls && sigismember (&tls->sigwait_mask, si.si_signo)) + insigwait_mask = true; + else if (!tls && (tls = _threadinfo::find_tls (si.si_signo))) + insigwait_mask = true; + else if (!(masked = sigismember (mask, si.si_signo)) && tls) + masked = sigismember (&tls->sigmask, si.si_signo); + + if (insigwait_mask) + goto thread_specific; if (!tls) tls = _main_tls; - if (si.si_signo == SIGSTOP) - goto stop; + if (special_case || masked) + { + sigproc_printf ("signal %d blocked", si.si_signo); + rc = -1; + goto done; + } + + void *handler; + handler = (void *) thissig.sa_handler; + + /* Clear pending SIGCONT on stop signals */ + if (si.si_signo == SIGTSTP || si.si_signo == SIGTTIN || si.si_signo == SIGTTOU) + sig_clear (SIGCONT); #if 0 char sigmsg[24]; @@ -1025,6 +1037,7 @@ done: thread_specific: tls->sig = si.si_signo; + tls->set_siginfo (this); sigproc_printf ("releasing sigwait for thread"); SetEvent (tls->event); goto done; diff --git a/winsup/cygwin/fork.cc b/winsup/cygwin/fork.cc index 407a47d..2471ad2 100644 --- a/winsup/cygwin/fork.cc +++ b/winsup/cygwin/fork.cc @@ -231,6 +231,7 @@ sync_with_parent (const char *s, bool hang_self) static int __stdcall fork_child (HANDLE& hParent, dll *&first_dll, bool& load_dlls) { + extern void fixup_timers_after_fork (); debug_printf ("child is running. pid %d, ppid %d, stack here %p", myself->pid, myself->ppid, __builtin_frame_address (0)); @@ -316,6 +317,7 @@ fork_child (HANDLE& hParent, dll *&first_dll, bool& load_dlls) (*t)->set (); pthread::atforkchild (); + fixup_timers_after_fork (); wait_for_sigthread (); cygbench ("fork-child"); return 0; diff --git a/winsup/cygwin/include/cygwin/signal.h b/winsup/cygwin/include/cygwin/signal.h index 20ab1c3..87af7c8 100644 --- a/winsup/cygwin/include/cygwin/signal.h +++ b/winsup/cygwin/include/cygwin/signal.h @@ -1,3 +1,13 @@ +/* signal.h + + Copyright 2004 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. */ + #ifndef _CYGWIN_SIGNAL_H #define _CYGWIN_SIGNAL_H @@ -22,6 +32,15 @@ typedef union sigval void *sival_ptr; /* pointer signal value */ } sigval_t; +typedef struct sigevent +{ + sigval_t sigev_value; /* signal value */ + int sigev_signo; /* signal number */ + int sigev_notify; /* notification type */ + void (*sigev_notify_function) (sigval_t); /* notification function */ + pthread_attr_t *sigev_notify_attributes; /* notification attributes */ +} sigevent_t; + #pragma pack(push,4) typedef struct { @@ -43,11 +62,12 @@ typedef struct { struct { - unsigned int si_tid; /* timer id */ + timer_t si_tid; /* timer id */ unsigned int si_overrun; /* overrun count */ }; + sigval_t si_sigval; /* signal value */ + sigval_t si_value; /* signal value */ }; - sigval_t si_sigval; /* signal value */ }; }; @@ -110,15 +130,6 @@ enum CLD_CONTINUED /* stopped child has continued */ }; -typedef struct sigevent -{ - sigval_t sigev_value; /* signal value */ - int sigev_signo; /* signal number */ - int sigev_notify; /* notification type */ - void (*sigev_notify_function) (sigval_t); /* notification function */ - pthread_attr_t *sigev_notify_attributes; /* notification attributes */ -} sigevent_t; - enum { SIGEV_SIGNAL = 0, /* a queued signal, with an application diff --git a/winsup/cygwin/include/cygwin/version.h b/winsup/cygwin/include/cygwin/version.h index 1a91721..e8b5577 100644 --- a/winsup/cygwin/include/cygwin/version.h +++ b/winsup/cygwin/include/cygwin/version.h @@ -233,12 +233,14 @@ details. */ 106: Export flock. 107: Export fcntl64. 108: Remove unused (hopefully) reent_data export. + 109: Export clock_gettime, sigwaitinfo, timer_create, timer_delete, + timer_settime */ /* Note that we forgot to bump the api for ualarm, strtoll, strtoull */ #define CYGWIN_VERSION_API_MAJOR 0 -#define CYGWIN_VERSION_API_MINOR 108 +#define CYGWIN_VERSION_API_MINOR 109 /* There is also a compatibity version number associated with the shared memory regions. It is incremented when incompatible diff --git a/winsup/cygwin/signal.cc b/winsup/cygwin/signal.cc index 4dde812..d442b17 100644 --- a/winsup/cygwin/signal.cc +++ b/winsup/cygwin/signal.cc @@ -481,11 +481,10 @@ siginterrupt (int sig, int flag) extern "C" int sigwait (const sigset_t *set, int *sig_ptr) { - siginfo_t si; - int pid = sigwaitinfo (set, NULL); - if (pid > 0) - *sig_ptr = pid; - return pid > 0 ? 0 : -1; + int sig = sigwaitinfo (set, NULL); + if (sig > 0) + *sig_ptr = sig; + return sig > 0 ? 0 : -1; } extern "C" int @@ -506,7 +505,8 @@ sigwaitinfo (const sigset_t *set, siginfo_t *info) switch (WaitForSingleObject (_my_tls.event, INFINITE)) { case WAIT_OBJECT_0: - res = _my_tls.infodata.si_pid; + res = _my_tls.infodata.si_signo; + sigproc_printf ("returning sig %d", res); if (info) *info = _my_tls.infodata; break; diff --git a/winsup/cygwin/timer.cc b/winsup/cygwin/timer.cc new file mode 100644 index 0000000..8e836e9 --- /dev/null +++ b/winsup/cygwin/timer.cc @@ -0,0 +1,275 @@ +/* timer.cc + + Copyright 2004 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 <time.h> +#include <stdlib.h> +#include "cygerrno.h" +#include "security.h" +#include "hires.h" +#include "thread.h" +#include "cygtls.h" +#include "cygthread.h" +#include "sigproc.h" +#include "sync.h" + +#define TT_MAGIC 0x513e4a1c +struct timer_tracker +{ + static muto *protect; + unsigned magic; + clockid_t clock_id; + sigevent evp; + itimerspec it; + HANDLE cancel; + int flags; + cygthread *th; + struct timer_tracker *next; + int settime (int, const itimerspec *, itimerspec *); + timer_tracker (clockid_t, const sigevent *); + timer_tracker (); +}; + +timer_tracker ttstart; + +muto *timer_tracker::protect; + +timer_tracker::timer_tracker () +{ + new_muto (protect); +} + +timer_tracker::timer_tracker (clockid_t c, const sigevent *e) +{ + if (e != NULL) + evp = *e; + else + { + evp.sigev_notify = SIGEV_SIGNAL; + evp.sigev_signo = SIGALRM; + evp.sigev_value.sival_ptr = this; + } + clock_id = c; + cancel = NULL; + flags = 0; + memset (&it, 0, sizeof (it)); + protect->acquire (); + next = ttstart.next; + ttstart.next = this; + protect->release (); + magic = TT_MAGIC; +} + +static long long +to_us (timespec& ts) +{ + long long res = ts.tv_sec; + res *= 1000000; + res += ts.tv_nsec / 1000 + ((ts.tv_nsec % 1000) >= 500 ? 1 : 0); + return res; +} + +static NO_COPY itimerspec itzero; +static NO_COPY timespec tzero; + +static DWORD WINAPI +timer_thread (VOID *x) +{ + timer_tracker *tp = ((timer_tracker *) x); + timer_tracker tt = *tp; + for (bool first = true; ; first = false) + { + long long sleep_us = to_us (first ? tt.it.it_value : tt.it.it_interval); + long long sleep_to = sleep_us; + long long now = gtod.usecs (false); + if (tt.flags & TIMER_ABSTIME) + sleep_us -= now; + else + sleep_to += now; + + DWORD sleep_ms = (sleep_us < 0) ? 0 : (sleep_us / 1000); + debug_printf ("%p waiting for %u ms, first %d", x, sleep_ms, first); + tp->it.it_value = tzero; + switch (WaitForSingleObject (tt.cancel, sleep_ms)) + { + case WAIT_TIMEOUT: + debug_printf ("timed out"); + break; + case WAIT_OBJECT_0: + now = gtod.usecs (false); + sleep_us = sleep_to - now; + if (sleep_us < 0) + sleep_us = 0; + tp->it.it_value.tv_sec = sleep_us / 1000000; + tp->it.it_value.tv_nsec = (sleep_us % 1000000) * 1000; + debug_printf ("%p cancelled, elapsed %D", x, sleep_us); + goto out; + default: + debug_printf ("%p timer wait failed, %E", x); + goto out; + } + + switch (tt.evp.sigev_notify) + { + case SIGEV_SIGNAL: + { + siginfo_t si; + memset (&si, 0, sizeof (si)); + si.si_signo = tt.evp.sigev_signo; + si.si_sigval.sival_ptr = tt.evp.sigev_value.sival_ptr; + debug_printf ("%p sending sig %d", x, tt.evp.sigev_signo); + sig_send (NULL, si); + break; + } + case SIGEV_THREAD: + { + pthread_t notify_thread; + debug_printf ("%p starting thread", x); + int rc = pthread_create (¬ify_thread, tt.evp.sigev_notify_attributes, + (void * (*) (void *)) tt.evp.sigev_notify_function, + &tt.evp.sigev_value); + if (rc) + { + debug_printf ("thread creation failed, %E"); + return 0; + } + // FIXME: pthread_join? + break; + } + } + if (!tt.it.it_interval.tv_sec && !tt.it.it_interval.tv_nsec) + break; + tt.flags = 0; + debug_printf ("looping"); + } + +out: + CloseHandle (tt.cancel); + // FIXME: race here but is it inevitable? + if (tt.cancel == tp->cancel) + tp->cancel = NULL; + return 0; +} + +static bool +it_bad (const timespec& t) +{ + if (t.tv_nsec < 0 || t.tv_nsec >= 1000000000 || t.tv_sec < 0) + { + set_errno (EINVAL); + return true; + } + return false; +} + +int +timer_tracker::settime (int in_flags, const itimerspec *value, itimerspec *ovalue) +{ + if (!value) + { + set_errno (EINVAL); + return -1; + } + + if (__check_invalid_read_ptr_errno (value, sizeof (*value))) + return -1; + + if (ovalue && check_null_invalid_struct_errno (ovalue)) + return -1; + + itimerspec *elapsed; + if (!cancel) + elapsed = &itzero; + else + { + SetEvent (cancel); // should be closed when the thread exits + th->detach (); + elapsed = ⁢ + } + + if (ovalue) + *ovalue = *elapsed; + + if (value->it_value.tv_sec || value->it_value.tv_nsec) + { + if (it_bad (value->it_value)) + return -1; + if (it_bad (value->it_interval)) + return -1; + flags = in_flags; + cancel = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL); + it = *value; + th = new cygthread (timer_thread, this, "itimer"); + } + + return 0; +} + +extern "C" int +timer_create (clockid_t clock_id, struct sigevent *evp, timer_t *timerid) +{ + if (evp && check_null_invalid_struct_errno (evp)) + return -1; + if (check_null_invalid_struct_errno (timerid)) + return -1; + + if (clock_id != CLOCK_REALTIME) + { + set_errno (EINVAL); + return -1; + } + + *timerid = (timer_t) new timer_tracker (clock_id, evp); + return 0; +} + +extern "C" int +timer_settime (timer_t timerid, int flags, const struct itimerspec *value, + struct itimerspec *ovalue) +{ + timer_tracker *tt = (timer_tracker *) timerid; + if (check_null_invalid_struct_errno (tt) || tt->magic != TT_MAGIC) + return -1; + return tt->settime (flags, value, ovalue); +} + +extern "C" int +timer_delete (timer_t timerid) +{ + timer_tracker *in_tt = (timer_tracker *) timerid; + if (check_null_invalid_struct_errno (in_tt) || in_tt->magic != TT_MAGIC) + return -1; + + timer_tracker::protect->acquire (); + for (timer_tracker *tt = &ttstart; tt->next != NULL; tt = tt->next) + if (tt->next == in_tt) + { + timer_tracker *deleteme = tt->next; + tt->next = deleteme->next; + delete deleteme; + timer_tracker::protect->release (); + return 0; + } + timer_tracker::protect->release (); + + set_errno (EINVAL); + return 0; +} + +void +fixup_timers_after_fork () +{ + for (timer_tracker *tt = &ttstart; tt->next != NULL; /* nothing */) + { + timer_tracker *deleteme = tt->next; + tt->next = deleteme->next; + delete deleteme; + } +} diff --git a/winsup/cygwin/times.cc b/winsup/cygwin/times.cc index 46e0243..635d05a 100644 --- a/winsup/cygwin/times.cc +++ b/winsup/cygwin/times.cc @@ -1,6 +1,6 @@ /* times.cc - Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003 Red Hat, Inc. + Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Red Hat, Inc. This file is part of Cygwin. @@ -15,6 +15,7 @@ details. */ #include <utime.h> #include <stdio.h> #include <stdlib.h> +#include <pthread.h> #include "cygerrno.h" #include "security.h" #include "path.h" @@ -22,6 +23,9 @@ details. */ #include "pinfo.h" #include "hires.h" #include "cygtls.h" +#include "cygthread.h" +#include "sigproc.h" +#include "sync.h" #define FACTOR (0x19db1ded53e8000LL) #define NSPERSEC 10000000LL @@ -612,7 +616,8 @@ hires_us::usecs (bool justdelta) // FIXME: Use round() here? now.QuadPart = (LONGLONG) (freq * (double) (now.QuadPart - primed_pc.QuadPart)); - return justdelta ? now.QuadPart : primed_ft.QuadPart + now.QuadPart; + LONGLONG res = justdelta ? now.QuadPart : primed_ft.QuadPart + now.QuadPart; + return res; } UINT @@ -657,3 +662,21 @@ hires_ms::usecs (bool justdelta) LONGLONG res = initime_us.QuadPart + ((LONGLONG) (now - initime_ms) * 1000); return res; } + +extern "C" int +clock_gettime (clockid_t clk_id, struct timespec *tp) +{ + if (clk_id != CLOCK_REALTIME) + { + set_errno (ENOSYS); + return -1; + } + + LONGLONG now = gtod.usecs (false); + if (now == (LONGLONG) -1) + return -1; + + tp->tv_sec = now / 1000000; + tp->tv_nsec = (now % 1000000) * 1000; + return 0; +} |