Skip to content

Commit

Permalink
- implement alarm in Windows (using thread/sleep/kill process)
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@18952 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adrpo committed Feb 2, 2014
1 parent 59ae476 commit 5a6fb36
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion Compiler/runtime/systemimpl.c
Expand Up @@ -78,13 +78,57 @@ typedef void* iconv_t;
#if defined(__MINGW32__) || defined(_MSC_VER)
#define getFunctionPointerFromDLL GetProcAddress
#define FreeLibraryFromHandle !FreeLibrary
#else

#ifndef SIGALRM
#define SIGALRM SIGTERM
#endif

static HANDLE thread = 0; // thread handle

static DWORD WINAPI killProcess (LPVOID arg)
{
Sleep (1000 * ((unsigned int)arg));
fprintf(stdout, "Killed"); fflush(NULL);
TerminateProcess(GetCurrentProcess(), 1);
return 0;
}


unsigned int alarm (unsigned int nsec)
{
static unsigned pending = 0; // previous alarm() argument
static time_t t0 = 0; // start of previous alarm()
time_t unslept = 0; // seconds until previous alarm expires

if (thread) {
// previous alarm is still pending, cancel it
unslept = pending - (time (0) - t0);
TerminateThread (thread, 0);
CloseHandle (thread);
thread = 0;
}

pending = nsec;

if (nsec) {
time (&t0); // keep track of when count down started
DWORD threadId;
thread = CreateThread (0, 0, killProcess, (void*)nsec, 0, &threadId);
}

return (unsigned int)(unslept);
}

#else /* *nix / Mac */

#define getFunctionPointerFromDLL dlsym
#define FreeLibraryFromHandle dlclose
#define GetLastError(X) 1L
#include <fcntl.h>

#endif


/*
* Platform specific includes and defines
*/
Expand Down

0 comments on commit 5a6fb36

Please sign in to comment.