urtware / src / urt_node.c @ f6e5368e
History | View | Annotate | Download (6.884 KB)
1 | 1fb06240 | skenneweg | /*
|
---|---|---|---|
2 | µRtWare is a lightweight publish/subscribe middleware for real-time
|
||
3 | applications. It was developed as part of the software habitat for the
|
||
4 | Autonomous Mini Robot [1] (AMiRo) but can be used for other purposes as well.
|
||
5 | |||
6 | Copyright (C) 2018..2020 Thomas Schöpping et al.
|
||
7 | |||
8 | This program is free software: you can redistribute it and/or modify
|
||
9 | it under the terms of the GNU General Public License as published by
|
||
10 | the Free Software Foundation, either version 3 of the License, or
|
||
11 | (at your option) any later version.
|
||
12 | |||
13 | This program is distributed in the hope that it will be useful,
|
||
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
16 | GNU General Public License for more details.
|
||
17 | |||
18 | You should have received a copy of the GNU General Public License
|
||
19 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
20 | */
|
||
21 | |||
22 | 7d9678db | skenneweg | #include <urtware.h> |
23 | |||
24 | 1fb06240 | skenneweg | /******************************************************************************/
|
25 | /* LOCAL DEFINITIONS */
|
||
26 | /******************************************************************************/
|
||
27 | |||
28 | /******************************************************************************/
|
||
29 | /* EXPORTED VARIABLES */
|
||
30 | /******************************************************************************/
|
||
31 | |||
32 | /******************************************************************************/
|
||
33 | /* LOCAL TYPES */
|
||
34 | /******************************************************************************/
|
||
35 | |||
36 | /******************************************************************************/
|
||
37 | /* LOCAL VARIABLES */
|
||
38 | /******************************************************************************/
|
||
39 | |||
40 | 792f6c74 | skenneweg | urt_node_t localNode; |
41 | 22ccef0f | skenneweg | static URT_THREAD_MEMORY(_node_thread, AMIROOS_CFG_SHELL_STACKSIZE); //TODO: not here |
42 | 17d978fe | skenneweg | |
43 | 1fb06240 | skenneweg | /******************************************************************************/
|
44 | /* LOCAL FUNCTIONS */
|
||
45 | /******************************************************************************/
|
||
46 | |||
47 | 17d978fe | skenneweg | /**
|
48 | * @brief Main function of a node.
|
||
49 | *
|
||
50 | * @param[in] arg Optional Argument to the thread main function.
|
||
51 | */
|
||
52 | void _main(void* arg) |
||
53 | { |
||
54 | 1bfc6b25 | skenneweg | urt_osEventMask_t mask; |
55 | cd0cba58 | skenneweg | urt_osEventFlags_t flag = 0;
|
56 | 408a606c | skenneweg | urtEventRegister(urtCoreGetEvtSource(), &localNode.listener, mask, flag); |
57 | 1bfc6b25 | skenneweg | if (localNode.setupcallback != NULL) |
58 | { |
||
59 | mask = localNode.setupcallback(&localNode, arg); |
||
60 | cd0cba58 | skenneweg | if (mask == urtCoreGetEventMask())
|
61 | 17d978fe | skenneweg | { |
62 | 1bfc6b25 | skenneweg | urtCoreStopNodes(URT_STATUS_NODE_INVALEVTMASK); |
63 | 17d978fe | skenneweg | } |
64 | 1bfc6b25 | skenneweg | } |
65 | else
|
||
66 | { |
||
67 | 792f6c74 | skenneweg | mask = ALL_EVENTS; |
68 | 1bfc6b25 | skenneweg | } |
69 | |||
70 | 408a606c | skenneweg | if (urtCoreGetStatus() == URT_STATUS_OK)
|
71 | 1bfc6b25 | skenneweg | { |
72 | urtCoreSynchronizeNodes(&localNode); |
||
73 | } |
||
74 | |||
75 | 792f6c74 | skenneweg | if (!urtThreadShouldTerminate())
|
76 | 1bfc6b25 | skenneweg | { |
77 | urt_osEventMask_t temp = urtEventWait(mask, URT_EVENT_WAIT_ONE, URT_DELAY_INFINITE); |
||
78 | cd0cba58 | skenneweg | if (temp == urtCoreGetEventMask())
|
79 | 17d978fe | skenneweg | { |
80 | 1bfc6b25 | skenneweg | localNode.loopcallback(&localNode, mask, arg); |
81 | #if (URT_CFG_PUBSUB_PROFILING || URT_CFG_RPC_PROFILING)
|
||
82 | localNode.loops++; |
||
83 | #endif /* URT_CFG_PUBSUB_PROFILING || URT_CFG_RPC_PROFILING */ |
||
84 | cd0cba58 | skenneweg | if (mask == urtCoreGetEventMask())
|
85 | 1bfc6b25 | skenneweg | { |
86 | urtCoreStopNodes(URT_STATUS_NODE_INVALEVTMASK); |
||
87 | } |
||
88 | 17d978fe | skenneweg | } |
89 | 1bfc6b25 | skenneweg | } |
90 | |||
91 | if (localNode.shutdowncallback)
|
||
92 | { |
||
93 | localNode.shutdowncallback(&localNode, urtCoreGetStatus(), arg); |
||
94 | } |
||
95 | 408a606c | skenneweg | urtEventUnregister(urtCoreGetEvtSource(), &localNode.listener); |
96 | 792f6c74 | skenneweg | |
97 | 1bfc6b25 | skenneweg | urt_osThread_t* threadToTerminate = localNode.thread; |
98 | 792f6c74 | skenneweg | while (threadToTerminate->children != NULL || threadToTerminate->sibling != NULL) |
99 | 1bfc6b25 | skenneweg | { |
100 | 792f6c74 | skenneweg | if (threadToTerminate->children != NULL) |
101 | urtThreadTerminate(threadToTerminate->children, URT_THREAD_TERMINATE_REQUEST); |
||
102 | if(threadToTerminate->sibling != NULL) |
||
103 | urtThreadTerminate(threadToTerminate->sibling, URT_THREAD_TERMINATE_REQUEST); |
||
104 | 1bfc6b25 | skenneweg | } |
105 | 792f6c74 | skenneweg | urtThreadTerminate(&threadToTerminate, URT_THREAD_TERMINATE_REQUEST); |
106 | |||
107 | 22ccef0f | skenneweg | urt_osThread_t* threadToJoin = localNode.thread; |
108 | while (threadToJoin->children != NULL || threadToJoin->sibling != NULL) |
||
109 | { |
||
110 | if (threadToJoin->children != NULL) |
||
111 | urtThreadJoin(threadToJoin->children); |
||
112 | if(threadToJoin->sibling != NULL) |
||
113 | urtThreadJoin(threadToJoin->sibling); |
||
114 | } |
||
115 | urtThreadJoin(&threadToJoin); |
||
116 | |||
117 | 1bfc6b25 | skenneweg | return;
|
118 | 17d978fe | skenneweg | } |
119 | |||
120 | 1fb06240 | skenneweg | /******************************************************************************/
|
121 | /* EXPORTED FUNCTIONS */
|
||
122 | /******************************************************************************/
|
||
123 | 7d9678db | skenneweg | |
124 | /**
|
||
125 | 17d978fe | skenneweg | * @brief Initalize a node.
|
126 | 7d9678db | skenneweg | *
|
127 | 5198dfae | skenneweg | * @param[in] node The node to initialize. Must not be NULL.
|
128 | 67844205 | skenneweg | * @param[in] thread The thread to intialize. Must be NULL.
|
129 | 5198dfae | skenneweg | * @param[in] setupcallback Callback function to be executed during setup.
|
130 | * May be NULL if no custom setup is required.
|
||
131 | * @param[in] setupparams Parameters for the setup callback function.
|
||
132 | 1f7ffcff | skenneweg | * Must be NULL if no setup callback is specified.
|
133 | * May be NULL if the specified setup callback does not expect parameters.
|
||
134 | 5198dfae | skenneweg | * @param[in] loopcallback Callback function to be executed in a loop.
|
135 | * @param[in] loopparams Parameters for the loop callback function.
|
||
136 | * May be NULL if the specified loop callback does not expect parameters.
|
||
137 | * @param[in] shutdowncallback Callback function to be executed during shutdown.
|
||
138 | * May be NULL if no custom shutdown is required.
|
||
139 | * @param[in] shutdownparams Parameters for the loop callback function.
|
||
140 | * Must be NULL if no shutdown callback is specified.
|
||
141 | * May be NULL if the specified shutdown callback does not expect parameters.
|
||
142 | 7d9678db | skenneweg | */
|
143 | 22ccef0f | skenneweg | void urtNodeInit(urt_node_t* node, urt_osThread_t* thread, urt_osThreadPrio_t prio, urt_nodeSetupCallback_t* setupcallback,
|
144 | 7d9678db | skenneweg | void* setupparams, urt_nodeLoopCallback_t* loopcallback, void* loopparams, |
145 | 1f7ffcff | skenneweg | urt_nodeShutdownCallback_t* shutdowncallback, void* shutdownparams)
|
146 | { |
||
147 | 17d978fe | skenneweg | urtDebugAssert(node != NULL);
|
148 | 67844205 | skenneweg | urtDebugAssert(thread == NULL);
|
149 | 17d978fe | skenneweg | if (setupcallback == NULL) |
150 | urtDebugAssert(setupparams == NULL);
|
||
151 | |||
152 | 64fde4ba | skenneweg | node->next = NULL;
|
153 | 22ccef0f | skenneweg | node->thread = urtThreadInit(_node_thread, sizeof(_node_thread), prio, _main, setupparams);
|
154 | 64fde4ba | skenneweg | node->setupcallback = setupcallback; |
155 | node->setupparams = setupparams; |
||
156 | 2c811df1 | skenneweg | node->loopcallback = loopcallback; |
157 | 64fde4ba | skenneweg | node->loopparams = loopparams; |
158 | 2c811df1 | skenneweg | node->shutdowncallback = shutdowncallback; |
159 | node->shutdownparams = shutdownparams; |
||
160 | 64fde4ba | skenneweg | node->stage = 0;
|
161 | 2c811df1 | skenneweg | urtEventListenerInit(node->listener); |
162 | 1f7ffcff | skenneweg | #if (URT_CFG_PUBSUB_PROFILING || URT_CFG_RPC_PROFILING)
|
163 | 64fde4ba | skenneweg | node->loops = 0;
|
164 | 1f7ffcff | skenneweg | #endif /* URT_CFG_PUBSUB_PROFILING || URT_CFG_RPC_PROFILING */ |
165 | 792f6c74 | skenneweg | urt_osMutex_t* mutexTemp = urtCoreGetMutex(); |
166 | urtMutexLock(mutexTemp); |
||
167 | 408a606c | skenneweg | node->next = urtCoreGetNodes(); |
168 | urt_node_t* nodeCore = urtCoreGetNodes(); |
||
169 | nodeCore = node; |
||
170 | 792f6c74 | skenneweg | urtMutexUnlock(mutexTemp); |
171 | 17d978fe | skenneweg | localNode = *node; |
172 | 1f7ffcff | skenneweg | return;
|
173 | } |