SCM Repository
Annotation of /sml/trunk/src/runtime/kernel/win32-timers.c
Parent Directory
|
Revision Log
Revision 250 - (view) (download) (as text)
1 : | monnier | 249 | /* win32-timers.c |
2 : | * | ||
3 : | * COPYRIGHT (c) 1996 Bell Laboratories, Lucent Technologies | ||
4 : | * | ||
5 : | * win32 specific interface to times and | ||
6 : | * an interface to interval timers. | ||
7 : | */ | ||
8 : | |||
9 : | #include <windows.h> | ||
10 : | #include <sys/types.h> | ||
11 : | #include <sys/timeb.h> | ||
12 : | #include "ml-base.h" | ||
13 : | #include "vproc-state.h" | ||
14 : | #include "ml-state.h" | ||
15 : | #include "ml-timer.h" | ||
16 : | #include "win32-fault.h" | ||
17 : | #include "win32-timers.h" | ||
18 : | |||
19 : | #include "signal-sysdep.h" | ||
20 : | #include "system-signals.h" | ||
21 : | |||
22 : | #ifndef MP_SUPPORT | ||
23 : | #define SELF_VPROC (VProc[0]) | ||
24 : | #else | ||
25 : | /** for MP_SUPPORT, we'll use SELF_VPROC for now **/ | ||
26 : | #define SELF_VPROC (VProc[0]) | ||
27 : | #endif | ||
28 : | |||
29 : | /* for computing times */ | ||
30 : | PVT struct _timeb start_timeb; | ||
31 : | |||
32 : | /* for interval timers */ | ||
33 : | #define WIN32_TIMER_DONE 0 | ||
34 : | #define WIN32_TIMER_COUNTING 1 | ||
35 : | #define WIN32_TIMER_HALTED 2 | ||
36 : | |||
37 : | typedef struct { | ||
38 : | HANDLE handle; | ||
39 : | DWORD id; | ||
40 : | int milli_secs; | ||
41 : | void (*action)(); | ||
42 : | } win32_timer_t; | ||
43 : | |||
44 : | |||
45 : | PVT | ||
46 : | void timer(win32_timer_t *ct) | ||
47 : | { | ||
48 : | while (1) { | ||
49 : | (*ct->action)(); | ||
50 : | Sleep(ct->milli_secs); | ||
51 : | } | ||
52 : | } | ||
53 : | |||
54 : | PVT | ||
55 : | BOOL create_win32_timer(win32_timer_t *ct, | ||
56 : | void (*f)(),int milli_secs,BOOL suspend) | ||
57 : | { | ||
58 : | /* create a thread */ | ||
59 : | ct->milli_secs = milli_secs; | ||
60 : | ct->action = f; | ||
61 : | return ((ct->handle = CreateThread(NULL, | ||
62 : | 0, /* default stack size */ | ||
63 : | (LPTHREAD_START_ROUTINE) timer, | ||
64 : | ct, | ||
65 : | suspend ? CREATE_SUSPENDED : 0, | ||
66 : | &ct->id)) != NULL); | ||
67 : | } | ||
68 : | |||
69 : | PVT | ||
70 : | BOOL destroy_win32_timer(win32_timer_t *ct) | ||
71 : | { | ||
72 : | return TerminateThread(ct->handle,1); | ||
73 : | } | ||
74 : | |||
75 : | PVT | ||
76 : | BOOL halt_win32_timer(win32_timer_t *ct) | ||
77 : | { | ||
78 : | return SuspendThread(ct->handle) != 0xffffffff; | ||
79 : | } | ||
80 : | |||
81 : | PVT | ||
82 : | BOOL resume_win32_timer(win32_timer_t *ct) | ||
83 : | { | ||
84 : | return ResumeThread(ct->handle) != 0xffffffff; | ||
85 : | } | ||
86 : | |||
87 : | PVT win32_timer_t wt; | ||
88 : | |||
89 : | bool_t win32StopTimer() | ||
90 : | { | ||
91 : | return halt_win32_timer(&wt); | ||
92 : | } | ||
93 : | |||
94 : | bool_t win32StartTimer(int milli_secs) | ||
95 : | { | ||
96 : | wt.milli_secs = milli_secs; | ||
97 : | return resume_win32_timer(&wt); | ||
98 : | } | ||
99 : | |||
100 : | PVT | ||
101 : | void win32_fake_sigalrm() | ||
102 : | { | ||
103 : | vproc_state_t *vsp = SELF_VPROC; | ||
104 : | |||
105 : | if (SuspendThread(win32_ML_thread_handle) == 0xffffffff) { | ||
106 : | Die ("win32_fake_sigalrm: unable to suspend ML thread"); | ||
107 : | } | ||
108 : | |||
109 : | win32_generic_handler(SIGALRM); | ||
110 : | |||
111 : | if (ResumeThread(win32_ML_thread_handle) == 0xffffffff) { | ||
112 : | Die ("win32_fake_sigalrm: unable to resume ML thread"); | ||
113 : | } | ||
114 : | } | ||
115 : | |||
116 : | |||
117 : | /* InitTimers: | ||
118 : | * | ||
119 : | * system specific timer initialization | ||
120 : | */ | ||
121 : | void InitTimers () | ||
122 : | { | ||
123 : | if (!create_win32_timer(&wt,win32_fake_sigalrm,0,TRUE)) { | ||
124 : | Die("InitTimers: unable to create_win32_timer"); | ||
125 : | } | ||
126 : | _ftime(&start_timeb); | ||
127 : | } /* end of InitTimers */ | ||
128 : | |||
129 : | |||
130 : | /* GetCPUTime: | ||
131 : | * | ||
132 : | * Get the elapsed user and/or system cpu times in a system independent way. | ||
133 : | */ | ||
134 : | void GetCPUTime (Time_t *usrT, Time_t *sysT) | ||
135 : | { | ||
136 : | struct _timeb now_timeb, elapsed_timeb; | ||
137 : | |||
138 : | _ftime(&now_timeb); | ||
139 : | if (now_timeb.millitm < start_timeb.millitm) { | ||
140 : | now_timeb.time--; | ||
141 : | ASSERT(now_timeb.time >= start_timeb.time); | ||
142 : | now_timeb.millitm += 1000; | ||
143 : | ASSERT(now_timeb.millitm > start_timeb.millitm); | ||
144 : | } | ||
145 : | elapsed_timeb.time = now_timeb.time - start_timeb.time; | ||
146 : | elapsed_timeb.millitm = now_timeb.millitm - start_timeb.millitm; | ||
147 : | if (usrT != NIL(Time_t *)) { | ||
148 : | usrT->seconds = (Int32_t) elapsed_timeb.time; | ||
149 : | usrT->uSeconds = ((Int32_t) elapsed_timeb.millitm) * 1000; | ||
150 : | } | ||
151 : | if (sysT != NIL(Time_t *)) { | ||
152 : | sysT->seconds = sysT->uSeconds = 0; | ||
153 : | } | ||
154 : | } | ||
155 : | |||
156 : | /* end of win32-timers.c */ |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |