*/
static inline void mbox_set_invalid(struct mailbox *mbox)
{
- sem_set_invalid(&mbox->prod_sem);
+ if (!!mbox)
+ sem_set_invalid(&mbox->prod_sem);
}
/*
*/
static inline bool mbox_is_valid(struct mailbox *mbox)
{
- return sem_is_valid(&mbox->prod_sem);
+ return ((!!mbox) && sem_is_valid(&mbox->prod_sem));
}
#endif /* _MBOX_H */
*/
static inline void sem_set_invalid(struct semaphore *sem)
{
- sem->list.next = NULL;
+ if (!!sem)
+ sem->list.next = NULL;
}
/*
*/
static inline bool sem_is_valid(struct semaphore *sem)
{
- return !!sem->list.next;
+ return ((!!sem) && (!!sem->list.next));
}
struct thread *start_thread(const char *name, size_t stack_size, int prio,
void sys_mbox_free(sys_mbox_t *mbox)
{
- if (!!mbox && !!*mbox)
+ if (!!mbox && !!*mbox) {
+ sys_mbox_set_invalid(mbox);
free(*mbox);
+ *mbox = NULL;
+ }
}
void sys_mbox_post(sys_mbox_t *mbox, void *msg)
void mbox_init(struct mailbox *mbox, size_t size)
{
- sem_init(&mbox->prod_sem, size); /* All slots empty */
- sem_init(&mbox->cons_sem, 0); /* No slots full */
- sem_init(&mbox->head_sem, 1); /* Head mutex */
- sem_init(&mbox->tail_sem, 1); /* Tail mutex */
-
- mbox->wrap = &mbox->data[size];
- mbox->head = &mbox->data[0];
- mbox->tail = &mbox->data[0];
+ if (!!mbox) {
+ sem_init(&mbox->prod_sem, size); /* All slots empty */
+ sem_init(&mbox->cons_sem, 0); /* No slots full */
+ sem_init(&mbox->head_sem, 1); /* Head mutex */
+ sem_init(&mbox->tail_sem, 1); /* Tail mutex */
+
+ mbox->wrap = &mbox->data[size];
+ mbox->head = &mbox->data[0];
+ mbox->tail = &mbox->data[0];
+ }
};
int mbox_post(struct mailbox *mbox, void *msg, mstime_t timeout)
{
+ if (!mbox)
+ return ENOMEM;
if (sem_down(&mbox->prod_sem, timeout) == (mstime_t)-1)
return ENOMEM;
sem_down(&mbox->head_sem, 0);
{
mstime_t t;
+ if (!mbox)
+ return -1;
t = sem_down(&mbox->cons_sem, timeout);
if (t == (mstime_t)-1)
return -1;
void sem_init(struct semaphore *sem, int count)
{
- sem->list.next = sem->list.prev = &sem->list;
- sem->count = count;
+ if (!!sem) {
+ sem->list.next = sem->list.prev = &sem->list;
+ sem->count = count;
+ }
}
mstime_t __sem_down_slow(struct semaphore *sem, mstime_t timeout)
irq = irq_save();
- if (sem->count >= 0) {
+ if (!sem) {
+ rv = -1;
+ } else if (sem->count >= 0) {
/* Something already freed the semaphore on us */
rv = 0;
} else if (timeout == -1) {
* we don't have to do anything, since the bailout clause in
* __sem_down_slow will take care of it.
*/
- l = sem->list.next;
- if (l != &sem->list) {
- struct thread_block *block;
- block = container_of(l, struct thread_block, list);
+ if (!!sem) {
+ l = sem->list.next;
+ if (l != &sem->list) {
+ struct thread_block *block;
+ block = container_of(l, struct thread_block, list);
- sem->list.next = block->list.next;
- block->list.next->prev = &sem->list;
+ sem->list.next = block->list.next;
+ block->list.next->prev = &sem->list;
- block->thread->blocked = NULL;
+ block->thread->blocked = NULL;
- __schedule();
+ __schedule();
+ }
}
irq_restore(irq);