Changeset aab5e46 in mainline


Ignore:
Timestamp:
2018-11-03T23:32:39Z (5 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
790f3a3
Parents:
ef1eab7
Message:

Thread and task iterator functions.

Location:
kernel
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/include/proc/task.h

    ref1eab7 raab5e46  
    158158extern void task_release(task_t *);
    159159extern task_t *task_find_by_id(task_id_t);
     160extern size_t task_count(void);
     161extern task_t *task_first(void);
     162extern task_t *task_next(task_t *);
    160163extern errno_t task_kill(task_id_t);
    161164extern void task_kill_self(bool) __attribute__((noreturn));
  • kernel/generic/include/proc/thread.h

    ref1eab7 raab5e46  
    261261extern void thread_destroy(thread_t *, bool);
    262262extern thread_t *thread_find_by_id(thread_id_t);
     263extern size_t thread_count(void);
     264extern thread_t *thread_first(void);
     265extern thread_t *thread_next(thread_t *);
    263266extern void thread_update_accounting(bool);
    264267extern bool thread_exists(thread_t *);
  • kernel/generic/src/proc/task.c

    ref1eab7 raab5e46  
    107107{
    108108        size_t tasks_left;
    109         odlink_t *odlink;
    110109        task_t *task;
    111110
     
    128127                tasks_left = 0;
    129128
    130                 odlink = odict_first(&tasks);
    131                 while (odlink != NULL) {
    132                         task = odict_get_instance(odlink, task_t, ltasks);
    133 
     129                task = task_first();
     130                while (task != NULL) {
    134131                        if (task != TASK) {
    135132                                tasks_left++;
     
    140137                        }
    141138
    142                         odlink = odict_next(odlink, &tasks);
     139                        task = task_next(task);
    143140                }
    144141
     
    452449}
    453450
     451/** Get count of tasks.
     452 *
     453 * @return Number of tasks in the system
     454 */
     455size_t task_count(void)
     456{
     457        assert(interrupts_disabled());
     458        assert(irq_spinlock_locked(&tasks_lock));
     459
     460        return odict_count(&tasks);
     461}
     462
     463/** Get first task (task with lowest ID).
     464 *
     465 * @return Pointer to first task or @c NULL if there are none.
     466 */
     467task_t *task_first(void)
     468{
     469        odlink_t *odlink;
     470
     471        assert(interrupts_disabled());
     472        assert(irq_spinlock_locked(&tasks_lock));
     473
     474        odlink = odict_first(&tasks);
     475        if (odlink == NULL)
     476                return NULL;
     477
     478        return odict_get_instance(odlink, task_t, ltasks);
     479}
     480
     481/** Get next task (with higher task ID).
     482 *
     483 * @param cur Current task
     484 * @return Pointer to next task or @c NULL if there are no more tasks.
     485 */
     486task_t *task_next(task_t *cur)
     487{
     488        odlink_t *odlink;
     489
     490        assert(interrupts_disabled());
     491        assert(irq_spinlock_locked(&tasks_lock));
     492
     493        odlink = odict_next(&cur->ltasks, &tasks);
     494        if (odlink == NULL)
     495                return NULL;
     496
     497        return odict_get_instance(odlink, task_t, ltasks);
     498}
     499
    454500/** Get accounting data of given task.
    455501 *
     
    658704#endif
    659705
    660         odlink_t *odlink;
    661706        task_t *task;
    662707
    663         odlink = odict_first(&tasks);
    664         while (odlink != NULL) {
    665                 task = odict_get_instance(odlink, task_t, ltasks);
     708        task = task_first();
     709        while (task != NULL) {
    666710                task_print(task, additional);
    667                 odlink = odict_next(odlink, &tasks);
     711                task = task_next(task);
    668712        }
    669713
  • kernel/generic/src/proc/thread.c

    ref1eab7 raab5e46  
    772772void thread_print_list(bool additional)
    773773{
    774         odlink_t *odlink;
    775774        thread_t *thread;
    776775
     
    796795#endif
    797796
    798         odlink = odict_first(&threads);
    799         while (odlink != NULL) {
    800                 thread = odict_get_instance(odlink, thread_t, lthreads);
     797        thread = thread_first();
     798        while (thread != NULL) {
    801799                thread_print(thread, additional);
    802                 odlink = odict_next(odlink, &threads);
     800                thread = thread_next(thread);
    803801        }
    804802
     
    860858thread_t *thread_find_by_id(thread_id_t thread_id)
    861859{
    862         odlink_t *odlink;
    863860        thread_t *thread;
    864861
     
    866863        assert(irq_spinlock_locked(&threads_lock));
    867864
    868         odlink = odict_first(&threads);
    869         while (odlink != NULL) {
    870                 thread = odict_get_instance(odlink, thread_t, lthreads);
     865        thread = thread_first();
     866        while (thread != NULL) {
    871867                if (thread->tid == thread_id)
    872868                        return thread;
    873869
    874                 odlink = odict_next(odlink, &threads);
     870                thread = thread_next(thread);
    875871        }
    876872
    877873        return NULL;
     874}
     875
     876/** Get count of threads.
     877 *
     878 * @return Number of threads in the system
     879 */
     880size_t thread_count(void)
     881{
     882        assert(interrupts_disabled());
     883        assert(irq_spinlock_locked(&threads_lock));
     884
     885        return odict_count(&threads);
     886}
     887
     888/** Get first thread.
     889 *
     890 * @return Pointer to first thread or @c NULL if there are none.
     891 */
     892thread_t *thread_first(void)
     893{
     894        odlink_t *odlink;
     895
     896        assert(interrupts_disabled());
     897        assert(irq_spinlock_locked(&threads_lock));
     898
     899        odlink = odict_first(&threads);
     900        if (odlink == NULL)
     901                return NULL;
     902
     903        return odict_get_instance(odlink, thread_t, lthreads);
     904}
     905
     906/** Get next thread.
     907 *
     908 * @param cur Current thread
     909 * @return Pointer to next thread or @c NULL if there are no more threads.
     910 */
     911thread_t *thread_next(thread_t *cur)
     912{
     913        odlink_t *odlink;
     914
     915        assert(interrupts_disabled());
     916        assert(irq_spinlock_locked(&threads_lock));
     917
     918        odlink = odict_next(&cur->lthreads, &threads);
     919        if (odlink == NULL)
     920                return NULL;
     921
     922        return odict_get_instance(odlink, thread_t, lthreads);
    878923}
    879924
  • kernel/generic/src/sysinfo/stats.c

    ref1eab7 raab5e46  
    246246
    247247        /* Count the tasks */
    248         size_t count = odict_count(&tasks);
     248        size_t count = task_count();
    249249
    250250        if (count == 0) {
     
    271271        /* Gather the statistics for each task */
    272272        size_t i = 0;
    273         odlink_t *odlink = odict_first(&tasks);
    274         while (odlink != NULL) {
    275                 task_t *task = odict_get_instance(odlink, task_t, ltasks);
    276 
     273        task_t *task = task_first();
     274        while (task != NULL) {
    277275                /* Interrupts are already disabled */
    278276                irq_spinlock_lock(&(task->lock), false);
     
    283281
    284282                irq_spinlock_unlock(&(task->lock), false);
    285                 odlink = odict_next(odlink, &tasks);
     283                task = task_next(task);
    286284        }
    287285
     
    336334
    337335        /* Count the threads */
    338         size_t count = odict_count(&threads);
     336        size_t count = thread_count();
    339337
    340338        if (count == 0) {
     
    362360        size_t i = 0;
    363361
    364         odlink_t *odlink = odict_first(&threads);
    365         while (odlink != NULL) {
    366                 thread_t *thread = odict_get_instance(odlink, thread_t,
    367                     lthreads);
    368 
     362        thread_t *thread = thread_first();
     363        while (thread != NULL) {
    369364                /* Interrupts are already disabled */
    370365                irq_spinlock_lock(&thread->lock, false);
     
    376371                irq_spinlock_unlock(&thread->lock, false);
    377372
    378                 odlink = odict_next(odlink, &threads);
     373                thread = thread_next(thread);
    379374        }
    380375
  • kernel/test/mm/falloc2.c

    ref1eab7 raab5e46  
    4343#define THREADS      8
    4444
    45 static atomic_t thread_count;
     45static atomic_t thread_cnt;
    4646static atomic_t thread_fail;
    4747
     
    5656                    "Unable to allocate frames\n", THREAD->tid, CPU->id);
    5757                atomic_inc(&thread_fail);
    58                 atomic_dec(&thread_count);
     58                atomic_dec(&thread_cnt);
    5959                return;
    6060        }
     
    110110        TPRINTF("Thread #%" PRIu64 " (cpu%u): Exiting\n",
    111111            THREAD->tid, CPU->id);
    112         atomic_dec(&thread_count);
     112        atomic_dec(&thread_cnt);
    113113}
    114114
    115115const char *test_falloc2(void)
    116116{
    117         atomic_store(&thread_count, THREADS);
     117        atomic_store(&thread_cnt, THREADS);
    118118        atomic_store(&thread_fail, 0);
    119119
     
    128128        }
    129129
    130         while (atomic_load(&thread_count) > 0) {
     130        while (atomic_load(&thread_cnt) > 0) {
    131131                TPRINTF("Threads left: %zu\n",
    132                     atomic_load(&thread_count));
     132                    atomic_load(&thread_cnt));
    133133                thread_sleep(1);
    134134        }
Note: See TracChangeset for help on using the changeset viewer.