Skip to content

Commit 57264c5

Browse files
committed
core: Access internal vars via helper funcs
Replace accesses to `sched_active_thread`, `sched_active_pid`, and `sched_threads` with `thread_get_active()`, `thread_get_active_pid()`, and `thread_get_unchecked()` where sensible.
1 parent 4a31578 commit 57264c5

File tree

10 files changed

+81
-86
lines changed

10 files changed

+81
-86
lines changed

core/cond.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void cond_init(cond_t *cond)
3535
void cond_wait(cond_t *cond, mutex_t *mutex)
3636
{
3737
unsigned irqstate = irq_disable();
38-
thread_t *me = (thread_t *)sched_active_thread;
38+
thread_t *me = thread_get_active();
3939

4040
mutex_unlock(mutex);
4141
sched_set_status(me, STATUS_COND_BLOCKED);

core/include/debug.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ extern "C" {
4646
#include "cpu_conf.h"
4747
#define DEBUG_PRINT(...) \
4848
do { \
49-
if ((sched_active_thread == NULL) || \
50-
(sched_active_thread->stack_size >= \
49+
if ((thread_get_active() == NULL) || \
50+
(thread_get_active()->stack_size >= \
5151
THREAD_EXTRA_STACKSIZE_PRINTF)) { \
5252
printf(__VA_ARGS__); \
5353
} \

core/include/thread_flags.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ thread_flags_t thread_flags_clear(thread_flags_t mask);
132132
* immediately, otherwise, it will suspend the thread (as
133133
* THREAD_STATUS_WAIT_ANY) until any of the flags in mask get set.
134134
*
135-
* Both ways, it will clear and return (sched_active_thread-flags & mask).
135+
* Both ways, it will clear and return (`thread_get_active()->flags & mask`).
136136
*
137137
* @param[in] mask mask of flags to wait for
138138
*
139-
* @returns flags that caused return/wakeup ((sched_active_thread-flags & mask).
139+
* @returns flags that caused return/wakeup (`thread_get_active()->flags & mask`).
140140
*/
141141
thread_flags_t thread_flags_wait_any(thread_flags_t mask);
142142

@@ -147,7 +147,7 @@ thread_flags_t thread_flags_wait_any(thread_flags_t mask);
147147
* immediately, otherwise, it will suspend the thread (as
148148
* THREAD_STATUS_WAIT_ALL) until all of the flags in mask have been set.
149149
*
150-
* Both ways, it will clear and return (sched_active_thread-flags & mask).
150+
* Both ways, it will clear and return (`thread_get_active()->flags & mask`).
151151
*
152152
* @param[in] mask mask of flags to wait for
153153
*

core/mbox.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static void _wake_waiter(thread_t *thread, unsigned irqstate)
3333
sched_set_status(thread, STATUS_PENDING);
3434

3535
DEBUG("mbox: Thread %" PRIkernel_pid ": _wake_waiter(): waking up "
36-
"%" PRIkernel_pid ".\n", sched_active_pid, thread->pid);
36+
"%" PRIkernel_pid ".\n", thread_getpid(), thread->pid);
3737

3838
uint16_t process_priority = thread->priority;
3939
irq_restore(irqstate);
@@ -43,16 +43,16 @@ static void _wake_waiter(thread_t *thread, unsigned irqstate)
4343
static void _wait(list_node_t *wait_list, unsigned irqstate)
4444
{
4545
DEBUG("mbox: Thread %" PRIkernel_pid " _wait(): going blocked.\n",
46-
sched_active_pid);
46+
thread_getpid());
4747

48-
thread_t *me = (thread_t *)sched_active_thread;
48+
thread_t *me = thread_get_active();
4949
sched_set_status(me, STATUS_MBOX_BLOCKED);
5050
thread_add_to_list(wait_list, me);
5151
irq_restore(irqstate);
5252
thread_yield();
5353

5454
DEBUG("mbox: Thread %" PRIkernel_pid " _wait(): woke up.\n",
55-
sched_active_pid);
55+
thread_getpid());
5656
}
5757

5858
int _mbox_put(mbox_t *mbox, msg_t *msg, int blocking)
@@ -63,7 +63,7 @@ int _mbox_put(mbox_t *mbox, msg_t *msg, int blocking)
6363

6464
if (next) {
6565
DEBUG("mbox: Thread %" PRIkernel_pid " mbox 0x%08x: _tryput(): "
66-
"there's a waiter.\n", sched_active_pid, (unsigned)mbox);
66+
"there's a waiter.\n", thread_getpid(), (unsigned)mbox);
6767
thread_t *thread =
6868
container_of((clist_node_t *)next, thread_t, rq_entry);
6969
*(msg_t *)thread->wait_data = *msg;
@@ -83,8 +83,8 @@ int _mbox_put(mbox_t *mbox, msg_t *msg, int blocking)
8383
}
8484

8585
DEBUG("mbox: Thread %" PRIkernel_pid " mbox 0x%08x: _tryput(): "
86-
"queued message.\n", sched_active_pid, (unsigned)mbox);
87-
msg->sender_pid = sched_active_pid;
86+
"queued message.\n", thread_getpid(), (unsigned)mbox);
87+
msg->sender_pid = thread_getpid();
8888
/* copy msg into queue */
8989
mbox->msg_array[cib_put_unsafe(&mbox->cib)] = *msg;
9090
irq_restore(irqstate);
@@ -98,7 +98,7 @@ int _mbox_get(mbox_t *mbox, msg_t *msg, int blocking)
9898

9999
if (cib_avail(&mbox->cib)) {
100100
DEBUG("mbox: Thread %" PRIkernel_pid " mbox 0x%08x: _tryget(): "
101-
"got queued message.\n", sched_active_pid, (unsigned)mbox);
101+
"got queued message.\n", thread_getpid(), (unsigned)mbox);
102102
/* copy msg from queue */
103103
*msg = mbox->msg_array[cib_get_unsafe(&mbox->cib)];
104104
list_node_t *next = list_remove_head(&mbox->writers);
@@ -113,7 +113,7 @@ int _mbox_get(mbox_t *mbox, msg_t *msg, int blocking)
113113
return 1;
114114
}
115115
else if (blocking) {
116-
sched_active_thread->wait_data = (void *)msg;
116+
thread_get_active()->wait_data = msg;
117117
_wait(&mbox->readers, irqstate);
118118
/* sender has copied message */
119119
return 1;

core/msg.c

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ int msg_send(msg_t *m, kernel_pid_t target_pid)
6565
if (irq_is_in()) {
6666
return msg_send_int(m, target_pid);
6767
}
68-
if (sched_active_pid == target_pid) {
68+
if (thread_getpid() == target_pid) {
6969
return msg_send_to_self(m);
7070
}
7171
return _msg_send(m, target_pid, true, irq_disable());
@@ -76,7 +76,7 @@ int msg_try_send(msg_t *m, kernel_pid_t target_pid)
7676
if (irq_is_in()) {
7777
return msg_send_int(m, target_pid);
7878
}
79-
if (sched_active_pid == target_pid) {
79+
if (thread_getpid() == target_pid) {
8080
return msg_send_to_self(m);
8181
}
8282
return _msg_send(m, target_pid, false, irq_disable());
@@ -91,21 +91,21 @@ static int _msg_send(msg_t *m, kernel_pid_t target_pid, bool block,
9191
}
9292
#endif /* DEVELHELP */
9393

94-
thread_t *target = (thread_t *)sched_threads[target_pid];
94+
thread_t *target = thread_get_unchecked(target_pid);
9595

96-
m->sender_pid = sched_active_pid;
96+
m->sender_pid = thread_getpid();
9797

9898
if (target == NULL) {
9999
DEBUG("msg_send(): target thread %d does not exist\n", target_pid);
100100
irq_restore(state);
101101
return -1;
102102
}
103103

104-
thread_t *me = (thread_t *)sched_active_thread;
104+
thread_t *me = thread_get_active();
105105

106106
DEBUG("msg_send() %s:%i: Sending from %" PRIkernel_pid " to %" PRIkernel_pid
107107
". block=%i src->state=%i target->state=%i\n", RIOT_FILE_RELATIVE,
108-
__LINE__, sched_active_pid, target_pid,
108+
__LINE__, thread_getpid(), target_pid,
109109
block, me->status, target->status);
110110

111111
if (target->status != STATUS_RECEIVE_BLOCKED) {
@@ -125,17 +125,16 @@ static int _msg_send(msg_t *m, kernel_pid_t target_pid, bool block,
125125
}
126126

127127
if (!block) {
128-
DEBUG(
129-
"msg_send: %" PRIkernel_pid ": Receiver not waiting, block=%u\n",
130-
me->pid, block);
128+
DEBUG("msg_send: %" PRIkernel_pid ": Receiver not waiting, "
129+
"block=%u\n", me->pid, block);
131130
irq_restore(state);
132131
return 0;
133132
}
134133

135134
DEBUG("msg_send: %" PRIkernel_pid ": going send blocked.\n",
136135
me->pid);
137136

138-
me->wait_data = (void *)m;
137+
me->wait_data = m;
139138

140139
int newstatus;
141140

@@ -146,7 +145,7 @@ static int _msg_send(msg_t *m, kernel_pid_t target_pid, bool block,
146145
newstatus = STATUS_SEND_BLOCKED;
147146
}
148147

149-
sched_set_status((thread_t *)me, newstatus);
148+
sched_set_status(me, newstatus);
150149

151150
thread_add_to_list(&(target->msg_waiters), me);
152151

@@ -166,7 +165,7 @@ static int _msg_send(msg_t *m, kernel_pid_t target_pid, bool block,
166165
PRIkernel_pid " to %" PRIkernel_pid ".\n",
167166
me->pid, thread_getpid(), target_pid);
168167
/* copy msg to target */
169-
msg_t *target_message = (msg_t *)target->wait_data;
168+
msg_t *target_message = target->wait_data;
170169
*target_message = *m;
171170
sched_set_status(target, STATUS_PENDING);
172171

@@ -181,8 +180,8 @@ int msg_send_to_self(msg_t *m)
181180
{
182181
unsigned state = irq_disable();
183182

184-
m->sender_pid = sched_active_pid;
185-
int res = queue_msg((thread_t *)sched_active_thread, m);
183+
m->sender_pid = thread_getpid();
184+
int res = queue_msg(thread_get_active(), m);
186185

187186
irq_restore(state);
188187
return res;
@@ -196,7 +195,7 @@ static int _msg_send_oneway(msg_t *m, kernel_pid_t target_pid)
196195
}
197196
#endif /* DEVELHELP */
198197

199-
thread_t *target = (thread_t *)sched_threads[target_pid];
198+
thread_t *target = thread_get_unchecked(target_pid);
200199

201200
if (target == NULL) {
202201
DEBUG("%s: target thread %d does not exist\n", __func__, target_pid);
@@ -242,7 +241,7 @@ int msg_send_bus(msg_t *m, msg_bus_t *bus)
242241
const uint32_t event_mask = (1UL << (m->type & 0x1F));
243242
int count = 0;
244243

245-
m->sender_pid = in_irq ? KERNEL_PID_ISR : sched_active_pid;
244+
m->sender_pid = in_irq ? KERNEL_PID_ISR : thread_getpid();
246245

247246
unsigned state = irq_disable();
248247

@@ -269,11 +268,11 @@ int msg_send_bus(msg_t *m, msg_bus_t *bus)
269268

270269
int msg_send_receive(msg_t *m, msg_t *reply, kernel_pid_t target_pid)
271270
{
272-
assert(sched_active_pid != target_pid);
271+
assert(thread_getpid() != target_pid);
273272
unsigned state = irq_disable();
274-
thread_t *me = (thread_t *)sched_active_thread;
273+
thread_t *me = thread_get_active();
275274
sched_set_status(me, STATUS_REPLY_BLOCKED);
276-
me->wait_data = (void *)reply;
275+
me->wait_data = reply;
277276

278277
/* we re-use (abuse) reply for sending, because wait_data might be
279278
* overwritten if the target is not in RECEIVE_BLOCKED */
@@ -286,20 +285,20 @@ int msg_reply(msg_t *m, msg_t *reply)
286285
{
287286
unsigned state = irq_disable();
288287

289-
thread_t *target = (thread_t *)sched_threads[m->sender_pid];
288+
thread_t *target = thread_get_unchecked(m->sender_pid);
290289

291290
assert(target != NULL);
292291

293292
if (target->status != STATUS_REPLY_BLOCKED) {
294293
DEBUG("msg_reply(): %" PRIkernel_pid ": Target \"%" PRIkernel_pid
295-
"\" not waiting for reply.", sched_active_thread->pid,
294+
"\" not waiting for reply.", thread_getpid(),
296295
target->pid);
297296
irq_restore(state);
298297
return -1;
299298
}
300299

301300
DEBUG("msg_reply(): %" PRIkernel_pid ": Direct msg copy.\n",
302-
sched_active_thread->pid);
301+
thread_getpid());
303302
/* copy msg to target */
304303
msg_t *target_message = (msg_t *)target->wait_data;
305304
*target_message = *reply;
@@ -313,11 +312,11 @@ int msg_reply(msg_t *m, msg_t *reply)
313312

314313
int msg_reply_int(msg_t *m, msg_t *reply)
315314
{
316-
thread_t *target = (thread_t *)sched_threads[m->sender_pid];
315+
thread_t *target = thread_get_unchecked(m->sender_pid);
317316

318317
if (target->status != STATUS_REPLY_BLOCKED) {
319318
DEBUG("msg_reply_int(): %" PRIkernel_pid ": Target \"%" PRIkernel_pid
320-
"\" not waiting for reply.", sched_active_thread->pid,
319+
"\" not waiting for reply.", thread_getpid(),
321320
target->pid);
322321
return -1;
323322
}
@@ -344,9 +343,9 @@ static int _msg_receive(msg_t *m, int block)
344343
unsigned state = irq_disable();
345344

346345
DEBUG("_msg_receive: %" PRIkernel_pid ": _msg_receive.\n",
347-
sched_active_thread->pid);
346+
thread_getpid());
348347

349-
thread_t *me = (thread_t *)sched_active_thread;
348+
thread_t *me = thread_get_active();
350349

351350
int queue_index = -1;
352351

@@ -361,9 +360,8 @@ static int _msg_receive(msg_t *m, int block)
361360
}
362361

363362
if (queue_index >= 0) {
364-
DEBUG(
365-
"_msg_receive: %" PRIkernel_pid ": _msg_receive(): We've got a queued message.\n",
366-
sched_active_thread->pid);
363+
DEBUG("_msg_receive: %" PRIkernel_pid ": _msg_receive(): We've got a "
364+
"queued message.\n", thread_getpid());
367365
*m = me->msg_array[queue_index];
368366
}
369367
else {
@@ -373,21 +371,19 @@ static int _msg_receive(msg_t *m, int block)
373371
list_node_t *next = list_remove_head(&me->msg_waiters);
374372

375373
if (next == NULL) {
376-
DEBUG(
377-
"_msg_receive: %" PRIkernel_pid ": _msg_receive(): No thread in waiting list.\n",
378-
sched_active_thread->pid);
374+
DEBUG("_msg_receive: %" PRIkernel_pid ": _msg_receive(): No thread in "
375+
"waiting list.\n", thread_getpid());
379376

380377
if (queue_index < 0) {
381-
DEBUG(
382-
"_msg_receive(): %" PRIkernel_pid ": No msg in queue. Going blocked.\n",
383-
sched_active_thread->pid);
378+
DEBUG("_msg_receive(): %" PRIkernel_pid ": No msg in queue. Going "
379+
"blocked.\n", thread_getpid());
384380
sched_set_status(me, STATUS_RECEIVE_BLOCKED);
385381

386382
irq_restore(state);
387383
thread_yield_higher();
388384

389385
/* sender copied message */
390-
assert(sched_active_thread->status != STATUS_RECEIVE_BLOCKED);
386+
assert(thread_get_active()->status != STATUS_RECEIVE_BLOCKED);
391387
}
392388
else {
393389
irq_restore(state);
@@ -396,9 +392,8 @@ static int _msg_receive(msg_t *m, int block)
396392
return 1;
397393
}
398394
else {
399-
DEBUG(
400-
"_msg_receive: %" PRIkernel_pid ": _msg_receive(): Waking up waiting thread.\n",
401-
sched_active_thread->pid);
395+
DEBUG("_msg_receive: %" PRIkernel_pid ": _msg_receive(): Waking up "
396+
"waiting thread.\n", thread_getpid());
402397

403398
thread_t *sender =
404399
container_of((clist_node_t *)next, thread_t, rq_entry);
@@ -435,9 +430,9 @@ static int _msg_receive(msg_t *m, int block)
435430
int msg_avail(void)
436431
{
437432
DEBUG("msg_available: %" PRIkernel_pid ": msg_available.\n",
438-
sched_active_thread->pid);
433+
thread_getpid());
439434

440-
thread_t *me = (thread_t *)sched_active_thread;
435+
thread_t *me = thread_get_active();
441436

442437
int queue_index = -1;
443438

@@ -450,7 +445,7 @@ int msg_avail(void)
450445

451446
void msg_init_queue(msg_t *array, int num)
452447
{
453-
thread_t *me = (thread_t *)sched_active_thread;
448+
thread_t *me = thread_get_active();
454449

455450
me->msg_array = array;
456451
cib_init(&(me->msg_queue), num);
@@ -460,7 +455,7 @@ void msg_queue_print(void)
460455
{
461456
unsigned state = irq_disable();
462457

463-
thread_t *thread = (thread_t *)sched_active_thread;
458+
thread_t *thread = thread_get_active();
464459
cib_t *msg_queue = &thread->msg_queue;
465460
msg_t *msg_array = thread->msg_array;
466461
unsigned int i = msg_queue->read_count & msg_queue->mask;

core/msg_bus.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void msg_bus_attach(msg_bus_t *bus, msg_bus_entry_t *entry)
3737

3838
entry->next.next = NULL;
3939
entry->event_mask = 0;
40-
entry->pid = sched_active_pid;
40+
entry->pid = thread_getpid();
4141

4242
state = irq_disable();
4343
list_add(&bus->subs, &entry->next);
@@ -62,7 +62,7 @@ msg_bus_entry_t *msg_bus_get_entry(msg_bus_t *bus)
6262

6363
msg_bus_entry_t *subscriber = container_of(e, msg_bus_entry_t, next);
6464

65-
if (subscriber->pid == sched_active_pid) {
65+
if (subscriber->pid == thread_getpid()) {
6666
s = subscriber;
6767
break;
6868
}

0 commit comments

Comments
 (0)