Skip to content

Commit c001d37

Browse files
elupushefloryd
authored andcommitted
feat(linux): always assert that calls succeed
This matches behavior on rt-kernel and freertos. Our stacks are not expected to be able to handle the case these functions failing.
1 parent 63fedeb commit c001d37

1 file changed

Lines changed: 21 additions & 52 deletions

File tree

src/linux/osal.c

Lines changed: 21 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ os_thread_t * os_thread_create (
6161
pthread_attr_t attr;
6262

6363
thread = malloc (sizeof (*thread));
64-
if (thread == NULL)
65-
{
66-
return NULL;
67-
}
64+
CC_ASSERT (thread != 0);
6865

6966
pthread_attr_init (&attr);
7067
pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN + stacksize);
@@ -78,11 +75,7 @@ os_thread_t * os_thread_create (
7875
#endif
7976

8077
result = pthread_create (thread, &attr, (void *)entry, arg);
81-
if (result != 0)
82-
{
83-
free (thread);
84-
return NULL;
85-
}
78+
CC_ASSERT (result == 0);
8679

8780
pthread_setname_np (*thread, name);
8881
return thread;
@@ -95,22 +88,15 @@ os_mutex_t * os_mutex_create (void)
9588
pthread_mutexattr_t mattr;
9689

9790
mutex = malloc (sizeof (*mutex));
98-
if (mutex == NULL)
99-
{
100-
return NULL;
101-
}
91+
CC_ASSERT (mutex != NULL);
10292

10393
CC_STATIC_ASSERT (_POSIX_THREAD_PRIO_INHERIT > 0);
10494
pthread_mutexattr_init (&mattr);
10595
pthread_mutexattr_setprotocol (&mattr, PTHREAD_PRIO_INHERIT);
10696
pthread_mutexattr_settype (&mattr, PTHREAD_MUTEX_RECURSIVE);
10797

10898
result = pthread_mutex_init (mutex, &mattr);
109-
if (result != 0)
110-
{
111-
free (mutex);
112-
return NULL;
113-
}
99+
CC_ASSERT (result == 0);
114100

115101
return mutex;
116102
}
@@ -141,10 +127,7 @@ os_sem_t * os_sem_create (size_t count)
141127
pthread_condattr_t cattr;
142128

143129
sem = malloc (sizeof (*sem));
144-
if (sem == NULL)
145-
{
146-
return NULL;
147-
}
130+
CC_ASSERT (sem != NULL);
148131

