Boot Linux faster!

Check our new training course

Boot Linux faster!

Check our new training course
and Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

  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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*
 * Copyright (c) 2016 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <ztest.h>
#include <irq_offload.h>

#define TIMEOUT 100
#define STACK_SIZE 512
#define PENDING_MAX 2
#define SEM_INITIAL 0
#define SEM_LIMIT 1

K_SEM_DEFINE(sync_sema, SEM_INITIAL, SEM_LIMIT);

static int alert_handler0(struct k_alert *);
static int alert_handler1(struct k_alert *);

/**TESTPOINT: init via K_ALERT_DEFINE*/
K_ALERT_DEFINE(kalert_pending, alert_handler1, PENDING_MAX);
K_ALERT_DEFINE(kalert_consumed, alert_handler0, PENDING_MAX);

enum handle_type {
	HANDLER_IGNORE,
	HANDLER_DEFAULT,
	HANDLER_0,
	HANDLER_1
};

static K_THREAD_STACK_DEFINE(tstack, STACK_SIZE);
static K_THREAD_STACK_DEFINE(sync_tstack, STACK_SIZE);
__kernel struct k_thread tdata;
__kernel struct k_thread sync_tdata;
__kernel struct k_alert thread_alerts[4];
static struct k_alert *palert;
static enum handle_type htype;
static volatile u32_t handler_executed;
static volatile u32_t handler_val;


/*handlers*/
static int alert_handler0(struct k_alert *alt)
{
	handler_executed++;
	return 0;
}

static int alert_handler1(struct k_alert *alt)
{
	handler_executed++;
	return 1;
}

static void alert_send(void)
{
	/**TESTPOINT: alert send*/
	for (int i = 0; i < PENDING_MAX; i++) {
		k_alert_send(palert);
	}
}

static void alert_recv(void)
{
	int ret;

	switch (htype) {
	case HANDLER_0:
		zassert_equal(handler_executed, PENDING_MAX, NULL);
		/* Fall through */
	case HANDLER_IGNORE:
		ret = k_alert_recv(palert, TIMEOUT);
		zassert_equal(ret, -EAGAIN, NULL);
		break;
	case HANDLER_1:
		zassert_equal(handler_executed, PENDING_MAX, NULL);
		/* Fall through */
	case HANDLER_DEFAULT:
		for (int i = 0; i < PENDING_MAX; i++) {
			/**TESTPOINT: alert recv*/
			ret = k_alert_recv(palert, K_NO_WAIT);
			zassert_false(ret, NULL);
		}
		/**TESTPOINT: alert recv -EAGAIN*/
		ret = k_alert_recv(palert, TIMEOUT);
		zassert_equal(ret, -EAGAIN, NULL);
		/**TESTPOINT: alert recv -EBUSY*/
		ret = k_alert_recv(palert, K_NO_WAIT);
		zassert_equal(ret, -EBUSY, NULL);
	}
}

static void thread_entry(void *p1, void *p2, void *p3)
{
	alert_recv();
}

static void thread_alert(void)
{
	handler_executed = 0;
	/**TESTPOINT: thread-thread sync via alert*/
	k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
				      thread_entry, NULL, NULL, NULL,
				      K_PRIO_PREEMPT(0),
				      K_USER | K_INHERIT_PERMS,
				      0);
	alert_send();
	k_sleep(TIMEOUT);
	k_thread_abort(tid);
}

static void tisr_entry(void *p)
{
	alert_send();
}

static void sync_entry(void *p)
{
	k_alert_send(palert);
}

static void isr_alert(void)
{
	handler_executed = 0;
	/**TESTPOINT: thread-isr sync via alert*/
	irq_offload(tisr_entry, NULL);
	k_sleep(TIMEOUT);
	alert_recv();
}

int event_handler(struct k_alert *alt)
{
	return handler_val;
}

/**
 * @brief Tests for the Alert kernel object
 * @defgroup kernel_alert_tests Alerts
 * @ingroup all_tests
 * @{
 */

