urtware / src / urt_node.c @ 37cd5dc2
History | View | Annotate | Download (7.722 KB)
| 1 |
/*
|
|---|---|
| 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 |
#include <urtware.h> |
| 23 |
|
| 24 |
/******************************************************************************/
|
| 25 |
/* LOCAL DEFINITIONS */
|
| 26 |
/******************************************************************************/
|
| 27 |
|
| 28 |
/******************************************************************************/
|
| 29 |
/* EXPORTED VARIABLES */
|
| 30 |
/******************************************************************************/
|
| 31 |
|
| 32 |
/******************************************************************************/
|
| 33 |
/* LOCAL TYPES */
|
| 34 |
/******************************************************************************/
|
| 35 |
|
| 36 |
/******************************************************************************/
|
| 37 |
/* LOCAL VARIABLES */
|
| 38 |
/******************************************************************************/
|
| 39 |
|
| 40 |
/******************************************************************************/
|
| 41 |
/* LOCAL FUNCTIONS */
|
| 42 |
/******************************************************************************/
|
| 43 |
|
| 44 |
/**
|
| 45 |
* @brief Main function of a node.
|
| 46 |
*
|
| 47 |
* @param[in] arg Optional Argument to the thread main function.
|
| 48 |
*/
|
| 49 |
void _main(void* arg) |
| 50 |
{
|
| 51 |
urt_osEventMask_t mask; |
| 52 |
urt_osEventFlags_t flag = 0;
|
| 53 |
urtEventRegister(urtCoreGetEvtSource(), &(((urt_node_t*)arg)->listener), mask, flag); |
| 54 |
if (((urt_node_t*)arg)->setupcallback != NULL) |
| 55 |
{
|
| 56 |
mask = ((urt_node_t*)arg)->setupcallback(((urt_node_t*)arg), ((urt_node_t*)arg)->setupparams); |
| 57 |
if (mask == urtCoreGetEventMask())
|
| 58 |
{
|
| 59 |
urtCoreStopNodes(URT_STATUS_NODE_INVALEVTMASK); |
| 60 |
} |
| 61 |
} |
| 62 |
else
|
| 63 |
{
|
| 64 |
mask = URT_EVENTMASK_ALL; |
| 65 |
} |
| 66 |
|
| 67 |
if (urtCoreGetStatus() == URT_STATUS_OK)
|
| 68 |
{
|
| 69 |
urtCoreSynchronizeNodes(((urt_node_t*)arg)); |
| 70 |
} |
| 71 |
|
| 72 |
while (urtThreadShouldTerminate())
|
| 73 |
{
|
| 74 |
urt_osEventMask_t temp = urtEventWait(mask, URT_EVENT_WAIT_ONE, URT_DELAY_INFINITE); |
| 75 |
if (temp == urtCoreGetEventMask())
|
| 76 |
{
|
| 77 |
((urt_node_t*)arg)->loopcallback(((urt_node_t*)arg), mask, ((urt_node_t*)arg)->loopparams); |
| 78 |
#if (URT_CFG_PUBSUB_PROFILING || URT_CFG_RPC_PROFILING)
|
| 79 |
((urt_node_t*)arg)->loops++; |
| 80 |
#endif /* URT_CFG_PUBSUB_PROFILING || URT_CFG_RPC_PROFILING */ |
| 81 |
if (mask == urtCoreGetEventMask())
|
| 82 |
{
|
| 83 |
urtCoreStopNodes(URT_STATUS_NODE_INVALEVTMASK); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if (((urt_node_t*)arg)->shutdowncallback != NULL) |
| 89 |
{
|
| 90 |
((urt_node_t*)arg)->shutdowncallback(((urt_node_t*)arg), urtCoreGetStatus(), ((urt_node_t*)arg)->shutdownparams); |
| 91 |
} |
| 92 |
urtEventUnregister(urtCoreGetEvtSource(), &((urt_node_t*)arg)->listener); |
| 93 |
|
| 94 |
|
| 95 |
//Terminate all children and siblings
|
| 96 |
urt_osThread_t* threadToTerminate = urtThreadGetSelf()->children; |
| 97 |
while (threadToTerminate != urtThreadGetSelf() && threadToTerminate != NULL) |
| 98 |
{
|
| 99 |
urtThreadTerminate(threadToTerminate, URT_THREAD_TERMINATE_REQUEST); |
| 100 |
if (threadToTerminate->children != NULL) |
| 101 |
{
|
| 102 |
threadToTerminate = threadToTerminate->children; |
| 103 |
} |
| 104 |
else
|
| 105 |
{
|
| 106 |
if (threadToTerminate->sibling != NULL) |
| 107 |
{
|
| 108 |
threadToTerminate = threadToTerminate->sibling; |
| 109 |
} |
| 110 |
else
|
| 111 |
{
|
| 112 |
threadToTerminate = threadToTerminate->parent; |
| 113 |
while (threadToTerminate->sibling == NULL && threadToTerminate != urtThreadGetSelf()) |
| 114 |
{
|
| 115 |
threadToTerminate = threadToTerminate->parent; |
| 116 |
} |
| 117 |
if (threadToTerminate != urtThreadGetSelf())
|
| 118 |
{
|
| 119 |
threadToTerminate = threadToTerminate->sibling; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
urt_osThread_t* threadToJoin = urtThreadGetSelf()->children; |
| 126 |
while (threadToJoin != urtThreadGetSelf() && threadToJoin != NULL) |
| 127 |
{
|
| 128 |
urtThreadJoin(threadToJoin); |
| 129 |
if (threadToJoin->children != NULL) |
| 130 |
{
|
| 131 |
threadToJoin = threadToJoin->children; |
| 132 |
} |
| 133 |
else
|
| 134 |
{
|
| 135 |
if (threadToJoin->sibling != NULL) |
| 136 |
{
|
| 137 |
threadToJoin = threadToJoin->sibling; |
| 138 |
} |
| 139 |
else
|
| 140 |
{
|
| 141 |
threadToJoin = threadToJoin->parent; |
| 142 |
while (threadToJoin->sibling == NULL && threadToJoin != urtThreadGetSelf()) |
| 143 |
{
|
| 144 |
threadToJoin = threadToJoin->parent; |
| 145 |
} |
| 146 |
if (threadToJoin != urtThreadGetSelf())
|
| 147 |
{
|
| 148 |
threadToJoin = threadToJoin->sibling; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
urtThreadExit(); |
| 155 |
return;
|
| 156 |
} |
| 157 |
|
| 158 |
/******************************************************************************/
|
| 159 |
/* EXPORTED FUNCTIONS */
|
| 160 |
/******************************************************************************/
|
| 161 |
|
| 162 |
/**
|
| 163 |
* @brief Initalize a node.
|
| 164 |
*
|
| 165 |
* @param[in] node The node to initialize. Must not be NULL.
|
| 166 |
* @param[in] thread The thread to intialize.
|
| 167 |
* @param[in] setupcallback Callback function to be executed during setup.
|
| 168 |
* May be NULL if no custom setup is required.
|
| 169 |
* @param[in] setupparams Parameters for the setup callback function.
|
| 170 |
* Must be NULL if no setup callback is specified.
|
| 171 |
* May be NULL if the specified setup callback does not expect parameters.
|
| 172 |
* @param[in] loopcallback Callback function to be executed in a loop.
|
| 173 |
* @param[in] loopparams Parameters for the loop callback function.
|
| 174 |
* May be NULL if the specified loop callback does not expect parameters.
|
| 175 |
* @param[in] shutdowncallback Callback function to be executed during shutdown.
|
| 176 |
* May be NULL if no custom shutdown is required.
|
| 177 |
* @param[in] shutdownparams Parameters for the loop callback function.
|
| 178 |
* Must be NULL if no shutdown callback is specified.
|
| 179 |
* May be NULL if the specified shutdown callback does not expect parameters.
|
| 180 |
*/
|
| 181 |
void urtNodeInit(urt_node_t* node, urt_osThread_t* thread, urt_osThreadPrio_t prio, urt_nodeSetupCallback_t setupcallback,
|
| 182 |
void* setupparams, urt_nodeLoopCallback_t loopcallback, void* loopparams, |
| 183 |
urt_nodeShutdownCallback_t shutdowncallback, void* shutdownparams)
|
| 184 |
{
|
| 185 |
urtDebugAssert(node != NULL);
|
| 186 |
if (setupcallback == NULL) |
| 187 |
urtDebugAssert(setupparams == NULL);
|
| 188 |
|
| 189 |
node->next = NULL;
|
| 190 |
node->thread = urtThreadInit((void*)thread, sizeof(thread), prio, (urt_osThreadFunction_t) _main, (void*)node); |
| 191 |
node->setupcallback = setupcallback; |
| 192 |
node->setupparams = setupparams; |
| 193 |
node->loopcallback = loopcallback; |
| 194 |
node->loopparams = loopparams; |
| 195 |
node->shutdowncallback = shutdowncallback; |
| 196 |
node->shutdownparams = shutdownparams; |
| 197 |
node->stage = 0;
|
| 198 |
urtEventListenerInit(node->listener); |
| 199 |
#if (URT_CFG_PUBSUB_PROFILING || URT_CFG_RPC_PROFILING)
|
| 200 |
node->loops = 0;
|
| 201 |
#endif /* URT_CFG_PUBSUB_PROFILING || URT_CFG_RPC_PROFILING */ |
| 202 |
urt_osMutex_t* mutexTemp = urtCoreGetMutex(); |
| 203 |
urtMutexLock(mutexTemp); |
| 204 |
node->next = urtCoreGetNodes(); |
| 205 |
urtCoreAddNode(node); |
| 206 |
urtMutexUnlock(mutexTemp); |
| 207 |
return;
|
| 208 |
} |