aboutsummaryrefslogtreecommitdiff
path: root/win32
diff options
context:
space:
mode:
Diffstat (limited to 'win32')
-rw-r--r--win32/mingw.c124
1 files changed, 0 insertions, 124 deletions
diff --git a/win32/mingw.c b/win32/mingw.c
index 6b63e2487..14cfdd992 100644
--- a/win32/mingw.c
+++ b/win32/mingw.c
@@ -707,130 +707,6 @@ clock_t times(struct tms *buf)
707 return 0; 707 return 0;
708} 708}
709 709
710static HANDLE timer_event;
711static HANDLE timer_thread;
712static int timer_interval;
713static int one_shot;
714static sighandler_t timer_fn = SIG_DFL;
715
716/* The timer works like this:
717 * The thread, ticktack(), is a trivial routine that most of the time
718 * only waits to receive the signal to terminate. The main thread tells
719 * the thread to terminate by setting the timer_event to the signalled
720 * state.
721 * But ticktack() interrupts the wait state after the timer's interval
722 * length to call the signal handler.
723 */
724
725static __stdcall unsigned ticktack(void *dummy UNUSED_PARAM)
726{
727 while (WaitForSingleObject(timer_event, timer_interval) == WAIT_TIMEOUT) {
728 if (timer_fn == SIG_DFL)
729 bb_error_msg_and_die("Alarm");
730 if (timer_fn != SIG_IGN)
731 timer_fn(SIGALRM);
732 if (one_shot)
733 break;
734 }
735 return 0;
736}
737
738static int start_timer_thread(void)
739{
740 timer_event = CreateEvent(NULL, FALSE, FALSE, NULL);
741 if (timer_event) {
742 timer_thread = (HANDLE) _beginthreadex(NULL, 0, ticktack, NULL, 0, NULL);
743 if (!timer_thread ) {
744 errno = ENOMEM;
745 return -1;
746 }
747 } else {
748 errno = ENOMEM;
749 return -1;
750 }
751 return 0;
752}
753
754static void stop_timer_thread(void)
755{
756 if (timer_event)
757 SetEvent(timer_event); /* tell thread to terminate */
758 if (timer_thread) {
759 int rc = WaitForSingleObject(timer_thread, 1000);
760 if (rc == WAIT_TIMEOUT)
761 fprintf(stderr, "timer thread did not terminate timely");
762 else if (rc != WAIT_OBJECT_0)
763 fprintf(stderr, "waiting for timer thread failed: %lu",
764 GetLastError());
765 CloseHandle(timer_thread);
766 }
767 if (timer_event)
768 CloseHandle(timer_event);
769 timer_event = NULL;
770 timer_thread = NULL;
771}
772
773static inline int is_timeval_eq(const struct timeval *i1, const struct timeval *i2)
774{
775 return i1->tv_sec == i2->tv_sec && i1->tv_usec == i2->tv_usec;
776}
777
778int setitimer(int type UNUSED_PARAM, struct itimerval *in, struct itimerval *out)
779{
780 static const struct timeval zero;
781 static int atexit_done;
782
783 if (out != NULL) {
784 errno = EINVAL;
785 return -1;
786 }
787 if (!is_timeval_eq(&in->it_interval, &zero) &&
788 !is_timeval_eq(&in->it_interval, &in->it_value)) {
789 errno = EINVAL;
790 return -1;
791 }
792
793 if (timer_thread)
794 stop_timer_thread();
795
796 if (is_timeval_eq(&in->it_value, &zero) &&
797 is_timeval_eq(&in->it_interval, &zero))
798 return 0;
799
800 timer_interval = in->it_value.tv_sec * 1000 + in->it_value.tv_usec / 1000;
801 one_shot = is_timeval_eq(&in->it_interval, &zero);
802 if (!atexit_done) {
803 atexit(stop_timer_thread);
804 atexit_done = 1;
805 }
806 return start_timer_thread();
807}
808
809int sigaction(int sig, struct sigaction *in, struct sigaction *out)
810{
811 if (sig != SIGALRM) {
812 errno = EINVAL;
813 return -1;
814 }
815 if (out != NULL) {
816 errno = EINVAL;
817 return -1;
818 }
819
820 timer_fn = in->sa_handler;
821 return 0;
822}
823
824#undef signal
825sighandler_t mingw_signal(int sig, sighandler_t handler)
826{
827 sighandler_t old = timer_fn;
828 if (sig != SIGALRM)
829 return signal(sig, handler);
830 timer_fn = handler;
831 return old;
832}
833
834int link(const char *oldpath, const char *newpath) 710int link(const char *oldpath, const char *newpath)
835{ 711{
836 typedef BOOL (WINAPI *T)(const char*, const char*, LPSECURITY_ATTRIBUTES); 712 typedef BOOL (WINAPI *T)(const char*, const char*, LPSECURITY_ATTRIBUTES);