Skip to content

Commit caa5561

Browse files
maribumguetschow
andcommitted
core/msg: fix msg_avail_thread() and msg_avail()
This now ensures race-free access to the CIB tracking the number of messages queued in the ringbuffer for a given thread. In addition, `msg_avail_thread()` now checks if the provided pid refers to a thread that is currently existing. Co-authored-by: Mikolai Gütschow <mikolai.guetschow@tu-dresden.de>
1 parent be88208 commit caa5561

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

core/include/msg.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,16 +375,21 @@ int msg_reply_int(msg_t *m, msg_t *reply);
375375
*
376376
* @param[in] pid a PID
377377
*
378-
* @return Number of messages available in queue of @p pid on success
379-
* @return 0, if no caller's message queue is initialized
378+
* @return Number of messages available in queue of the thread identified by
379+
* PID @p pid
380+
* @retval 0 The message queue of the thread identified by PID @p pid is
381+
* *not* initialized or PID @p pid does not refer to a running
382+
* thread
380383
*/
381384
unsigned msg_avail_thread(kernel_pid_t pid);
382385

383386
/**
384387
* @brief Check how many messages are available (waiting) in the message queue
385388
*
386-
* @return Number of messages available in our queue on success
387-
* @return 0, if no caller's message queue is initialized
389+
* @pre The caller is running in thread context
390+
*
391+
* @return Number of messages available in our queue
392+
* @retval 0 Caller's message queue is *not* initialized
388393
*/
389394
unsigned msg_avail(void);
390395

core/msg.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,12 +458,23 @@ static unsigned _msg_avail(thread_t *thread)
458458

459459
unsigned msg_avail_thread(kernel_pid_t pid)
460460
{
461-
return _msg_avail(thread_get(pid));
461+
unsigned irq_state = irq_disable();
462+
thread_t *t = thread_get(pid);
463+
if (!t) {
464+
irq_restore(irq_state);
465+
return 0;
466+
}
467+
unsigned result = _msg_avail(t);
468+
irq_restore(irq_state);
469+
return result;
462470
}
463471

464472
unsigned msg_avail(void)
465473
{
466-
return _msg_avail(thread_get_active());
474+
unsigned irq_state = irq_disable();
475+
unsigned result = _msg_avail(thread_get_active());
476+
irq_restore(irq_state);
477+
return result;
467478
}
468479

469480
unsigned msg_queue_capacity(kernel_pid_t pid)

0 commit comments

Comments
 (0)