#750788 clarification of alarm() support status

#750788#5
Date:
2014-06-06 22:11:35 UTC
From:
To:
Package: mingw-w64-i686-dev
Version: 3.1.0-1
Severity: wishlist

Hello Stephen,

It would be nice if mingw would provide alarm.
Maybe it kinda does actually:
/usr/i686-w64-mingw32/include/io.h:unsigned int alarm(unsigned int seconds);
/usr/i686-w64-mingw32/include/signal.h:#define      SIGALRM 14      /* alarm clock */

but it does not seem to work (at least under wine) and
alarm is supposed to be in unistd.h.

There is a lot of confusion on the web wheter mingw support alarm, whether
it is implemented as a noop, why it is in io.h, etc.

Some clarification would be very welcome.

Cheers,

#750788#10
Date:
2014-06-07 12:41:23 UTC
From:
To:
Hi Bill,

It is implemented as a noop; in mingw-w64-crt/misc/alarm.c in the mingw-w64
source code:

unsigned int alarm(unsigned int __UNUSED_PARAM(seconds))
{
  return 0;
}

I'll forward your request upstream!

Regards,

Stephen

#750788#19
Date:
2014-06-07 18:55:04 UTC
From:
To:
Hello Stephen, thanks again for your answer.

Is there a clear documentation of the mingw API ? (at least the difference
between mingw and POSIX) Then I would not need to bother you.


Thanks, though I understand win32 signal model might not make it easy.

Cheers,

#750788#24
Date:
2014-06-07 19:06:41 UTC
From:
To:
The API is "documented" on
https://sourceforge.net/apps/trac/mingw-w64/wiki/Mingw-w64%20API%20documentation
but very little of the API is described.

Yes, I imagine it would have to be emulated using SetTimer but even then
producing a signal from the timer function might be difficult...

Regards,

Stephen

#750788#29
Date:
2014-06-17 11:18:47 UTC
From:
To:
Well I came with the code below.
This works, but of course it should do raise(SIGALRM), but
mingw does not support catching SIGALRM.

static HANDLE hTimerQueue = NULL;

static void CALLBACK
win32_cb_alarm(void *lpParam, BOOLEAN TimerOrWaitFired)
{
  raise(SIGINT);
}

void
win32_alarm(unsigned int s)
{
  if (hTimerQueue)
  {
    HANDLE oldhTimerQueue = hTimerQueue;
    hTimerQueue = NULL;
    DeleteTimerQueue(oldhTimerQueue);
  }
  if (s)
  {
    void *arg = NULL;
    HANDLE hTimer = NULL;
    hTimerQueue = CreateTimerQueue();
    CreateTimerQueueTimer( &hTimer, hTimerQueue,
        (WAITORTIMERCALLBACK)win32_cb_alarm, &arg , s*1000, 0, 0);
  }
}

Cheers,