Statistics
| Branch: | Tag: | Revision:

amiro-os / kernel / patches / introduce-thread-hierarchy.patch @ 8547080b

History | View | Annotate | Download (51.975 KB)

1 0a89baf2 Thomas Schöpping
diff --git a/os/rt/include/chschd.h b/os/rt/include/chschd.h
2
--- a/os/rt/include/chschd.h
3
+++ b/os/rt/include/chschd.h
4 732a4657 Thomas Schöpping
@@ -176,6 +176,23 @@ struct ch_thread {
5
    * @brief   References to this thread.
6 0a89baf2 Thomas Schöpping
    */
7
   trefs_t               refs;
8 732a4657 Thomas Schöpping
+#endif
9 0a89baf2 Thomas Schöpping
+#if (CH_CFG_USE_THREADHIERARCHY == TRUE) || defined(__DOXYGEN__)
10
+  /**
11
+   * @brief   Pointer to the parent thread.
12
+   * @note    NULL only for the main thread.
13
+   */
14
+  thread_t              *parent;
15
+  /**
16
+   * @brief   Pointer to the first child thread.
17
+   * @note    NULL if there are no child threads.
18
+   */
19
+  thread_t              *children;
20
+  /**
21
+   * @brief   Pointer to the next sibling thread.
22
+   * @brief   NULL if there are no more child threads.
23
+   */
24
+  thread_t              *sibling;
25 732a4657 Thomas Schöpping
 #endif
26 0a89baf2 Thomas Schöpping
   /**
27
    * @brief   Number of ticks remaining to this thread.
28
diff --git a/os/rt/include/chthreads.h b/os/rt/include/chthreads.h
29
--- a/os/rt/include/chthreads.h
30
+++ b/os/rt/include/chthreads.h
31 732a4657 Thomas Schöpping
@@ -79,6 +79,12 @@ typedef struct {
32 0a89baf2 Thomas Schöpping
    * @brief   Thread argument.
33
    */
34
   void              *arg;
35 732a4657 Thomas Schöpping
+#if (CH_CFG_USE_THREADHIERARCHY) == TRUE || defined(__DOXYGEN__)
36 0a89baf2 Thomas Schöpping
+  /**
37
+   * @brief   Pointer to the parent thread.
38
+   */
39
+  thread_t          *parent;
40 732a4657 Thomas Schöpping
+#endif
41 0a89baf2 Thomas Schöpping
 } thread_descriptor_t;
42
 
43
 /*===========================================================================*/
