I don't really understand the intricacies of cygwin signal delivery, but I see
that it can suspend the target thread to determine if it's in a safe place to
deliver the signal (outside a win32 API call).  I think that sometimes the
thread is not correctly resumed.

This appears to be the cause of been a long-standing problem with the X.org X
server on cygwin, which we work around by disabling smart-scheduling (no great
loss), but I've only just recently understood enough about the problem to
produce a STC.

If you run the attached for a while, it stops.  According to Process Hacker,
the main thread is in the suspended state, inside ntdll.dll. e.g.:

$ uname -a
CYGWIN_NT-5.1 byron 1.7.26(0.271/5/3) 2013-11-29 11:25 i686 Cygwin

$ ./signal-in-kernel32
hMod is 0x7c800000
iteration 1
[...]
iteration 139325
# stops!
#include <assert.h>
#include <sys/time.h>
#include <signal.h>
#include <stdio.h>
#include <windows.h>

long SmartScheduleInterval = 20; /* ms */
long SmartScheduleTime = 0;

static void
SmartScheduleTimer(int sig)
{
    SmartScheduleTime += SmartScheduleInterval;
}

void
SmartScheduleStartTimer(void)
{
    struct itimerval timer;
    timer.it_interval.tv_sec = 0;
    timer.it_interval.tv_usec = SmartScheduleInterval * 1000;
    timer.it_value.tv_sec = 0;
    timer.it_value.tv_usec = SmartScheduleInterval * 1000;
    setitimer(ITIMER_REAL, &timer, 0);
}

int main()
{
    /* Set up the timer signal function */
    struct sigaction act;
    act.sa_handler = SmartScheduleTimer;
    sigemptyset(&act.sa_mask);
    sigaddset(&act.sa_mask, SIGALRM);
    if (sigaction(SIGALRM, &act, 0) < 0) {
        perror("sigaction failed");
        return -1;
    }

   HMODULE hMod = LoadLibraryEx("kernel32.dll", NULL, 0);
   assert(hMod);
   printf("hMod is %p\n", hMod);

   /* start timer */
   SmartScheduleStartTimer();

   /* Loop forever, spending most of our time inside ntdll */
   int i = 0;
   while (1) {
      void *gpa = GetProcAddress(hMod, "GetProcAddress");
      assert(gpa);
      i++;
      printf("iteration %d\n", i);
  }
}

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

Reply via email to