Changeset 41bfc64 in mainline


Ignore:
Timestamp:
2024-01-20T17:24:56Z (4 months ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
master
Children:
7364e2d1
Parents:
3d84734
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2024-01-20 16:23:31)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2024-01-20 17:24:56)
Message:

Make thread→state weakly atomic so we don't need to hold thread lock

Location:
kernel/generic
Files:
4 edited

Legend:

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

    r3d84734 r41bfc64  
    135135
    136136        /** Thread state. */
    137         state_t state;
     137        atomic_int_fast32_t state;
    138138
    139139        /** Thread CPU. */
  • kernel/generic/src/proc/scheduler.c

    r3d84734 r41bfc64  
    313313        assert(atomic_get_unordered(&THREAD->cpu) == CPU);
    314314
    315         THREAD->state = Running;
     315        atomic_set_unordered(&THREAD->state, Running);
    316316        atomic_set_unordered(&THREAD->priority, rq_index);  /* Correct rq index */
    317317
     
    386386        irq_spinlock_lock(&thread->lock, false);
    387387
    388         assert(thread->state == Running);
     388        assert(atomic_get_unordered(&thread->state) == Running);
    389389        assert(atomic_get_unordered(&thread->cpu) == CPU);
    390390
     
    396396        }
    397397
    398         thread->state = Ready;
     398        atomic_set_unordered(&thread->state, Ready);
    399399
    400400        irq_spinlock_unlock(&thread->lock, false);
     
    409409        irq_spinlock_lock(&thread->lock, false);
    410410
    411         assert(thread->state == Sleeping || thread->state == Entering);
     411        assert(atomic_get_unordered(&thread->state) == Sleeping || atomic_get_unordered(&thread->state) == Entering);
    412412
    413413        atomic_set_unordered(&thread->priority, 0);
    414         thread->state = Ready;
     414        atomic_set_unordered(&thread->state, Ready);
    415415
    416416        /* Prefer the CPU on which the thread ran last */
     
    471471                 */
    472472                panic("tid%" PRIu64 ": unexpected state %s.",
    473                     thread->tid, thread_states[thread->state]);
     473                    thread->tid, thread_states[out_state]);
    474474                break;
    475475        }
     
    501501
    502502        irq_spinlock_lock(&THREAD->lock, false);
    503         THREAD->state = new_state;
     503
     504        atomic_set_unordered(&THREAD->state, new_state);
    504505
    505506        /* Update thread kernel accounting */
     
    809810                            thread) {
    810811                                printf("%" PRIu64 "(%s) ", thread->tid,
    811                                     thread_states[thread->state]);
     812                                    thread_states[atomic_get_unordered(&thread->state)]);
    812813                        }
    813814                        printf("\n");
  • kernel/generic/src/proc/thread.c

    r3d84734 r41bfc64  
    209209void thread_start(thread_t *thread)
    210210{
    211         assert(thread->state == Entering);
     211        assert(atomic_get_unordered(&thread->state) == Entering);
    212212        thread_requeue_sleeping(thread_ref(thread));
    213213}
     
    269269
    270270        thread->nomigrate = 0;
    271         thread->state = Entering;
     271        atomic_init(&thread->state, Entering);
    272272
    273273        atomic_init(&thread->sleep_queue, NULL);
     
    339339        irq_spinlock_unlock(&thread->task->lock, false);
    340340
    341         assert((thread->state == Exiting) || (thread->state == Lingering));
     341        assert((atomic_get_unordered(&thread->state) == Exiting) || (atomic_get_unordered(&thread->state) == Lingering));
    342342
    343343        /* Clear cpu->fpu_owner if set to this thread. */
     
    637637                return EINVAL;
    638638
    639         irq_spinlock_lock(&thread->lock, true);
    640         state_t state = thread->state;
    641         irq_spinlock_unlock(&thread->lock, true);
     639        state_t state = atomic_get_unordered(&thread->state);
    642640
    643641        errno_t rc = EOK;
     
    686684        order_suffix(thread->ucycles, &ucycles, &usuffix);
    687685        order_suffix(thread->kcycles, &kcycles, &ksuffix);
     686
     687        state_t state = atomic_get_unordered(&thread->state);
    688688
    689689        char *name;
     
    699699        else
    700700                printf("%-8" PRIu64 " %-14s %p %-8s %p %-5" PRIu32 "\n",
    701                     thread->tid, name, thread, thread_states[thread->state],
     701                    thread->tid, name, thread, thread_states[state],
    702702                    thread->task, thread->task->container);
    703703
     
    709709                        printf("none ");
    710710
    711                 if (thread->state == Sleeping) {
     711                if (state == Sleeping) {
    712712                        printf(" %p", thread->sleep_queue);
    713713                }
     
    914914                printf("Scheduling thread stack trace.\n");
    915915                thread->btrace = true;
    916                 if (thread->state == Sleeping)
     916                if (atomic_get_unordered(&thread->state) == Sleeping)
    917917                        sleeping = true;
    918918        } else
  • kernel/generic/src/sysinfo/stats.c

    r3d84734 r41bfc64  
    303303        stats_thread->thread_id = thread->tid;
    304304        stats_thread->task_id = thread->task->taskid;
    305         stats_thread->state = thread->state;
     305        stats_thread->state = atomic_get_unordered(&thread->state);
    306306        stats_thread->priority = atomic_get_unordered(&thread->priority);
    307307        stats_thread->ucycles = thread->ucycles;
Note: See TracChangeset for help on using the changeset viewer.