Statistics
| Branch: | Revision:

urtware / src / urt_node.c @ f86427e8

History | View | Annotate | Download (6.85 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_node_t* localNode = (urt_node_t*)arg;
52
  urt_osEventMask_t mask;
53
  urt_osEventFlags_t flag = 0;
54
  urtEventRegister(urtCoreGetEvtSource(), &(localNode->listener), mask, flag);
55
  if (localNode->setupcallback != NULL)
56
  {
57
    mask = localNode->setupcallback(localNode, localNode->setupparams);
58
    if (mask == urtCoreGetEventMask())
59
    {
60
        urtCoreStopNodes(URT_STATUS_NODE_INVALEVTMASK);
61
    }
62
  }
63
  else
64
  {
65
     mask = URT_EVENTMASK_ALL;
66
  }
67

    
68
  if (urtCoreGetStatus() == URT_STATUS_OK)
69
  {
70
    urtCoreSynchronizeNodes(localNode);
71
  }
72

    
73
  if (!urtThreadShouldTerminate())
74
  {
75
    urt_osEventMask_t temp = urtEventWait(mask, URT_EVENT_WAIT_ONE, URT_DELAY_INFINITE);
76
    if (temp == urtCoreGetEventMask())
77
    {
78
      localNode->loopcallback(localNode, mask, localNode->loopparams);
79
      #if (URT_CFG_PUBSUB_PROFILING || URT_CFG_RPC_PROFILING)
80
        localNode->loops++;
81
      #endif /* URT_CFG_PUBSUB_PROFILING || URT_CFG_RPC_PROFILING */
82
      if (mask == urtCoreGetEventMask())
83
      {
84
        urtCoreStopNodes(URT_STATUS_NODE_INVALEVTMASK);
85
      }
86
    }
87
  }
88

    
89
  if (localNode->shutdowncallback)
90
  {
91
    localNode->shutdowncallback(localNode, urtCoreGetStatus(), localNode->shutdownparams);
92
  }
93
  urtEventUnregister(urtCoreGetEvtSource(), &localNode->listener);
94

    
95
  urt_osThread_t* threadToTerminate = localNode->thread;
96
  while (threadToTerminate->children != NULL || threadToTerminate->sibling != NULL)
97
  {
98
    if (threadToTerminate->children != NULL)
99
      urtThreadTerminate(threadToTerminate->children, URT_THREAD_TERMINATE_REQUEST);
100
    if(threadToTerminate->sibling != NULL)
101
      urtThreadTerminate(threadToTerminate->sibling, URT_THREAD_TERMINATE_REQUEST);
102
  }
103
  urtThreadTerminate(threadToTerminate, URT_THREAD_TERMINATE_REQUEST);
104

    
105
  urt_osThread_t* threadToJoin = localNode->thread;
106
  while (threadToJoin->children != NULL || threadToJoin->sibling != NULL)
107
  {
108
    if (threadToJoin->children != NULL)
109
      urtThreadJoin(threadToJoin->children);
110
    if(threadToJoin->sibling != NULL)
111
      urtThreadJoin(threadToJoin->sibling);
112
  }
113
  urtThreadJoin(threadToJoin);
114

    
115
  return;
116
}
117

    
118
/******************************************************************************/
119
/* EXPORTED FUNCTIONS                                                         */
120
/******************************************************************************/
121

    
122
/**
123
 * @brief  Initalize a node.
124
 *
125
 * @param[in] node  The node to initialize. Must not be NULL.
126
 * @param[in] thread  The thread to intialize. Must be NULL.
127
 * @param[in] setupcallback  Callback function to be executed during setup.
128
 *                           May be NULL if no custom setup is required.
129
 * @param[in] setupparams  Parameters for the setup callback function.
130
 *                         Must be NULL if no setup callback is specified.
131
 *                         May be NULL if the specified setup callback does not expect parameters.
132
 * @param[in] loopcallback  Callback function to be executed in a loop.
133
 * @param[in] loopparams  Parameters for the loop callback function.
134
 *                        May be NULL if the specified loop callback does not expect parameters.
135
 * @param[in] shutdowncallback  Callback function to be executed during shutdown.
136
 *                              May be NULL if no custom shutdown is required.
137
 * @param[in] shutdownparams  Parameters for the loop callback function.
138
 *                            Must be NULL if no shutdown callback is specified.
139
 *                            May be NULL if the specified shutdown callback does not expect parameters.
140
 */
141
void urtNodeInit(urt_node_t* node, urt_osThread_t* thread, urt_osThreadPrio_t prio, urt_nodeSetupCallback_t setupcallback,
142
                 void* setupparams, urt_nodeLoopCallback_t loopcallback, void* loopparams,
143
                 urt_nodeShutdownCallback_t shutdowncallback, void* shutdownparams)
144
{
145
  urtDebugAssert(node != NULL);
146
  urtDebugAssert(thread == NULL);
147
  if (setupcallback == NULL)
148
    urtDebugAssert(setupparams == NULL);
149

    
150
  node->next = NULL;
151
  node->thread = urtThreadInit(thread, sizeof(thread), prio, _main, node);
152
  node->setupcallback = setupcallback;
153
  node->setupparams = setupparams;
154
  node->loopcallback = loopcallback;
155
  node->loopparams = loopparams;
156
  node->shutdowncallback = shutdowncallback;
157
  node->shutdownparams = shutdownparams;
158
  node->stage = 0;
159
  urtEventListenerInit(node->listener);
160
  #if (URT_CFG_PUBSUB_PROFILING || URT_CFG_RPC_PROFILING)
161
    node->loops = 0;
162
  #endif /* URT_CFG_PUBSUB_PROFILING || URT_CFG_RPC_PROFILING */
163
  urt_osMutex_t* mutexTemp = urtCoreGetMutex();
164
  urtMutexLock(mutexTemp);
165
    node->next = urtCoreGetNodes();
166
    urt_node_t* nodeCore = urtCoreGetNodes();
167
    nodeCore->next = node;
168
  urtMutexUnlock(mutexTemp);
169
  return;
170
}