aboutsummaryrefslogtreecommitdiffstats
path: root/test/rt/testdyn.c
blob: dfbdc40b0ad31e7959404ac6f51f0883d9a5b3e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
    ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#include "ch.h"
#include "test.h"

/**
 * @page test_dynamic Dynamic APIs test
 *
 * File: @ref testdyn.c
 *
 * <h2>Description</h2>
 * This module implements the test sequence for the dynamic thread creation
 * APIs.
 *
 * <h2>Objective</h2>
 * Objective of the test module is to cover 100% of the dynamic APIs code.
 *
 * <h2>Preconditions</h2>
 * The module requires the following kernel options:
 * - @p CH_CFG_USE_DYNAMIC
 * - @p CH_CFG_USE_HEAP
 * - @p CH_CFG_USE_MEMPOOLS
 * .
 * In case some of the required options are not enabled then some or all tests
 * may be skipped.
 *
 * <h2>Test Cases</h2>
 * - @subpage test_dynamic_001
 * - @subpage test_dynamic_002
 * - @subpage test_dynamic_003
 * .
 * @file testdyn.c
 * @brief Dynamic thread APIs test source file
 * @file testdyn.h
 * @brief Dynamic thread APIs test header file
 */

#if CH_CFG_USE_DYNAMIC || defined(__DOXYGEN__)
#if CH_CFG_USE_HEAP || defined(__DOXYGEN__)
static memory_heap_t heap1;
#endif
#if CH_CFG_USE_MEMPOOLS || defined(__DOXYGEN__)
static memory_pool_t mp1;
#endif

/**
 * @page test_dynamic_001 Threads creation from Memory Heap
 *
 * <h2>Description</h2>
 * Two threads are started by allocating the memory from the Memory Heap then
 * the remaining heap space is arbitrarily allocated and a third tread startup
 * is attempted.<br>
 * The test expects the first two threads to successfully start and the last
 * one to fail.
 */

static THD_FUNCTION(thread, p) {

  test_emit_token(*(char *)p);
}

#if CH_CFG_USE_HEAP || defined(__DOXYGEN__)
static void dyn1_setup(void) {

  chHeapObjectInit(&heap1, test.buffer, sizeof(union test_buffers));
}

static void dyn1_execute(void) {
  size_t n, sz;
  tprio_t prio = chThdGetPriorityX();

  (void)chHeapStatus(&heap1, &sz, NULL);
  /* Starting threads from the heap. */
  threads[0] = chThdCreateFromHeap(&heap1,
                                   THD_WORKING_AREA_SIZE(THREADS_STACK_SIZE),
                                   "dyn1",
                                   prio-1, thread, "A");
  threads[1] = chThdCreateFromHeap(&heap1,
                                   THD_WORKING_AREA_SIZE(THREADS_STACK_SIZE),
                                   "dyn2",
                                   prio-2, thread, "B");
  /* Large working area in order to make the thread creation fail.*/
  threads[2] = chThdCreateFromHeap(&heap1,
                                   THD_WORKING_AREA_SIZE(THREADS_STACK_SIZE * 16),
                                   "dyn3",
                                   prio-3, thread, "C");

  test_assert(1, (threads[0] != NULL) &&
                 (threads[1] != NULL) &&
                 (threads[2] == NULL) &&
                 (threads[3] == NULL) &&
                 (threads[4] == NULL),
                 "thread creation failed");

  /* Claiming the memory from terminated threads. */
  test_wait_threads();
  test_assert_sequence(2, "AB");

  /* Heap status checked again.*/
  test_assert(3, chHeapStatus(&heap1, &n, NULL) == 1, "heap fragmented");
  test_assert(4, n == sz, "heap size changed");
}

ROMCONST struct testcase testdyn1 = {
  "Dynamic APIs, threads creation from heap",
  dyn1_setup,
  NULL,
  dyn1_execute
};
#endif /* (CH_CFG_USE_HEAP && !CH_CFG_USE_MALLOC_HEAP) */

#if CH_CFG_USE_MEMPOOLS || defined(__DOXYGEN__)
/**
 * @page test_dynamic_002 Threads creation from Memory Pool
 *
 * <h2>Description</h2>
 * Five thread creation are attempted from a pool containing only four
 * elements.<br>
 * The test expects the first four threads to successfully start and the last
 * one to fail.
 */

static void dyn2_setup(void) {

  chPoolObjectInit(&mp1, THD_WORKING_AREA_SIZE(THREADS_STACK_SIZE), NULL);
}

