os_port_windows.c
Go to the documentation of this file.
1 /**
2  * @file os_port_windows.c
3  * @brief RTOS abstraction layer (Windows)
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program 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
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  *
23  * @author Oryx Embedded SARL (www.oryx-embedded.com)
24  * @version 1.9.0
25  **/
26 
27 //Switch to the appropriate trace level
28 #define TRACE_LEVEL TRACE_LEVEL_OFF
29 
30 //Memory leaks detection
31 #if (defined(_WIN32) && defined(_DEBUG))
32  #define _CRTDBG_MAP_ALLOC
33  #include <stdlib.h>
34  #include <crtdbg.h>
35 #endif
36 
37 //Dependencies
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <windows.h>
41 #include "os_port.h"
42 #include "os_port_windows.h"
43 #include "debug.h"
44 
45 
46 /**
47  * @brief Kernel initialization
48  **/
49 
50 void osInitKernel(void)
51 {
52  //Not implemented
53 }
54 
55 
56 /**
57  * @brief Start kernel
58  **/
59 
60 void osStartKernel(void)
61 {
62  //Not implemented
63 }
64 
65 
66 /**
67  * @brief Create a new task
68  * @param[in] name A name identifying the task
69  * @param[in] taskCode Pointer to the task entry function
70  * @param[in] param A pointer to a variable to be passed to the task
71  * @param[in] stackSize The initial size of the stack, in words
72  * @param[in] priority The priority at which the task should run
73  * @return If the function succeeds, the return value is a pointer to the
74  * new task. If the function fails, the return value is NULL
75  **/
76 
78  void *param, size_t stackSize, int_t priority)
79 {
80  void *handle;
81 
82  //Create a new thread
83  handle = CreateThread(NULL, 0,
84  (LPTHREAD_START_ROUTINE) taskCode, param, 0, NULL);
85 
86  //Return a pointer to the newly created thread
87  return handle;
88 }
89 
90 
91 /**
92  * @brief Delete a task
93  * @param[in] task Pointer to the task to be deleted
94  **/
95 
96 void osDeleteTask(OsTask *task)
97 {
98  //Delete the calling thread?
99  if(task == NULL)
100  {
101  //Kill ourselves
102  ExitThread(0);
103  }
104  else
105  {
106  //Delete the specified thread
107  TerminateThread(task, 0);
108  }
109 }
110 
111 
112 /**
113  * @brief Delay routine
114  * @param[in] delay Amount of time for which the calling task should block
115  **/
116 
118 {
119  //Delay the task for the specified duration
120  Sleep(delay);
121 }
122 
123 
124 /**
125  * @brief Yield control to the next task
126  **/
127 
128 void osSwitchTask(void)
129 {
130  //Not implemented
131 }
132 
133 
134 /**
135  * @brief Suspend scheduler activity
136  **/
137 
139 {
140  //Not implemented
141 }
142 
143 
144 /**
145  * @brief Resume scheduler activity
146  **/
147 
149 {
150  //Not implemented
151 }
152 
153 
154 /**
155  * @brief Create an event object
156  * @param[in] event Pointer to the event object
157  * @return The function returns TRUE if the event object was successfully
158  * created. Otherwise, FALSE is returned
159  **/
160 
162 {
163  //Create an event object
164  event->handle = CreateEvent(NULL, FALSE, FALSE, NULL);
165 
166  //Check whether the returned handle is valid
167  if(event->handle != NULL)
168  return TRUE;
169  else
170  return FALSE;
171 }
172 
173 
174 /**
175  * @brief Delete an event object
176  * @param[in] event Pointer to the event object
177  **/
178 
179 void osDeleteEvent(OsEvent *event)
180 {
181  //Make sure the handle is valid
182  if(event->handle != NULL)
183  {
184  //Properly dispose the event object
185  CloseHandle(event->handle);
186  }
187 }
188 
189 
190 /**
191  * @brief Set the specified event object to the signaled state
192  * @param[in] event Pointer to the event object
193  **/
194 
195 void osSetEvent(OsEvent *event)
196 {
197  //Set the specified event to the signaled state
198  SetEvent(event->handle);
199 }
200 
201 
202 /**
203  * @brief Set the specified event object to the nonsignaled state
204  * @param[in] event Pointer to the event object
205  **/
206 
207 void osResetEvent(OsEvent *event)
208 {
209  //Force the specified event to the nonsignaled state
210  ResetEvent(event->handle);
211 }
212 
213 
214 /**
215  * @brief Wait until the specified event is in the signaled state
216  * @param[in] event Pointer to the event object
217  * @param[in] timeout Timeout interval
218  * @return The function returns TRUE if the state of the specified object is
219  * signaled. FALSE is returned if the timeout interval elapsed
220  **/
221 
223 {
224  //Wait until the specified event is in the signaled state
225  if(WaitForSingleObject(event->handle, timeout) == WAIT_OBJECT_0)
226  return TRUE;
227  else
228  return FALSE;
229 }
230 
231 
232 /**
233  * @brief Set an event object to the signaled state from an interrupt service routine
234  * @param[in] event Pointer to the event object
235  * @return TRUE if setting the event to signaled state caused a task to unblock
236  * and the unblocked task has a priority higher than the currently running task
237  **/
238 
240 {
241  //Not implemented
242  return FALSE;
243 }
244 
245 
246 /**
247  * @brief Create a semaphore object
248  * @param[in] semaphore Pointer to the semaphore object
249  * @param[in] count The maximum count for the semaphore object. This value
250  * must be greater than zero
251  * @return The function returns TRUE if the semaphore was successfully
252  * created. Otherwise, FALSE is returned
253  **/
254 
256 {
257  //Create a semaphore object
258  semaphore->handle = CreateSemaphore(NULL, count, count, NULL);
259 
260  //Check whether the returned handle is valid
261  if(semaphore->handle != NULL)
262  return TRUE;
263  else
264  return FALSE;
265 }
266 
267 
268 /**
269  * @brief Delete a semaphore object
270  * @param[in] semaphore Pointer to the semaphore object
271  **/
272 
274 {
275  //Make sure the handle is valid
276  if(semaphore->handle != NULL)
277  {
278  //Properly dispose the semaphore object
279  CloseHandle(semaphore->handle);
280  }
281 }
282 
283 
284 /**
285  * @brief Wait for the specified semaphore to be available
286  * @param[in] semaphore Pointer to the semaphore object
287  * @param[in] timeout Timeout interval
288  * @return The function returns TRUE if the semaphore is available. FALSE is
289  * returned if the timeout interval elapsed
290  **/
291 
293 {
294  //Wait until the specified semaphore becomes available
295  if(WaitForSingleObject(semaphore->handle, timeout) == WAIT_OBJECT_0)
296  return TRUE;
297  else
298  return FALSE;
299 }
300 
301 
302 /**
303  * @brief Release the specified semaphore object
304  * @param[in] semaphore Pointer to the semaphore object
305  **/
306 
308 {
309  //Release the semaphore
310  ReleaseSemaphore(semaphore->handle, 1, NULL);
311 }
312 
313 
314 /**
315  * @brief Create a mutex object
316  * @param[in] mutex Pointer to the mutex object
317  * @return The function returns TRUE if the mutex was successfully
318  * created. Otherwise, FALSE is returned
319  **/
320 
322 {
323  //Create a mutex object
324  mutex->handle = CreateMutex(NULL, FALSE, NULL);
325 
326  //Check whether the returned handle is valid
327  if(mutex->handle != NULL)
328  return TRUE;
329  else
330  return FALSE;
331 }
332 
333 
334 /**
335  * @brief Delete a mutex object
336  * @param[in] mutex Pointer to the mutex object
337  **/
338 
339 void osDeleteMutex(OsMutex *mutex)
340 {
341  //Make sure the handle is valid
342  if(mutex->handle != NULL)
343  {
344  //Properly dispose the mutex object
345  CloseHandle(mutex->handle);
346  }
347 }
348 
349 
350 /**
351  * @brief Acquire ownership of the specified mutex object
352  * @param[in] mutex A handle to the mutex object
353  **/
354 
356 {
357  //Obtain ownership of the mutex object
358  WaitForSingleObject(mutex->handle, INFINITE);
359 }
360 
361 
362 /**
363  * @brief Release ownership of the specified mutex object
364  * @param[in] mutex Pointer to the mutex object
365  **/
366 
368 {
369  //Release ownership of the mutex object
370  ReleaseMutex(mutex->handle);
371 }
372 
373 
374 /**
375  * @brief Retrieve system time
376  * @return Number of milliseconds elapsed since the system was last started
377  **/
378 
380 {
381  //Get current tick count
382  return GetTickCount();
383 }
384 
385 
386 /**
387  * @brief Allocate a memory block
388  * @param[in] size Bytes to allocate
389  * @return A pointer to the allocated memory block or NULL if
390  * there is insufficient memory available
391  **/
392 
393 void *osAllocMem(size_t size)
394 {
395  //Allocate a memory block
396  return malloc(size);
397 }
398 
399 
400 /**
401  * @brief Release a previously allocated memory block
402  * @param[in] p Previously allocated memory block to be freed
403  **/
404 
405 void osFreeMem(void *p)
406 {
407  //Free memory block
408  free(p);
409 }
void osReleaseSemaphore(OsSemaphore *semaphore)
Release the specified semaphore object.
uint16_t priority
Definition: dns_common.h:219
bool_t osCreateEvent(OsEvent *event)
Create an event object.
uint32_t systime_t
Definition: compiler_port.h:44
void osDeleteEvent(OsEvent *event)
Delete an event object.
void osSwitchTask(void)
Yield control to the next task.
char char_t
Definition: compiler_port.h:41
Debugging facilities.
uint8_t p
Definition: ndp.h:295
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
xSemaphoreHandle handle
void osResumeAllTasks(void)
Resume scheduler activity.
xSemaphoreHandle handle
Event object.
bool_t osCreateMutex(OsMutex *mutex)
Create a mutex object.
systime_t osGetSystemTime(void)
Retrieve system time.
#define TRUE
Definition: os_port.h:48
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
Task object.
signed int int_t
Definition: compiler_port.h:42
char_t name[]
bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Wait until the specified event is in the signaled state.
RTOS abstraction layer.
RTOS abstraction layer (Windows)
bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count)
Create a semaphore object.
void osSuspendAllTasks(void)
Suspend scheduler activity.
void(* OsTaskCode)(void *param)
Task routine.
void osDeleteTask(OsTask *task)
Delete a task.
void osDelayTask(systime_t delay)
Delay routine.
void osResetEvent(OsEvent *event)
Set the specified event object to the nonsignaled state.
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
void osDeleteMutex(OsMutex *mutex)
Delete a mutex object.
unsigned int uint_t
Definition: compiler_port.h:43
void osStartKernel(void)
Start kernel.
Mutex object.
OsTask * osCreateTask(const char_t *name, OsTaskCode taskCode, void *param, size_t stackSize, int_t priority)
Create a new task.
xSemaphoreHandle handle
void osDeleteSemaphore(OsSemaphore *semaphore)
Delete a semaphore object.
void osFreeMem(void *p)
Release a previously allocated memory block.
bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout)
Wait for the specified semaphore to be available.
void * osAllocMem(size_t size)
Allocate a memory block.
Semaphore object.
#define FALSE
Definition: os_port.h:44
int bool_t
Definition: compiler_port.h:47
void osInitKernel(void)
Kernel initialization.
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.