/**
 * @brief Test thread alert default
 *
 * Checks k_alert_init(), k_alert_send(), k_alert_recv() Kernel APIs.
 *
 * Initializes an alert and creates a thread that signals an alert with
 * k_alert_send() and then calls k_alert_recv() with K_NO_WAIT to receive the
 * alerts. Checks if k_alert_recv() returns appropriate error values when
 * alerts are not received.
 *
 * @see k_alert_init(), k_alert_send(), k_alert_recv()
 */
void test_thread_alert_default(void)
{
	palert = &thread_alerts[HANDLER_DEFAULT];
	htype = HANDLER_DEFAULT;
	thread_alert();
}

/**
 * @brief Test thread alert ignore
 *
 * Checks k_alert_init(), k_alert_send(), k_alert_recv() Kernel APIs - creates
 * a thread that signals an alert using k_alert_send() and then calls
 * k_alert_recv() with TIMEOUT of 100ms which is the waiting period for
 * receiving the alert.
 *
 * @see k_alert_init(), k_alert_send(), k_alert_recv()
 */
void test_thread_alert_ignore(void)
{
	palert = &thread_alerts[HANDLER_IGNORE];
	htype = HANDLER_IGNORE;
	thread_alert();
}

/**
 * @brief Test thread alert consumed
 *
 * Checks k_alert_init(), k_alert_send(), k_alert_recv() Kernel APIs.
 *
 * Creates a thread that signals an alert using k_alert_send(). Now
 * k_alert_send() for this case is initialized with an address of the handler
 * function, which increases handler_executed count each time it is called.
 *
 * @see k_alert_init(), k_alert_send(), k_alert_recv()
 */
void test_thread_alert_consumed(void)
{
	/**TESTPOINT: alert handler return 0*/
	palert = &thread_alerts[HANDLER_0];
	htype = HANDLER_0;
	thread_alert();
}

/**
 * @brief Test thread alert pending
 *
 * Checks k_alert_init(), k_alert_send(), k_alert_recv() Kernel APIs
 *
 * Creates a thread that signals an alert using k_alert_send().
 *
 * @see k_alert_init(), k_alert_send(), k_alert_recv()
 */
void test_thread_alert_pending(void)
{
	/**TESTPOINT: alert handler return 1*/
	palert = &thread_alerts[HANDLER_1];
	htype = HANDLER_1;
	thread_alert();
}

/**
 * @brief Test default isr alert
 *
 * Similar to test_thread_alert_default(), but verifies kernel objects and
 * APIs work correctly in interrupt context with the help of irq_offload()
 * function.
 *
 * @see k_alert_init(), k_alert_send(), k_alert_recv()
 */
void test_isr_alert_default(void)
{
	struct k_alert alert;

	/**TESTPOINT: init via k_alert_init*/
	k_alert_init(&alert, K_ALERT_DEFAULT, PENDING_MAX);

	/**TESTPOINT: alert handler default*/
	palert = &alert;
	htype = HANDLER_DEFAULT;
	isr_alert();
}

/**
 * @brief Test isr alert ignore
 *
 * Similar to test_thread_alert_ignore(), but verifies kernel objects and
 * APIs work correctly in interrupt context with the help of irq_offload()
 * function.
 *
 * @see k_alert_init(), k_alert_send(), k_alert_recv()
 */
void test_isr_alert_ignore(void)
{
	/**TESTPOINT: alert handler ignore*/
	struct k_alert alert;

	/**TESTPOINT: init via k_alert_init*/
	k_alert_init(&alert, K_ALERT_IGNORE, PENDING_MAX);
	palert = &alert;
	htype = HANDLER_IGNORE;
	isr_alert();
}

/**
 * @brief Test isr alert consumed
 *
 * Similar to test_thread_alert_consumed, but verifies kernel objects
 * and APIs work correctly in interrupt context with the help of irq_offload()
 * function.
 *
 * @see k_alert_init(), k_alert_send(), k_alert_recv()
 */
