1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
/* MSPDebug - debugging tool for MSP430 MCUs
* Copyright (C) 2009-2012 Daniel Beer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <unistd.h>
#include "ctrlc.h"
#ifdef __Windows__
#include <windows.h>
static int ctrlc_flag;
static HANDLE ctrlc_event;
static CRITICAL_SECTION ctrlc_cs;
static WINAPI BOOL ctrlc_handler(DWORD event)
{
if ((event == CTRL_C_EVENT) || (event == CTRL_BREAK_EVENT)) {
ctrlc_raise();
return TRUE;
}
return FALSE;
}
void ctrlc_init(void)
{
ctrlc_event = CreateEvent(0, TRUE, FALSE, NULL);
InitializeCriticalSection(&ctrlc_cs);
SetConsoleCtrlHandler(ctrlc_handler, TRUE);
}
void ctrlc_exit(void)
{
SetConsoleCtrlHandler(NULL, TRUE);
DeleteCriticalSection(&ctrlc_cs);
CloseHandle(ctrlc_event);
}
int ctrlc_check(void)
{
int cc;
EnterCriticalSection(&ctrlc_cs);
cc = ctrlc_flag;
LeaveCriticalSection(&ctrlc_cs);
return cc;
}
void ctrlc_clear(void)
{
EnterCriticalSection(&ctrlc_cs);
ctrlc_flag = 0;
ResetEvent(ctrlc_event);
LeaveCriticalSection(&ctrlc_cs);
}
void ctrlc_raise(void)
{
EnterCriticalSection(&ctrlc_cs);
ctrlc_flag = 1;
SetEvent(ctrlc_event);
LeaveCriticalSection(&ctrlc_cs);
}
HANDLE ctrlc_win32_event(void)
{
return ctrlc_event;
}
#else /* __Windows__ */
#include <pthread.h>
#include <signal.h>
static volatile sig_atomic_t ctrlc_flag;
static pthread_t ctrlc_thread;
static void sigint_handler(int signum)
{
(void)signum;
ctrlc_flag = 1;
}
void ctrlc_init(void)
{
#ifndef __CYGWIN__
static const struct sigaction siga = {
.sa_handler = sigint_handler,
.sa_flags = 0
};
#endif
ctrlc_thread = pthread_self();
#ifdef __CYGWIN__
signal(SIGINT, sigint_handler);
#else
sigaction(SIGINT, &siga, NULL);
#endif
}
void ctrlc_exit(void)
{
signal(SIGINT, SIG_DFL);
}
void ctrlc_clear(void)
{
ctrlc_flag = 0;
}
void ctrlc_raise(void)
{
pthread_kill(ctrlc_thread, SIGINT);
}
int ctrlc_check(void)
{
#ifdef __CYGWIN__
/* Cygwin's signal emulation seems to require the process to
* block.
*/
usleep(1);
#endif
return ctrlc_flag;
}
#endif
|