Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
SimplelinkPthread.cxx
1
35#include <fcntl.h>
36#include <unistd.h>
37#include <pthread.h>
38
39#include "nmranet_config.h"
40
41#include "os/os.h"
42#include "os/os_private.h"
43
44extern "C"
45{
46#if defined (__FreeRTOS__)
47typedef struct
48{
49 TaskHandle_t freeRTOSTask;
50} PthreadObj;
51
52//
53// ::os_thread_start_entry_hook()
54//
55void os_thread_start_entry_hook(void)
56{
57 // make sure that pthread_self() struct did not rot by asserting
58 // against the FreeRTOS task handle
59 PthreadObj *thread_handle = static_cast<PthreadObj*>(pthread_self());
60 HASSERT(thread_handle->freeRTOSTask == xTaskGetCurrentTaskHandle());
61}
62
63//
64// ::os_thread_start_exit_hook()
65//
66void os_thread_start_exit_hook(void *context)
67{
68 pthread_exit(context);
69}
70#else
71#error Unsupported OS
72#endif // __FreeRTOS__
73
78static void *os_thread_start_posix(void *arg)
79{
80 os_thread_start(arg);
81
82 return NULL;
83}
84
85//
86// ::os_thread_create_helper()
87//
88int os_thread_create_helper(os_thread_t *thread, const char *name, int priority,
89 size_t stack_size, void *priv)
90{
91 HASSERT(thread);
92 pthread_attr_t attr;
93
94 int result = pthread_attr_init(&attr);
95 if (result != 0)
96 {
97 return result;
98 }
99 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
100 if (result != 0)
101 {
102 return result;
103 }
104
105 result = pthread_attr_setstacksize(&attr, stack_size);
106 if (result != 0)
107 {
108 return result;
109 }
110
111 struct sched_param sched_param;
112 sched_param.sched_priority = priority;
113 result = pthread_attr_setschedparam(&attr, &sched_param);
114 if (result != 0)
115 {
116 return result;
117 }
118
119 pthread_t pthread;
120 result = pthread_create(&pthread, &attr, os_thread_start_posix, priv);
121 if (result != 0)
122 {
123 return result;
124 }
125
126#if defined (__FreeRTOS__)
127 // Hack: Though not pretty, it allows us to get the FreeRTOS
128 // task handle from the pthread handle.
129 *thread = static_cast<PthreadObj*>(pthread)->freeRTOSTask;
130
131 // Hack: The pthread API doesn't support setting the FreeRTOS task name
132 // directly. Call pcTaskGetName() and override the const pointer.
133 char *task_name = static_cast<char*>(pcTaskGetName(*thread));
134 strncpy(task_name, name, configMAX_TASK_NAME_LEN - 1);
135 task_name[configMAX_TASK_NAME_LEN - 1] = '\0';
136#else
137#error Unsupported OS
138#endif
139
140 return result;
141}
142
143} // extern "C"
144
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138