Changeset 0f4f1b2 in mainline for kernel/test/thread/thread1.c


Ignore:
Timestamp:
2024-01-15T17:10:27Z (11 months ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
master
Children:
e82879c
Parents:
a064d4f
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2024-01-15 16:37:22)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2024-01-15 17:10:27)
Message:

Add (and use) functions thread_start() and thread_detach()

Mostly cosmetic, with thread_start() replacing calls to thread_ready(),
but not consuming the passed reference, and thread_detach() being
synonym for thread_put(). Makes the code's function more obvious.

Also modify some threaded tests to use thread_join() for waiting,
instead of counting threads with atomics or semaphores.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/test/thread/thread1.c

    ra064d4f r0f4f1b2  
    3838
    3939static atomic_bool finish;
    40 static atomic_size_t threads_finished;
    4140
    4241static void threadtest(void *data)
     
    4645                thread_usleep(100000);
    4746        }
    48         atomic_inc(&threads_finished);
    4947}
    5048
    5149const char *test_thread1(void)
    5250{
    53         unsigned int i;
    54         size_t total = 0;
     51        atomic_store(&finish, true);
    5552
    56         atomic_store(&finish, true);
    57         atomic_store(&threads_finished, 0);
     53        thread_t *threads[THREADS] = { };
    5854
    59         for (i = 0; i < THREADS; i++) {
    60                 thread_t *t;
    61                 if (!(t = thread_create(threadtest, NULL, TASK,
    62                     THREAD_FLAG_NONE, "threadtest"))) {
     55        for (int i = 0; i < THREADS; i++) {
     56                threads[i] = thread_create(threadtest, NULL,
     57                    TASK, THREAD_FLAG_NONE, "threadtest");
     58
     59                if (threads[i]) {
     60                        thread_start(threads[i]);
     61                } else {
    6362                        TPRINTF("Could not create thread %d\n", i);
    6463                        break;
    6564                }
    66                 thread_ready(t);
    67                 total++;
    6865        }
    6966
     
    7269
    7370        atomic_store(&finish, false);
    74         while (atomic_load(&threads_finished) < total) {
    75                 TPRINTF("Threads left: %zu\n", total - atomic_load(&threads_finished));
    76                 thread_sleep(1);
     71
     72        for (int i = 0; i < THREADS; i++) {
     73                if (threads[i] != NULL)
     74                        thread_join(threads[i]);
     75
     76                TPRINTF("Threads left: %d\n", THREADS - i - 1);
    7777        }
    7878
Note: See TracChangeset for help on using the changeset viewer.