static void dyn2_execute(void) {
  int i;
  tprio_t prio = chThdGetPriorityX();

  /* Adding the WAs to the pool. */
  for (i = 0; i < 4; i++)
    chPoolFree(&mp1, wa[i]);

  /* Starting threads from the memory pool. */
  threads[0] = chThdCreateFromMemoryPool(&mp1, "dyn1", prio-1, thread, "A");
  threads[1] = chThdCreateFromMemoryPool(&mp1, "dyn2", prio-2, thread, "B");
  threads[2] = chThdCreateFromMemoryPool(&mp1, "dyn3", prio-3, thread, "C");
  threads[3] = chThdCreateFromMemoryPool(&mp1, "dyn4", prio-4, thread, "D");
  threads[4] = chThdCreateFromMemoryPool(&mp1, "dyn5", prio-5, thread, "E");

  test_assert(1, (threads[0] != NULL) &&
                 (threads[1] != NULL) &&
                 (threads[2] != NULL) &&
                 (threads[3] != NULL) &&
                 (threads[4] == NULL),
                 "thread creation failed");

  /* Claiming the memory from terminated threads. */
  test_wait_threads();
  test_assert_sequence(2, "ABCD");

  /* Now the pool must be full again. */
  for (i = 0; i < 4; i++)
    test_assert(3, chPoolAlloc(&mp1) != NULL, "pool list empty");
  test_assert(4, chPoolAlloc(&mp1) == NULL, "pool list not empty");
}

ROMCONST struct testcase testdyn2 = {
  "Dynamic APIs, threads creation from memory pool",
  dyn2_setup,
  NULL,
  dyn2_execute
};
#endif /* CH_CFG_USE_MEMPOOLS */

#if (CH_CFG_USE_HEAP && CH_CFG_USE_REGISTRY) || defined(__DOXYGEN__)
/**
 * @page test_dynamic_003 Registry and References test
 *
 * <h2>Description</h2>
 * Registry and Thread References APIs are tested for functionality and
 * coverage.
 */

static bool regfind(thread_t *tp) {
  thread_t *ftp;
  bool found = false;

  ftp = chRegFirstThread();
  do {
    found |= ftp == tp;
    ftp = chRegNextThread(ftp);
  } while (ftp != NULL);
  return found;
}

static void dyn3_setup(void) {

  chHeapObjectInit(&heap1, test.buffer, sizeof(union test_buffers));
}

static void dyn3_execute(void) {
  thread_t *tp;
  tprio_t prio = chThdGetPriorityX();

  /* Testing references increase/decrease and final detach.*/
  tp = chThdCreateFromHeap(&heap1, WA_SIZE, "dyn1", prio-1, thread, "A");
  test_assert(1, tp->refs == 1, "wrong initial reference counter");
  chThdAddRef(tp);
  test_assert(2, tp->refs == 2, "references increase failure");
  chThdRelease(tp);
  test_assert(3, tp->refs == 1, "references decrease failure");

  /* Verify the new threads count.*/
  test_assert(4, regfind(tp), "thread missing from registry");
  test_assert(5, regfind(tp), "thread disappeared");

  /* Detach and let the thread execute and terminate.*/
  chThdRelease(tp);
  test_assert(6, tp->refs == 0, "detach failure");
  test_assert(7, tp->state == CH_STATE_READY, "invalid state");
  test_assert(8, regfind(tp), "thread disappeared");
  test_assert(9, regfind(tp), "thread disappeared");
  chThdSleepMilliseconds(50);           /* The thread just terminates.      */
  test_assert(10, tp->state == CH_STATE_FINAL, "invalid state");

  /* Clearing the zombie by scanning the registry.*/
  test_assert(11, regfind(tp), "thread disappeared");
  test_assert(12, !regfind(tp), "thread still in registry");
}

ROMCONST struct testcase testdyn3 = {
  "Dynamic APIs, registry and references",
  dyn3_setup,
  NULL,
  dyn3_execute
};
#endif /* CH_CFG_USE_HEAP && CH_CFG_USE_REGISTRY */
#endif /* CH_CFG_USE_DYNAMIC */

/**
 * @brief   Test sequence for dynamic APIs.
 */
ROMCONST struct testcase * ROMCONST patterndyn[] = {
#if CH_CFG_USE_DYNAMIC || defined(__DOXYGEN__)
#if CH_CFG_USE_HEAP || defined(__DOXYGEN__)
  &testdyn1,
#endif
#if CH_CFG_USE_MEMPOOLS || defined(__DOXYGEN__)
  &testdyn2,
#endif
#if (CH_CFG_USE_HEAP && CH_CFG_USE_REGISTRY) || defined(__DOXYGEN__)
  &testdyn3,
#endif
#endif
  NULL
};