drivers/timers_win32/timers_win32.cpp
author peter
Fri, 14 Mar 2008 10:55:34 +0100
changeset 417 ae068232859c
parent 406 92e28415d026
child 458 a3906286b09b
permissions -rwxr-xr-x
Forces an emission of PDO by resetting the COB of the last message
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
     1
/*
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack.
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
     3
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
     5
Copyright (C) Win32 Port Leonid Tochinski
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
     6
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
     7
See COPYING file for copyrights details.
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
     8
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
     9
This library is free software; you can redistribute it and/or
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    10
modify it under the terms of the GNU Lesser General Public
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    11
License as published by the Free Software Foundation; either
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    12
version 2.1 of the License, or (at your option) any later version.
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    13
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    14
This library is distributed in the hope that it will be useful,
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    15
but WITHOUT ANY WARRANTY; without even the implied warranty of
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    17
Lesser General Public License for more details.
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    18
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    19
You should have received a copy of the GNU Lesser General Public
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    20
License along with this library; if not, write to the Free Software
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    21
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    22
*/
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    23
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    24
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    25
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    26
#include <windows.h>
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    27
#include <stdlib.h>
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    28
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    29
extern "C"
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    30
{
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    31
#include "applicfg.h"
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    32
#include "can_driver.h"
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    33
#include "timer.h"
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    34
#include "timers_driver.h"
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    35
};
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    36
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    37
// --------------- Synchronization Object Implementation ---------------
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    38
class ccritical_section
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    39
   {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    40
   public:
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    41
      ccritical_section()
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    42
         {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    43
         ::InitializeCriticalSection(&m_cs);
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    44
         }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    45
      ~ccritical_section()
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    46
         {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    47
         ::DeleteCriticalSection(&m_cs);
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    48
         }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    49
      void enter()
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    50
         {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    51
         ::EnterCriticalSection(&m_cs);
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    52
         }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    53
      void leave()
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    54
         {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    55
         ::LeaveCriticalSection(&m_cs);
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    56
         }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    57
   private:
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    58
      CRITICAL_SECTION m_cs;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    59
   };
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    60
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    61
static ccritical_section g_cs;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    62
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    63
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    64
void EnterMutex(void)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    65
   {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    66
   g_cs.enter();
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    67
   }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    68
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    69
void LeaveMutex(void)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    70
   {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    71
   g_cs.leave();
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    72
   }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    73
// --------------- Synchronization Object Implementation ---------------
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    74
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    75
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    76
// --------------- CAN Receive Thread Implementation ---------------
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    77
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    78
void CreateReceiveTask(CAN_HANDLE fd0, TASK_HANDLE* Thread, void* ReceiveLoopPtr)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    79
   {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    80
   unsigned long thread_id = 0;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    81
   *Thread = ::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReceiveLoopPtr, fd0, 0, &thread_id);
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    82
   }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    83
406
92e28415d026 Repercutions of parameter type change in waitReceiveTaskEnd in win32
etisserant
parents: 267
diff changeset
    84
void WaitReceiveTaskEnd(TASK_HANDLE *Thread)
92e28415d026 Repercutions of parameter type change in waitReceiveTaskEnd in win32
etisserant
parents: 267
diff changeset
    85
   {
92e28415d026 Repercutions of parameter type change in waitReceiveTaskEnd in win32
etisserant
parents: 267
diff changeset
    86
   ::WaitForSingleObject(*Thread, INFINITE);
92e28415d026 Repercutions of parameter type change in waitReceiveTaskEnd in win32
etisserant
parents: 267
diff changeset
    87
   ::CloseHandle(*Thread);
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    88
   //*Thread = NULL;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    89
   }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    90