void test_isr_alert_consumed(void)
{
	struct k_alert alert;

	/**TESTPOINT: init via k_alert_init*/
	k_alert_init(&alert, alert_handler0, PENDING_MAX);

	/**TESTPOINT: alert handler return 0*/
	palert = &alert;
	htype = HANDLER_0;
	isr_alert();
}

/**
 * @brief Test isr alert pending
 *
 * Similar to test_thread_alert_pending(), but verifies kernel objects and
 * APIs work correctly in interrupt context with the help of irq_offload()
 * function.
 *
 * @see k_alert_init(), k_alert_send(), k_alert_recv()
 */
void test_isr_alert_pending(void)
{
	struct k_alert alert;

	/**TESTPOINT: init via k_alert_init*/
	k_alert_init(&alert, alert_handler1, PENDING_MAX);

	/**TESTPOINT: alert handler return 0*/
	palert = &alert;
	htype = HANDLER_1;
	isr_alert();
}

/**
 * @brief Test thread kinit alert
 *
 * Tests consumed and pending thread alert cases (reference line numbers 4 and
 * 5), but handles case where alert has been defined via #K_ALERT_DEFINE(x) and not
 * k_alert_init()
 *
 * @see #K_ALERT_DEFINE(x)
 */
void test_thread_kinit_alert(void)
{
	palert = &kalert_consumed;
	htype = HANDLER_0;
	thread_alert();
	palert = &kalert_pending;
	htype = HANDLER_1;
	thread_alert();
}

/**
 * @brief Test isr kinit alert
 *
 * Checks consumed and pending isr alert cases but alert has been defined via
 * #K_ALERT_DEFINE(x) and not k_alert_init()
 *
 * @see #K_ALERT_DEFINE(x)
 */
void test_isr_kinit_alert(void)
{
	palert = &kalert_consumed;
	htype = HANDLER_0;
	isr_alert();
	palert = &kalert_pending;
	htype = HANDLER_1;
	isr_alert();
}


/**
 * @brief Test alert_recv(timeout)
 *
 * This test checks k_alert_recv(timeout) against the following cases:
 *  1. The current task times out while waiting for the event.
 *  2. There is already an event waiting (signaled from a task).
 *  3. The current task must wait on the event until it is signaled
 *     from either another task or an ISR.
 *
 * @see k_alert_recv()
 */
void test_thread_alert_timeout(void)
{
	/**TESTPOINT: alert handler ignore*/
	struct k_alert alert;
	int ret, i;

	/**TESTPOINT: init via k_alert_init*/
	k_alert_init(&alert, K_ALERT_DEFAULT, PENDING_MAX);

	palert = &alert;

	ret = k_alert_recv(&alert, TIMEOUT);

	zassert_equal(ret, -EAGAIN, NULL);

	k_alert_send(&alert);

	ret = k_alert_recv(&alert, TIMEOUT);

	zassert_equal(ret, 0, NULL);

	k_sem_give(&sync_sema);

	for (i = 0; i < 2; i++) {
		ret = k_alert_recv(&alert, TIMEOUT);

		zassert_equal(ret, 0, NULL);
	}
}

/**
 * @brief Test k_alert_recv() against different cases
 *
 * This test checks k_alert_recv(K_FOREVER) against
 * the following cases:
 *  1. There is already an event waiting (signaled from a task and ISR).
 *  2. The current task must wait on the event until it is signaled
 *     from either another task or an ISR.
 *
 * @see k_alert_recv()
 */
void test_thread_alert_wait(void)
{
	/**TESTPOINT: alert handler ignore*/
	struct k_alert alert;
	int ret, i;

	/**TESTPOINT: init via k_alert_init*/
	k_alert_init(&alert, K_ALERT_DEFAULT, PENDING_MAX);

	palert = &alert;

	k_alert_send(&alert);

	ret = k_alert_recv(&alert, K_FOREVER);

	zassert_equal(ret, 0, NULL);

	irq_offload(sync_entry, NULL);

	ret = k_alert_recv(&alert, K_FOREVER);

	zassert_equal(ret, 0, NULL);

	k_sem_give(&sync_sema);

	for (i = 0; i < 2; i++) {
		ret = k_alert_recv(&alert, K_FOREVER);

		zassert_equal(ret, 0, NULL);
	}
}

