public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: "Jon Beniston" <jon@beniston.com>
To: <cygwin@cygwin.com>
Subject: SIGINT lost while program calls PeekMessage
Date: Thu, 2 Jul 2020 17:16:10 +0100	[thread overview]
Message-ID: <01ea01d6508c$1a340a70$4e9c1f50$@beniston.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 813 bytes --]

Hi,

I have a Cygwin program that:

- registers a SIGINT handler, via signal(SIGINT,h)
- creates a window, using Win32 CreateWindow
- and is then calling PeekMessage for that window in a loop

It appears that while PeekMessage is being called, any SIGINTs sent to the
program are lost. Is this to be expected as it's not really supported or a
bug?

To reproduce:

- Compile and run the attached example program, and repeatedly press CTRL-C
in the console. 
- Only maybe 1 of every 10 or 20 times CTRL-C is pressed, is the signal
handler called.
- Similarly, running 'kill -SIGINT PID' results in most of the signals being
ignored.
- Remove the call to PeekMessage, and the signals are received. 

Cheers,
Jon


Windows 10 Professional Ver 10.0 Build 18363
    Cygwin DLL version info:
        DLL version: 3.1.5


[-- Attachment #2: sigint_example.c --]
[-- Type: application/octet-stream, Size: 1945 bytes --]

#include <signal.h>
#include <stdio.h>

#include <windows.h>

int cnt;
HWND window;
volatile int x = 0;

void ctrl_c_handler(int x)
{
  signal(SIGINT, ctrl_c_handler);
  printf("sig=%d cnt=%d\n", x, ++cnt);
}

LRESULT CALLBACK window_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch (message) 
    {
    case WM_DESTROY:
      PostQuitMessage(0);
      x=1;
      break;
      
    default:
      return DefWindowProc(hWnd, message, wParam, lParam);
  }
  return 0;
}

void create_win()
{
  HINSTANCE hInstance;
  WNDCLASSEX wcex; 
             
  hInstance = GetModuleHandle(NULL);
    
  wcex.cbSize = sizeof(wcex);
  wcex.style = CS_HREDRAW | CS_VREDRAW;
  wcex.lpfnWndProc = window_proc;
  wcex.cbClsExtra = 0;
  wcex.cbWndExtra = 0;
  wcex.hInstance = hInstance;
  wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  wcex.lpszMenuName = NULL;
  wcex.lpszClassName = "CtrlCWin";
  wcex.hIconSm = NULL;
                     
  if (RegisterClassEx(&wcex))
    {        
      window = CreateWindow("CtrlCWin", "CtrlCWin", WS_OVERLAPPEDWINDOW,
                                 CW_USEDEFAULT, CW_USEDEFAULT, 270, 230, NULL, 
                                 NULL, hInstance, NULL);
      if (window) 
        {
          ShowWindow(window, SW_SHOW);
          UpdateWindow(window);
        }
    }
}

int main()
{
  MSG msg;
  BOOL bRet;

  signal(SIGINT, ctrl_c_handler);
  
  create_win();

  while(!x)
  {
    /* If this call to PeekMessage is removed, signals get through */ 
    while (PeekMessage(&msg, window, 0, 0, PM_NOREMOVE)) 
      {
        bRet = GetMessage(&msg, window, 0, 0);
        if (bRet > 0) 
          {  
            TranslateMessage(&msg);
            DispatchMessage(&msg);
          }  
      }    
  }
  return 0;
}

             reply	other threads:[~2020-07-02 16:16 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-02 16:16 Jon Beniston [this message]
2020-07-03 19:22 ` Corinna Vinschen
2020-07-06 13:16 Jon Beniston
2020-07-06 18:21 ` Corinna Vinschen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='01ea01d6508c$1a340a70$4e9c1f50$@beniston.com' \
    --to=jon@beniston.com \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).