// --------------- CAN Receive Thread Implementation ---------------
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    91
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    92
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    93
// --------------- Timer Thread Implementation ---------------
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    94
class class_timers
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    95
   {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    96
   public:
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    97
      class_timers();
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    98
      ~class_timers();
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
    99
      void start_timer_thread();
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   100
      void resume_timer_thread();
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   101
      void stop_timer_thread();
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   102
      void set_timer(TIMEVAL value);
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   103
      TIMEVAL get_elapsed_time();
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   104
   private:
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   105
      TIMEVAL get_timer() const;   
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   106
      static DWORD WINAPI timer_loop_thread_proc(void* arg);
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   107
   private:
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   108
      TIMEVAL m_last_occured_alarm_time;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   109
      volatile TIMEVAL m_last_alarm_set_time;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   110
      HANDLE m_timer_thread;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   111
      volatile bool m_continue_timer_loop;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   112
      bool m_use_hi_res_timer;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   113
      double m_counts_per_usec;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   114
   };
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   115
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   116
class_timers::class_timers() : m_last_occured_alarm_time(TIMEVAL_MAX),
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   117
      m_last_alarm_set_time(TIMEVAL_MAX),
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   118
      m_timer_thread(0),
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   119
      m_continue_timer_loop(false),
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   120
      m_use_hi_res_timer(false),
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   121
      m_counts_per_usec(0.)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   122
   {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   123
   // initialize hi resolution timer
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   124
   LARGE_INTEGER counts_per_sec = {0, 0};
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   125
   if (::QueryPerformanceFrequency(&counts_per_sec) && counts_per_sec.QuadPart > 0)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   126
      {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   127
      m_use_hi_res_timer = true;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   128
      m_counts_per_usec = counts_per_sec.QuadPart / 1000000.;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   129
      }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   130
   m_use_hi_res_timer = true;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   131
   }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   132
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   133
class_timers::~class_timers()
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   134
   {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   135
   stop_timer_thread();
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   136
   }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   137
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   138
// time is in micro seconds
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   139
TIMEVAL class_timers::get_timer() const
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   140
   {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   141
   if (m_use_hi_res_timer)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   142
      {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   143
      LARGE_INTEGER performance_count = {0, 0};
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   144
      ::QueryPerformanceCounter(&performance_count);
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   145
      return (TIMEVAL)(performance_count.QuadPart / m_counts_per_usec);
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   146
      }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   147
   // hi-res timer is unavailable
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   148
   return 1000 * ::GetTickCount();
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   149
   }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   150
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   151
DWORD WINAPI class_timers::timer_loop_thread_proc(void* arg)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   152
   {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   153
   class_timers* This = reinterpret_cast<class_timers*>(arg);
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   154
   while (This->m_continue_timer_loop)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   155
      {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   156
      TIMEVAL cur_time = This->get_timer();
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   157
      if (cur_time >= This->m_last_alarm_set_time)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   158
         {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   159
         This->m_last_occured_alarm_time = cur_time;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   160
         This->m_last_alarm_set_time = TIMEVAL_MAX;         
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   161
         EnterMutex();
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   162
         TimeDispatch();
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   163
         LeaveMutex();
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   164
         }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   165
      else
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   166
         {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   167
         ::Sleep(1);
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   168
         }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   169
      }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   170
   return 0;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   171
   }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   172
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   173
void class_timers::start_timer_thread()
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   174
   {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   175
   if (m_timer_thread == 0)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   176
      {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   177
      unsigned long thread_id = 0;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   178
      m_timer_thread = ::CreateThread(NULL, 0, &timer_loop_thread_proc, this, CREATE_SUSPENDED, &thread_id);
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   179
      m_last_alarm_set_time = TIMEVAL_MAX;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   180
      m_last_occured_alarm_time = get_timer();
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   181
      }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   182
   }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   183
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   184
void class_timers::resume_timer_thread()
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   185
   {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   186
   if (m_timer_thread)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   187
      {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   188
      m_continue_timer_loop = true;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   189
      ::ResumeThread(m_timer_thread);
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   190
      }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   191
   }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   192
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   193
void class_timers::stop_timer_thread()
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   194
   {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   195
   if (m_timer_thread)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   196
      {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   197
      m_continue_timer_loop = false;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   198
      ::WaitForSingleObject(m_timer_thread, INFINITE);
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   199
      ::CloseHandle(m_timer_thread);
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   200
      m_timer_thread = 0;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   201
      }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   202
   }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   203
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   204
void class_timers::set_timer(TIMEVAL value)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   205
   {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   206
   m_last_alarm_set_time = (value == TIMEVAL_MAX) ? TIMEVAL_MAX : get_timer() + value;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   207
   }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   208
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   209
// elapsed time since last occured alarm
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   210
TIMEVAL class_timers::get_elapsed_time()
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   211
   {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   212
   return get_timer() - m_last_occured_alarm_time;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   213
   }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   214
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   215
// ----------------------------------------------------------
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   216
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   217
static class_timers s_timers;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   218
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   219
void StartTimerLoop(TimerCallback_t init_callback)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   220
   {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   221
   s_timers.start_timer_thread();
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   222
   // At first, TimeDispatch will call init_callback.
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   223
   if (init_callback != NULL)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   224
      SetAlarm(NULL, 0, init_callback, (TIMEVAL)0, (TIMEVAL)0);
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   225
   s_timers.resume_timer_thread();
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   226
   }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   227
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   228
void StopTimerLoop(void)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   229
   {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   230
   s_timers.stop_timer_thread();
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   231
   }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   232
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   233
void setTimer(TIMEVAL value)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   234
   {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   235
   s_timers.set_timer(value);
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   236
   }
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   237
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   238
TIMEVAL getElapsedTime(void)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   239
   {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   240
   return s_timers.get_elapsed_time();
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
diff changeset
   241
   }