44 732a4657 Thomas Schöpping
@@ -220,7 +226,11 @@ typedef struct {
45 0a89baf2 Thomas Schöpping
 #ifdef __cplusplus
46
 extern "C" {
47
 #endif
48 732a4657 Thomas Schöpping
+#if CH_CFG_USE_THREADHIERARCHY == TRUE
49 0a89baf2 Thomas Schöpping
+   thread_t *_thread_init(thread_t *tp, thread_t* parent, const char *name, tprio_t prio);
50 732a4657 Thomas Schöpping
+#else
51
    thread_t *_thread_init(thread_t *tp, const char *name, tprio_t prio);
52
+#endif
53 0a89baf2 Thomas Schöpping
 #if CH_DBG_FILL_THREADS == TRUE
54
   void _thread_memfill(uint8_t *startp, uint8_t *endp, uint8_t v);
55
 #endif
56 732a4657 Thomas Schöpping
@@ -228,8 +238,13 @@ extern "C" {
57 0a89baf2 Thomas Schöpping
   thread_t *chThdCreateSuspended(const thread_descriptor_t *tdp);
58
   thread_t *chThdCreateI(const thread_descriptor_t *tdp);
59
   thread_t *chThdCreate(const thread_descriptor_t *tdp);
60
+#if CH_CFG_USE_THREADHIERARCHY == TRUE
61
+  thread_t *chThdCreateStatic(void *wsp, size_t size,
62
+                              tprio_t prio, tfunc_t pf, void *arg, thread_t *parent);
63
+#else
64
   thread_t *chThdCreateStatic(void *wsp, size_t size,
65
                               tprio_t prio, tfunc_t pf, void *arg);
66
+#endif
67
   thread_t *chThdStart(thread_t *tp);
68
 #if CH_CFG_USE_REGISTRY == TRUE
69
   thread_t *chThdAddRef(thread_t *tp);
70
diff --git a/os/rt/src/chsys.c b/os/rt/src/chsys.c
71
--- a/os/rt/src/chsys.c
72
+++ b/os/rt/src/chsys.c
73 732a4657 Thomas Schöpping
@@ -126,14 +126,26 @@ void chSysInit(void) {
74 0a89baf2 Thomas Schöpping
 #if CH_CFG_NO_IDLE_THREAD == FALSE
75
   /* Now this instructions flow becomes the main thread.*/
76
 #if CH_CFG_USE_REGISTRY == TRUE
77 732a4657 Thomas Schöpping
+#if CH_CFG_USE_THREADHIERARCHY == TRUE
78 0a89baf2 Thomas Schöpping
+  currp = _thread_init(&ch.mainthread, NULL, (const char *)&ch_debug, NORMALPRIO);
79 732a4657 Thomas Schöpping
+#else
80
   currp = _thread_init(&ch.mainthread, (const char *)&ch_debug, NORMALPRIO);
81
+#endif
82
+#else
83
+#if CH_CFG_USE_THREADHIERARCHY == TRUE
84 0a89baf2 Thomas Schöpping
+  currp = _thread_init(&ch.mainthread, NULL, "main", NORMALPRIO);
85 732a4657 Thomas Schöpping
 #else
86
   currp = _thread_init(&ch.mainthread, "main", NORMALPRIO);
87 0a89baf2 Thomas Schöpping
 #endif
88 732a4657 Thomas Schöpping
+#endif
89 0a89baf2 Thomas Schöpping
 #else
90
   /* Now this instructions flow becomes the idle thread.*/
91 732a4657 Thomas Schöpping
+#if CH_CFG_USE_THREADHIERARCHY == TRUE
92 0a89baf2 Thomas Schöpping
+  currp = _thread_init(&ch.mainthread, NULL, "idle", IDLEPRIO);
93 732a4657 Thomas Schöpping
+#else
94
   currp = _thread_init(&ch.mainthread, "idle", IDLEPRIO);
95 0a89baf2 Thomas Schöpping
 #endif
96 732a4657 Thomas Schöpping
+#endif
97 0a89baf2 Thomas Schöpping
 
98
 #if CH_DBG_ENABLE_STACK_CHECK == TRUE
99 732a4657 Thomas Schöpping
   {
100
@@ -172,6 +184,9 @@ void chSysInit(void) {
101 0a89baf2 Thomas Schöpping
       THD_WORKING_AREA_END(ch_idle_thread_wa),
102
       IDLEPRIO,
103
       _idle_thread,
104 732a4657 Thomas Schöpping
+#if CH_CFG_USE_THREADHIERARCHY == TRUE
105 0a89baf2 Thomas Schöpping
+      NULL,
106 732a4657 Thomas Schöpping
+#endif
107 0a89baf2 Thomas Schöpping
       NULL
108
     };
109
 
110
diff --git a/os/rt/src/chthreads.c b/os/rt/src/chthreads.c
111
--- a/os/rt/src/chthreads.c
112
+++ b/os/rt/src/chthreads.c
113 732a4657 Thomas Schöpping
@@ -70,22 +70,67 @@
114 0a89baf2 Thomas Schöpping
 /* Module local functions.                                                   */
115
 /*===========================================================================*/
116
 
117
+#if CH_CFG_USE_THREADHIERARCHY == TRUE || defined(__DOXYGEN__)
118
+/**
119
+ * @brief   Insert a thread to the list of children of another thread.
120
+ * @details If @p CH_CFG_THREADHIERARCHY_ORDERED is @p TRUE, children are ordered by their priorities (high to low).
121
+ *          Children with identical priority are ordered by 'age' (youngest first).
122
+ *
123
+ * @param[in] parent  Pointer to the parent thread (must not be NULL).
124
+ * @param[in] child   Pointer to the child thread (must not be NULL).
125
+ */
126
+inline void _thread_addChild(thread_t *parent, thread_t *child) {
127
+#if CH_CFG_THREADHIERARCHY_ORDERED == TRUE
128
+  thread_t *sibling = parent->children;
129
+  child->parent = parent;
130
+#if CH_CFG_USE_MUTEXES == TRUE
131
+  if (sibling == NULL || sibling->realprio <= child->realprio) {
132
+#else
133
+  if (sibling == NULL || sibling->prio <= child->prio) {
134
+#endif
135
+    child->sibling = sibling;
136
+    parent->children = child;
137
+  } else {
138
+#if CH_CFG_USE_MUTEXES == TRUE
139
+    while (sibling->sibling != NULL && sibling->sibling->realprio > child->realprio) {
140
+#else
141
+    while (sibling->sibling != NULL && sibling->sibling->prio > child->prio) {
142
+#endif
143
+      sibling = sibling->sibling;
144
+    }
145
+    child->sibling = sibling->sibling;
146
+    sibling->sibling = child;
147
+  }
148
+#else
149
+  child->parent = parent;
150
+  child->sibling = parent->children;
151
+  parent->children = child;
152
+#endif
153
+  return;
154
+}
155
+#endif
156
+
157
 /*===========================================================================*/
158
 /* Module exported functions.                                                */
159
 /*===========================================================================*/
160 732a4657 Thomas Schöpping
 
161
+#if CH_CFG_USE_THREADHIERARCHY == TRUE || defined(__DOXYGEN__)
162
 /**
163
  * @brief   Initializes a thread structure.
164 0a89baf2 Thomas Schöpping
  * @note    This is an internal functions, do not use it in application code.
165
  *
166
  * @param[in] tp        pointer to the thread
167
+ * @param[in] parent    pointer to the parent thread
168
  * @param[in] name      thread name
169
  * @param[in] prio      the priority level for the new thread
170
  * @return              The same thread pointer passed as parameter.
171
  *
172
  * @notapi
173
  */
174
+thread_t *_thread_init(thread_t *tp, thread_t *parent,  const char *name, tprio_t prio) {
175 732a4657 Thomas Schöpping
+#else
176
 thread_t *_thread_init(thread_t *tp, const char *name, tprio_t prio) {
177
+#endif
178 0a89baf2 Thomas Schöpping
 
179
   tp->prio      = prio;
180
   tp->state     = CH_STATE_WTSTART;
181 732a4657 Thomas Schöpping
@@ -110,6 +155,17 @@ thread_t *_thread_init(thread_t *tp, const char *name, tprio_t prio) {
182 0a89baf2 Thomas Schöpping
 #else
183
   (void)name;
184
 #endif
185
+#if CH_CFG_USE_THREADHIERARCHY == TRUE
186
+  if (parent != NULL) {
187
+    _thread_addChild(parent, tp);
188
+  } else {
189
+    tp->parent = parent;
190
+    tp->sibling = NULL;
191
+  }
192
+  tp->children = NULL;
193
+#else