author | bmakuc <blaz.makuc@smarteh.si> |
Fri, 24 Mar 2017 10:52:38 +0100 | |
changeset 796 | 1c87f7a8cb8a |
parent 697 | 24a2aec61731 |
child 801 | 32d146b64a35 |
permissions | -rw-r--r-- |
556 | 1 |
/* |
2 |
This file is part of CanFestival, a library implementing CanOpen Stack. |
|
3 |
||
4 |
Copyright (C): Edouard TISSERANT and Francis DUPIN |
|
5 |
Copyright (C) Win32 Port Leonid Tochinski |
|
6 |
||
7 |
See COPYING file for copyrights details. |
|
8 |
||
9 |
This library is free software; you can redistribute it and/or |
|
10 |
modify it under the terms of the GNU Lesser General Public |
|
11 |
License as published by the Free Software Foundation; either |
|
12 |
version 2.1 of the License, or (at your option) any later version. |
|
13 |
||
14 |
This library is distributed in the hope that it will be useful, |
|
15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
17 |
Lesser General Public License for more details. |
|
18 |
||
19 |
You should have received a copy of the GNU Lesser General Public |
|
20 |
License along with this library; if not, write to the Free Software |
|
21 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
22 |
*/ |
|
23 |
||
24 |
#include <windows.h> |
|
25 |
#include <stdlib.h> |
|
26 |
#include <sys/timeb.h> |
|
27 |
||
591 | 28 |
#ifdef __cplusplus |
29 |
extern "C" { |
|
30 |
#endif |
|
31 |
||
556 | 32 |
#include "applicfg.h" |
33 |
#include "can_driver.h" |
|
34 |
#include "timer.h" |
|
35 |
#include "timers_driver.h" |
|
591 | 36 |
|
37 |
#ifdef __cplusplus |
|
577 | 38 |
}; |
591 | 39 |
#endif |
556 | 40 |
|
680 | 41 |
DWORD timebuffer; |
556 | 42 |
|
43 |
/* Synchronization Object Implementation */ |
|
44 |
CRITICAL_SECTION CanFestival_mutex; |
|
45 |
HANDLE timer_thread = NULL; |
|
46 |
HANDLE timer = NULL; |
|
47 |
||
697
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
680
diff
changeset
|
48 |
volatile int stop_timer=0; |
556 | 49 |
|
50 |
static TimerCallback_t init_callback; |
|
51 |
||
52 |
||
53 |
void EnterMutex(void) |
|
54 |
{ |
|
55 |
EnterCriticalSection(&CanFestival_mutex); |
|
56 |
} |
|
57 |
||
58 |
void LeaveMutex(void) |
|
59 |
{ |
|
60 |
LeaveCriticalSection(&CanFestival_mutex); |
|
61 |
} |
|
62 |
||
63 |
// --------------- CAN Receive Thread Implementation --------------- |
|
64 |
||
65 |
void CreateReceiveTask(CAN_HANDLE fd0, TASK_HANDLE* Thread, void* ReceiveLoopPtr) |
|
66 |
{ |
|
67 |
unsigned long thread_id = 0; |
|
68 |
*Thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReceiveLoopPtr, fd0, 0, &thread_id); |
|
69 |
} |
|
70 |
||
71 |
void WaitReceiveTaskEnd(TASK_HANDLE *Thread) |
|
72 |
{ |
|
600
7767029937aa
add timeout for waitreceivetaskend for the win32, fix GetLastError print
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents:
591
diff
changeset
|
73 |
if(WaitForSingleObject(*Thread, 1000) == WAIT_TIMEOUT) |
7767029937aa
add timeout for waitreceivetaskend for the win32, fix GetLastError print
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents:
591
diff
changeset
|
74 |
{ |
7767029937aa
add timeout for waitreceivetaskend for the win32, fix GetLastError print
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents:
591
diff
changeset
|
75 |
TerminateThread(*Thread, -1); |
7767029937aa
add timeout for waitreceivetaskend for the win32, fix GetLastError print
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents:
591
diff
changeset
|
76 |
} |
556 | 77 |
CloseHandle(*Thread); |
78 |
} |
|
79 |
||
629 | 80 |
#if !defined(WIN32) || defined(__CYGWIN__) |
556 | 81 |
int TimerThreadLoop(void) |
629 | 82 |
#else |
83 |
DWORD TimerThreadLoop(LPVOID arg) |
|
84 |
#endif |
|
556 | 85 |
{ |
605
f91ee161b3a1
-add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents:
591
diff
changeset
|
86 |
|
556 | 87 |
|
571 | 88 |
while(!stop_timer) |
556 | 89 |
{ |
571 | 90 |
WaitForSingleObject(timer, INFINITE); |
91 |
if(stop_timer) |
|
92 |
break; |
|
556 | 93 |
EnterMutex(); |
680 | 94 |
timebuffer = GetTickCount(); |
556 | 95 |
TimeDispatch(); |
96 |
LeaveMutex(); |
|
97 |
} |
|
98 |
return 0; |
|
99 |
} |
|
100 |
||
101 |
void TimerInit(void) |
|
102 |
{ |
|
103 |
LARGE_INTEGER liDueTime; |
|
104 |
liDueTime.QuadPart = 0; |
|
105 |
||
106 |
InitializeCriticalSection(&CanFestival_mutex); |
|
107 |
||
571 | 108 |
timer = CreateWaitableTimer(NULL, FALSE, NULL); |
556 | 109 |
if(NULL == timer) |
110 |
{ |
|
111 |
printf("CreateWaitableTimer failed (%d)\n", GetLastError()); |
|
112 |
} |
|
113 |
||
114 |
// Take first absolute time ref in milliseconds. |
|
680 | 115 |
timebuffer = GetTickCount(); |
556 | 116 |
} |
117 |
||
118 |
void TimerCleanup(void) |
|
119 |
{ |
|
120 |
DeleteCriticalSection(&CanFestival_mutex); |
|
121 |
} |
|
122 |
||
123 |
void StopTimerLoop(TimerCallback_t exitfunction) |
|
124 |
{ |
|
125 |
EnterMutex(); |
|
126 |
exitfunction(NULL,0); |
|
127 |
LeaveMutex(); |
|
128 |
||
129 |
stop_timer = 1; |
|
571 | 130 |
setTimer(0); |
599
b2d2c3fab094
Added timeout for waiting timer thread end on windows.
edouard
parents:
591
diff
changeset
|
131 |
if(WaitForSingleObject(timer_thread,1000) == WAIT_TIMEOUT) |
b2d2c3fab094
Added timeout for waiting timer thread end on windows.
edouard
parents:
591
diff
changeset
|
132 |
{ |
b2d2c3fab094
Added timeout for waiting timer thread end on windows.
edouard
parents:
591
diff
changeset
|
133 |
TerminateThread(timer_thread, -1); |
b2d2c3fab094
Added timeout for waiting timer thread end on windows.
edouard
parents:
591
diff
changeset
|
134 |
} |
556 | 135 |
CloseHandle(timer); |
136 |
CloseHandle(timer_thread); |
|
137 |
} |
|
138 |
||
139 |
void StartTimerLoop(TimerCallback_t _init_callback) |
|
140 |
{ |
|
141 |
unsigned long timer_thread_id; |
|
142 |
stop_timer = 0; |
|
143 |
init_callback = _init_callback; |
|
605
f91ee161b3a1
-add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents:
591
diff
changeset
|
144 |
EnterMutex(); |
f91ee161b3a1
-add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents:
591
diff
changeset
|
145 |
// At first, TimeDispatch will call init_callback. |
f91ee161b3a1
-add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents:
591
diff
changeset
|
146 |
SetAlarm(NULL, 0, init_callback, 0, 0); |
f91ee161b3a1
-add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents:
591
diff
changeset
|
147 |
LeaveMutex(); |
556 | 148 |
timer_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)TimerThreadLoop, NULL, 0, &timer_thread_id); |
149 |
} |
|
150 |
||
151 |
/* Set the next alarm */ |
|
152 |
void setTimer(TIMEVAL value) |
|
153 |
{ |
|
154 |
if(value == TIMEVAL_MAX) |
|
155 |
CancelWaitableTimer(timer); |
|
156 |
else |
|
157 |
{ |
|
158 |
LARGE_INTEGER liDueTime; |
|
159 |
||
160 |
/* arg 2 of SetWaitableTimer take 100 ns interval */ |
|
680 | 161 |
liDueTime.QuadPart = ((long long) (-1) * value * 10000); |
556 | 162 |
//printf("SetTimer(%llu)\n", value); |
163 |
||
164 |
if (!SetWaitableTimer(timer, &liDueTime, 0, NULL, NULL, FALSE)) |
|
165 |
{ |
|
166 |
printf("SetWaitableTimer failed (%d)\n", GetLastError()); |
|
167 |
} |
|
168 |
} |
|
169 |
} |
|
170 |
||
171 |
/* Get the elapsed time since the last occured alarm */ |
|
172 |
TIMEVAL getElapsedTime(void) |
|
173 |
{ |
|
680 | 174 |
DWORD timetmp = GetTickCount(); |
175 |
return (timetmp - timebuffer); |
|
556 | 176 |
} |
177 |