Changeset 0f4f1b2 in mainline


Ignore:
Timestamp:
2024-01-15T17:10:27Z (4 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.

Location:
kernel
Files:
18 edited

Legend:

Unmodified
Added
Removed
  • kernel/arch/ia64/src/drivers/ski.c

    ra064d4f r0f4f1b2  
    258258
    259259        instance->srlnin = srlnin;
    260         thread_ready(instance->thread);
     260        thread_start(instance->thread);
    261261
    262262        sysinfo_set_item_val("kbd", NULL, true);
  • kernel/arch/sparc64/src/drivers/niagara.c

    ra064d4f r0f4f1b2  
    253253
    254254                        instance->srlnin = srln;
    255                         thread_ready(instance->thread);
     255                        thread_start(instance->thread);
    256256                }
    257257        }
  • kernel/genarch/src/kbrd/kbrd.c

    ra064d4f r0f4f1b2  
    200200
    201201        instance->sink = sink;
    202         thread_ready(instance->thread);
     202        thread_start(instance->thread);
    203203
    204204        return &instance->raw;
  • kernel/genarch/src/kbrd/kbrd_at.c

    ra064d4f r0f4f1b2  
    198198
    199199        instance->sink = sink;
    200         thread_ready(instance->thread);
     200        thread_start(instance->thread);
    201201
    202202        return &instance->raw;
  • kernel/genarch/src/srln/srln.c

    ra064d4f r0f4f1b2  
    156156
    157157        instance->sink = sink;
    158         thread_ready(instance->thread);
     158        thread_start(instance->thread);
    159159
    160160        return &instance->raw;
  • kernel/generic/include/proc/thread.h

    ra064d4f r0f4f1b2  
    185185extern void thread_wire(thread_t *, cpu_t *);
    186186extern void thread_attach(thread_t *, task_t *);
     187extern void thread_start(thread_t *);
    187188extern void thread_ready(thread_t *);
    188189extern void thread_exit(void) __attribute__((noreturn));
     
    242243extern errno_t thread_join(thread_t *);
    243244extern errno_t thread_join_timeout(thread_t *, uint32_t, unsigned int);
     245extern void thread_detach(thread_t *);
    244246
    245247extern void thread_yield(void);
  • kernel/generic/src/console/cmd.c

    ra064d4f r0f4f1b2  
    10041004                        printf("cpu%u: ", i);
    10051005                        thread_wire(thread, &cpus[i]);
    1006                         thread_ready(thread_ref(thread));
     1006                        thread_start(thread);
    10071007                        thread_join(thread);
    10081008                } else
  • kernel/generic/src/ipc/kbox.c

    ra064d4f r0f4f1b2  
    246246                }
    247247
    248                 task->kb.thread = thread_ref(kb_thread);
    249                 thread_ready(kb_thread);
     248                task->kb.thread = kb_thread;
     249                thread_start(kb_thread);
    250250        }
    251251
  • kernel/generic/src/main/kinit.c

    ra064d4f r0f4f1b2  
    122122
    123123                thread_wire(thread, &cpus[0]);
    124                 thread_ready(thread_ref(thread));
     124                thread_start(thread);
    125125                thread_join(thread);
    126126
     
    135135                        if (thread != NULL) {
    136136                                thread_wire(thread, &cpus[i]);
    137                                 thread_ready(thread);
     137                                thread_start(thread);
     138                                thread_detach(thread);
    138139                        } else
    139140                                log(LF_OTHER, LVL_ERROR,
     
    151152        thread = thread_create(kload, NULL, TASK, THREAD_FLAG_NONE,
    152153            "kload");
    153         if (thread != NULL)
    154                 thread_ready(thread);
    155         else
     154        if (thread != NULL) {
     155                thread_start(thread);
     156                thread_detach(thread);
     157        } else {
    156158                log(LF_OTHER, LVL_ERROR, "Unable to create kload thread");
     159        }
    157160
    158161#ifdef CONFIG_KCONSOLE
     
    163166                thread = thread_create(kconsole_thread, NULL, TASK,
    164167                    THREAD_FLAG_NONE, "kconsole");
    165                 if (thread != NULL)
    166                         thread_ready(thread);
    167                 else
     168                if (thread != NULL) {
     169                        thread_start(thread);
     170                        thread_detach(thread);
     171                } else {
    168172                        log(LF_OTHER, LVL_ERROR,
    169173                            "Unable to create kconsole thread");
     174                }
    170175        }
    171176#endif /* CONFIG_KCONSOLE */
  • kernel/generic/src/main/main.c

    ra064d4f r0f4f1b2  
    282282        if (!kinit_thread)
    283283                panic("Cannot create kinit thread.");
    284         thread_ready(kinit_thread);
     284        thread_start(kinit_thread);
     285        thread_detach(kinit_thread);
    285286
    286287        /*
  • kernel/generic/src/proc/program.c

    ra064d4f r0f4f1b2  
    212212void program_ready(program_t *prg)
    213213{
    214         thread_ready(prg->main_thread);
     214        thread_start(prg->main_thread);
     215        thread_detach(prg->main_thread);
    215216        prg->main_thread = NULL;
    216217}
  • kernel/generic/src/proc/thread.c

    ra064d4f r0f4f1b2  
    234234}
    235235
     236/** Start a thread that wasn't started yet since it was created.
     237 *
     238 * @param thread A reference to the newly created thread.
     239 */
     240void thread_start(thread_t *thread)
     241{
     242        assert(thread->state == Entering);
     243        thread_ready(thread_ref(thread));
     244}
     245
    236246/** Make thread ready
    237247 *
     
    696706errno_t thread_join_timeout(thread_t *thread, uint32_t usec, unsigned int flags)
    697707{
     708        assert(thread != NULL);
     709
    698710        if (thread == THREAD)
    699711                return EINVAL;
     
    712724
    713725        return rc;
     726}
     727
     728void thread_detach(thread_t *thread)
     729{
     730        thread_put(thread);
    714731}
    715732
  • kernel/test/mm/falloc2.c

    ra064d4f r0f4f1b2  
    4343#define THREADS      8
    4444
    45 static atomic_size_t thread_cnt;
    4645static atomic_size_t thread_fail;
    4746
     
    5655                    "Unable to allocate frames\n", THREAD->tid, CPU->id);
    5756                atomic_inc(&thread_fail);
    58                 atomic_dec(&thread_cnt);
    5957                return;
    6058        }
     
    108106        TPRINTF("Thread #%" PRIu64 " (cpu%u): Exiting\n",
    109107            THREAD->tid, CPU->id);
    110         atomic_dec(&thread_cnt);
    111108}
    112109
    113110const char *test_falloc2(void)
    114111{
    115         atomic_store(&thread_cnt, THREADS);
    116112        atomic_store(&thread_fail, 0);
     113
     114        thread_t *threads[THREADS] = { };
    117115
    118116        for (unsigned int i = 0; i < THREADS; i++) {
     
    123121                        break;
    124122                }
    125                 thread_ready(thrd);
     123                thread_start(thrd);
     124                threads[i] = thrd;
    126125        }
    127126
    128         while (atomic_load(&thread_cnt) > 0) {
    129                 TPRINTF("Threads left: %zu\n",
    130                     atomic_load(&thread_cnt));
    131                 thread_sleep(1);
     127        for (unsigned int i = 0; i < THREADS; i++) {
     128                if (threads[i] != NULL)
     129                        thread_join(threads[i]);
     130
     131                TPRINTF("Threads left: %u\n", THREADS - i - 1);
    132132        }
    133133
  • kernel/test/mm/slab1.c

    ra064d4f r0f4f1b2  
    121121static void *thr_data[THREADS][THR_MEM_COUNT];
    122122static slab_cache_t *thr_cache;
    123 static semaphore_t thr_sem;
    124123
    125124static void slabtest(void *data)
     
    142141
    143142        TPRINTF("Thread #%" PRIu64 " finished\n", THREAD->tid);
    144 
    145         semaphore_up(&thr_sem);
    146143}
    147144
    148145static void testthreads(void)
    149146{
    150         thread_t *t;
    151         int i;
    152 
    153147        thr_cache = slab_cache_create("thread_cache", THR_MEM_SIZE, 0, NULL, NULL,
    154148            SLAB_CACHE_NOMAGAZINE);
    155149
    156         semaphore_initialize(&thr_sem, 0);
    157         for (i = 0; i < THREADS; i++) {
    158                 if (!(t = thread_create(slabtest, (void *) (sysarg_t) i, TASK, THREAD_FLAG_NONE, "slabtest"))) {
     150        thread_t *threads[THREADS] = { };
     151
     152        for (int i = 0; i < THREADS; i++) {
     153                threads[i] = thread_create(slabtest, (void *) (sysarg_t) i,
     154                    TASK, THREAD_FLAG_NONE, "slabtest");
     155                if (threads[i]) {
     156                        thread_start(threads[i]);
     157                } else {
    159158                        TPRINTF("Could not create thread %d\n", i);
    160                 } else
    161                         thread_ready(t);
     159                }
    162160        }
    163161
    164         for (i = 0; i < THREADS; i++)
    165                 semaphore_down(&thr_sem);
     162        for (int i = 0; i < THREADS; i++) {
     163                if (threads[i] != NULL)
     164                        thread_join(threads[i]);
     165        }
    166166
    167167        slab_cache_destroy(thr_cache);
  • kernel/test/mm/slab2.c

    ra064d4f r0f4f1b2  
    127127
    128128static slab_cache_t *thr_cache;
    129 static semaphore_t thr_sem;
    130129static condvar_t thread_starter;
    131130static mutex_t starter_mutex;
     
    188187        if (!test_quiet)
    189188                slab_print_list();
    190 
    191         semaphore_up(&thr_sem);
    192189}
    193190
     
    198195         * then release everything, then again allocate, then release
    199196         */
    200         thread_t *t;
    201         int i;
    202197
    203198        TPRINTF("Running stress test with size %d\n", size);
     
    207202
    208203        thr_cache = slab_cache_create("thread_cache", size, 0, NULL, NULL, 0);
    209         semaphore_initialize(&thr_sem, 0);
    210         for (i = 0; i < THREADS; i++) {
    211                 if (!(t = thread_create(slabtest, NULL, TASK, THREAD_FLAG_NONE, "slabtest"))) {
     204
     205        thread_t *threads[THREADS] = { };
     206
     207        for (int i = 0; i < THREADS; i++) {
     208                threads[i] = thread_create(slabtest, NULL,
     209                    TASK, THREAD_FLAG_NONE, "slabtest");
     210                if (threads[i]) {
     211                        thread_start(threads[i]);
     212                } else {
    212213                        TPRINTF("Could not create thread %d\n", i);
    213                 } else
    214                         thread_ready(t);
    215         }
     214                }
     215        }
     216
    216217        thread_sleep(1);
    217218        condvar_broadcast(&thread_starter);
    218219
    219         for (i = 0; i < THREADS; i++)
    220                 semaphore_down(&thr_sem);
     220        for (int i = 0; i < THREADS; i++) {
     221                if (threads[i] != NULL)
     222                        thread_join(threads[i]);
     223        }
    221224
    222225        slab_cache_destroy(thr_cache);
  • kernel/test/synch/semaphore1.c

    ra064d4f r0f4f1b2  
    8989                                thrd = thread_create(consumer, NULL, TASK,
    9090                                    THREAD_FLAG_NONE, "consumer");
    91                                 if (thrd)
    92                                         thread_ready(thrd);
    93                                 else
     91                                if (thrd) {
     92                                        thread_start(thrd);
     93                                        thread_detach(thrd);
     94                                } else {
    9495                                        TPRINTF("could not create consumer %d\n", i);
     96                                }
    9597                        }
    9698                        for (k = 0; k < (4 - i); k++) {
    9799                                thrd = thread_create(producer, NULL, TASK,
    98100                                    THREAD_FLAG_NONE, "producer");
    99                                 if (thrd)
    100                                         thread_ready(thrd);
    101                                 else
     101                                if (thrd) {
     102                                        thread_start(thrd);
     103                                        thread_detach(thrd);
     104                                } else {
    102105                                        TPRINTF("could not create producer %d\n", i);
     106                                }
    103107                        }
    104108                }
  • kernel/test/synch/semaphore2.c

    ra064d4f r0f4f1b2  
    9292                thrd = thread_create(consumer, NULL, TASK,
    9393                    THREAD_FLAG_NONE, "consumer");
    94                 if (thrd)
    95                         thread_ready(thrd);
    96                 else
     94                if (thrd) {
     95                        thread_start(thrd);
     96                        thread_detach(thrd);
     97                } else {
    9798                        TPRINTF("Error creating thread\n");
     99                }
    98100        }
    99101
  • 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.