/**
 * @brief Test thread alert handler
 *
 * This test checks that the event handler is set up properly when
 * k_alert_init() is called with an event handler.  It shows that event
 * handlers are tied to the specified event and that the return value from the
 * handler affects whether the event wakes a task waiting upon that event.
 *
 * @see k_alert_init()
 */
void test_thread_alert_handler(void)
{
	/**TESTPOINT: alert handler ignore*/
	struct k_alert alert;
	int ret;

	/**TESTPOINT: init via k_alert_init*/
	k_alert_init(&alert, event_handler, PENDING_MAX);

	palert = &alert;

	k_sem_give(&sync_sema);

	ret = k_alert_recv(&alert, TIMEOUT);

	zassert_equal(ret, -EAGAIN, NULL);

	k_sem_give(&sync_sema);

	ret = k_alert_recv(&alert, TIMEOUT);

	zassert_equal(ret, 0, NULL);
}
/**
 * @}
 */


/**
 * Signal various events to a waiting task
 */
void signal_task(void *p1, void *p2, void *p3)
{
	k_sem_init(&sync_sema, 0, 1);

	k_sem_take(&sync_sema, K_FOREVER);
	k_alert_send(palert);
	irq_offload(sync_entry, NULL);

	k_sem_take(&sync_sema, K_FOREVER);
	k_alert_send(palert);
	irq_offload(sync_entry, NULL);

	k_sem_take(&sync_sema, K_FOREVER);
	handler_val = 0;
	k_alert_send(palert);

	k_sem_take(&sync_sema, K_FOREVER);
	handler_val = 1;
	k_alert_send(palert);
}

/*test case main entry*/
void test_main(void)
{
	k_thread_access_grant(k_current_get(), &kalert_pending,
			      &kalert_consumed, &tdata, &tstack,
			      &thread_alerts[HANDLER_DEFAULT],
			      &thread_alerts[HANDLER_IGNORE],
			      &thread_alerts[HANDLER_0],
			      &thread_alerts[HANDLER_1], NULL);

	k_alert_init(&thread_alerts[HANDLER_DEFAULT], K_ALERT_DEFAULT,
		     PENDING_MAX);
	k_alert_init(&thread_alerts[HANDLER_IGNORE], K_ALERT_IGNORE,
		     PENDING_MAX);
	k_alert_init(&thread_alerts[HANDLER_0], alert_handler0, PENDING_MAX);
	k_alert_init(&thread_alerts[HANDLER_1], alert_handler1, PENDING_MAX);

	/**TESTPOINT: thread-thread sync via alert*/
	k_thread_create(&sync_tdata, sync_tstack, STACK_SIZE,
				      signal_task, NULL, NULL, NULL,
				      K_PRIO_PREEMPT(0), 0, 0);

	ztest_test_suite(alert_api,
			 ztest_unit_test(test_thread_alert_timeout),
			 ztest_unit_test(test_thread_alert_wait),
			 ztest_unit_test(test_thread_alert_handler),
			 ztest_user_unit_test(test_thread_alert_default),
			 ztest_user_unit_test(test_thread_alert_ignore),
			 ztest_user_unit_test(test_thread_alert_consumed),
			 ztest_user_unit_test(test_thread_alert_pending),
			 ztest_unit_test(test_isr_alert_default),
			 ztest_unit_test(test_isr_alert_ignore),
			 ztest_unit_test(test_isr_alert_consumed),
			 ztest_unit_test(test_isr_alert_pending),
			 ztest_user_unit_test(test_thread_kinit_alert),
			 ztest_unit_test(test_isr_kinit_alert));
	ztest_run_test_suite(alert_api);
}