149132
pthread_condattr_init (&cattr);
150133
pthread_condattr_setclock (&cattr, CLOCK_MONOTONIC);
@@ -178,7 +161,7 @@ bool os_sem_wait (os_sem_t * sem, uint32_t time)
178161
if (time != OS_WAIT_FOREVER)
179162
{
180163
error = pthread_cond_timedwait (&sem->cond, &sem->mutex, &ts);
181-
assert (error != EINVAL);
164+
CC_ASSERT (error != EINVAL);
182165
if (error)
183166
{
184167
goto timeout;
@@ -187,7 +170,7 @@ bool os_sem_wait (os_sem_t * sem, uint32_t time)
187170
else
188171
{
189172
error = pthread_cond_wait (&sem->cond, &sem->mutex);
190-
assert (error != EINVAL);
173+
CC_ASSERT (error != EINVAL);
191174
}
192175
}
193176

@@ -271,10 +254,7 @@ os_event_t * os_event_create (void)
271254
pthread_condattr_t cattr;
272255

273256
event = (os_event_t *)malloc (sizeof (*event));
274-
if (event == NULL)
275-
{
276-
return NULL;
277-
}
257+
CC_ASSERT (event != NULL);
278258

279259
pthread_condattr_init (&cattr);
280260
pthread_condattr_setclock (&cattr, CLOCK_MONOTONIC);
@@ -309,7 +289,7 @@ bool os_event_wait (os_event_t * event, uint32_t mask, uint32_t * value, uint32_
309289
if (time != OS_WAIT_FOREVER)
310290
{
311291
error = pthread_cond_timedwait (&event->cond, &event->mutex, &ts);
312-
assert (error != EINVAL);
292+
CC_ASSERT (error != EINVAL);
313293
if (error)
314294
{
315295
goto timeout;
@@ -318,7 +298,7 @@ bool os_event_wait (os_event_t * event, uint32_t mask, uint32_t * value, uint32_
318298
else
319299
{
320300
error = pthread_cond_wait (&event->cond, &event->mutex);
321-
assert (error != EINVAL);
301+
CC_ASSERT (error != EINVAL);
322302
}
323303
}
324304

@@ -358,10 +338,7 @@ os_mbox_t * os_mbox_create (size_t size)
358338
pthread_condattr_t cattr;
359339

360340
mbox = (os_mbox_t *)malloc (sizeof (*mbox) + size * sizeof (void *));
361-
if (mbox == NULL)
362-
{
363-
return NULL;
364-
}
341+
CC_ASSERT (mbox != NULL);
365342

366343
pthread_condattr_init (&cattr);
367344
pthread_condattr_setclock (&cattr, CLOCK_MONOTONIC);
@@ -400,7 +377,7 @@ bool os_mbox_fetch (os_mbox_t * mbox, void ** msg, uint32_t time)
400377
if (time != OS_WAIT_FOREVER)
401378
{
402379
error = pthread_cond_timedwait (&mbox->cond, &mbox->mutex, &ts);
403-
assert (error != EINVAL);
380+
CC_ASSERT (error != EINVAL);
404381
if (error)
405382
{
406383
goto timeout;
@@ -409,7 +386,7 @@ bool os_mbox_fetch (os_mbox_t * mbox, void ** msg, uint32_t time)
409386
else
410387
{
411388
error = pthread_cond_wait (&mbox->cond, &mbox->mutex);
412-
assert (error != EINVAL);
389+
CC_ASSERT (error != EINVAL);
413390
}
414391
}
415392

@@ -448,7 +425,7 @@ bool os_mbox_post (os_mbox_t * mbox, void * msg, uint32_t time)
448425
if (time != OS_WAIT_FOREVER)
449426
{
450427
error = pthread_cond_timedwait (&mbox->cond, &mbox->mutex, &ts);
451-
assert (error != EINVAL);
428+
CC_ASSERT (error != EINVAL);
452429
if (error)
453430
{
454431
goto timeout;
@@ -457,7 +434,7 @@ bool os_mbox_post (os_mbox_t * mbox, void * msg, uint32_t time)
457434
else
458435
{
459436
error = pthread_cond_wait (&mbox->cond, &mbox->mutex);
460-
assert (error != EINVAL);
437+
CC_ASSERT (error != EINVAL);
461438
}
462439
}
463440

@@ -518,17 +495,15 @@ os_timer_t * os_timer_create (
518495
os_timer_t * timer;
519496
struct sigevent sev;
520497
sigset_t sigset;
498+
int res;
521499

522500
/* Block SIGALRM in calling thread */
523501
sigemptyset (&sigset);
524502
sigaddset (&sigset, SIGALRM);
525503
sigprocmask (SIG_BLOCK, &sigset, NULL);
526504

527505
timer = (os_timer_t *)malloc (sizeof (*timer));
528-
if (timer == NULL)
529-
{
530-
return NULL;
531-
}
506+
CC_ASSERT (timer != NULL);
532507

533508
timer->exit = false;
534509
timer->thread_id = 0;
@@ -540,11 +515,8 @@ os_timer_t * os_timer_create (
540515
/* Create timer thread */
541516
timer->thread =
542517
os_thread_create ("os_timer", TIMER_PRIO, 1024, os_timer_thread, timer);
543-
if (timer->thread == NULL)
544-
{
545-
free (timer);
546-
return NULL;
547-
}
518+
CC_ASSERT (timer->thread != NULL);
519+
548520

549521
/* Wait until timer thread sets its (kernel) thread id */
550522
do
@@ -559,11 +531,8 @@ os_timer_t * os_timer_create (
559531
sev.sigev_signo = SIGALRM;
560532
sev.sigev_notify_attributes = NULL;
561533

562-
if (timer_create (CLOCK_MONOTONIC, &sev, &timer->timerid) == -1)
563-
{
564-
free (timer);
565-
return NULL;
566-
}
534+
res = timer_create (CLOCK_MONOTONIC, &sev, &timer->timerid);
535+
CC_ASSERT (res != -1);
567536

568537
return timer;
569538
}

0 commit comments

Comments
 (0)