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