Jerome Forissier | cb16387 | 2025-04-18 16:09:38 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2025 Linaro Limited |
| 4 | * |
| 5 | * Unit test for uthread |
| 6 | */ |
| 7 | |
| 8 | #include <stdbool.h> |
| 9 | #include <test/lib.h> |
| 10 | #include <test/ut.h> |
| 11 | #include <uthread.h> |
| 12 | |
| 13 | static int count; |
| 14 | |
| 15 | /* A thread entry point */ |
| 16 | static void worker(void *arg) |
| 17 | { |
| 18 | int loops = (int)(unsigned long)arg; |
| 19 | int i; |
| 20 | |
| 21 | for (i = 0; i < loops; i++) { |
| 22 | count++; |
| 23 | uthread_schedule(); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | /* |
| 28 | * uthread() - testing the uthread API |
| 29 | * |
| 30 | * This function creates two threads with the same entry point. The first one |
| 31 | * receives 5 as an argument, the second one receives 10. The number indicates |
| 32 | * the number of time the worker thread should loop on uthread_schedule() |
| 33 | * before returning. The workers increment a global counter each time they loop. |
| 34 | * As a result the main thread knows how many times it should call |
| 35 | * uthread_schedule() to let the two threads proceed, and it also knows which |
| 36 | * value the counter should have at any moment. |
| 37 | */ |
| 38 | static int uthread(struct unit_test_state *uts) |
| 39 | { |
| 40 | int i; |
| 41 | int id1, id2; |
| 42 | |
| 43 | count = 0; |
| 44 | id1 = uthread_grp_new_id(); |
| 45 | ut_assert(id1 != 0); |
| 46 | id2 = uthread_grp_new_id(); |
| 47 | ut_assert(id2 != 0); |
| 48 | ut_assert(id1 != id2); |
| 49 | ut_assertok(uthread_create(NULL, worker, (void *)5, 0, id1)); |
| 50 | ut_assertok(uthread_create(NULL, worker, (void *)10, 0, 0)); |
| 51 | /* |
| 52 | * The first call is expected to schedule the first worker, which will |
| 53 | * schedule the second one, which will schedule back to the main thread |
| 54 | * (here). Therefore count should be 2. |
| 55 | */ |
| 56 | ut_assert(uthread_schedule()); |
| 57 | ut_asserteq(2, count); |
| 58 | ut_assert(!uthread_grp_done(id1)); |
| 59 | /* Four more calls should bring the count to 10 */ |
| 60 | for (i = 0; i < 4; i++) { |
| 61 | ut_assert(!uthread_grp_done(id1)); |
| 62 | ut_assert(uthread_schedule()); |
| 63 | } |
| 64 | ut_asserteq(10, count); |
| 65 | /* This one allows the first worker to exit */ |
| 66 | ut_assert(uthread_schedule()); |
| 67 | /* At this point there should be no runnable thread in group 'id1' */ |
| 68 | ut_assert(uthread_grp_done(id1)); |
| 69 | /* Five more calls for the second worker to finish incrementing */ |
| 70 | for (i = 0; i < 5; i++) |
| 71 | ut_assert(uthread_schedule()); |
| 72 | ut_asserteq(15, count); |
| 73 | /* Plus one call to let the second worker return from its entry point */ |
| 74 | ut_assert(uthread_schedule()); |
| 75 | /* Now both tasks should be done, schedule should return false */ |
| 76 | ut_assert(!uthread_schedule()); |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | LIB_TEST(uthread, 0); |