Skip to content

Commit 301581a

Browse files
Christopher Friedtcfriedt
authored andcommitted
posix: pthread: wrapper to check attribute initialization
Add a small wrapper to check if a pthread_attr_t has been properly initialized (i.e. ready to pass to pthread_create()). Signed-off-by: Christopher Friedt <[email protected]>
1 parent 9f3d777 commit 301581a

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

lib/posix/pthread.c

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ int posix_to_zephyr_priority(int priority, int policy)
253253
return POSIX_TO_ZEPHYR_PRIORITY(priority, policy);
254254
}
255255

256+
static bool __attr_is_initialized(const struct posix_thread_attr *attr)
257+
{
258+
return attr != NULL && attr->initialized;
259+
}
260+
256261
/**
257262
* @brief Set scheduling parameter attributes in thread attributes object.
258263
*
@@ -262,7 +267,7 @@ int pthread_attr_setschedparam(pthread_attr_t *_attr, const struct sched_param *
262267
{
263268
struct posix_thread_attr *attr = (struct posix_thread_attr *)_attr;
264269

265-
if (attr == NULL || !attr->initialized || schedparam == NULL ||
270+
if (!__attr_is_initialized(attr) || schedparam == NULL ||
266271
!is_posix_policy_prio_valid(schedparam->sched_priority, attr->schedpolicy)) {
267272
LOG_ERR("Invalid pthread_attr_t or sched_param");
268273
return EINVAL;
@@ -286,7 +291,8 @@ int pthread_attr_setstack(pthread_attr_t *_attr, void *stackaddr, size_t stacksi
286291
return EACCES;
287292
}
288293

289-
if (stacksize == 0 || stacksize < PTHREAD_STACK_MIN || stacksize > PTHREAD_STACK_MAX) {
294+
if (!__attr_is_initialized(attr) || stacksize < PTHREAD_STACK_MIN ||
295+
stacksize > PTHREAD_STACK_MAX) {
290296
LOG_ERR("Invalid stacksize %zu", stacksize);
291297
return EINVAL;
292298
}
@@ -298,15 +304,22 @@ int pthread_attr_setstack(pthread_attr_t *_attr, void *stackaddr, size_t stacksi
298304

299305
static bool pthread_attr_is_valid(const struct posix_thread_attr *attr)
300306
{
307+
size_t stacksize;
308+
301309
/* auto-alloc thread stack */
302310
if (attr == NULL) {
303311
return true;
304312
}
305313

306-
/* caller-provided thread stack */
307-
if (!attr->initialized || attr->stack == NULL || attr->stacksize == 0 ||
308-
__get_attr_stacksize(attr) < PTHREAD_STACK_MIN) {
309-
LOG_ERR("pthread_attr_t is not initialized, has a NULL stack, or invalid size");
314+
if (!__attr_is_initialized(attr) || attr->stack == NULL) {
315+
LOG_DBG("attr %p is not initialized", attr);
316+
return false;
317+
}
318+
319+
stacksize = __get_attr_stacksize(attr);
320+
if (stacksize < PTHREAD_STACK_MIN) {
321+
LOG_DBG("attr %p has stacksize %zu is smaller than PTHREAD_STACK_MIN (%zu)", attr,
322+
stacksize, (size_t)PTHREAD_STACK_MIN);
310323
return false;
311324
}
312325

@@ -888,7 +901,7 @@ int pthread_attr_getdetachstate(const pthread_attr_t *_attr, int *detachstate)
888901
{
889902
const struct posix_thread_attr *attr = (const struct posix_thread_attr *)_attr;
890903

891-
if ((attr == NULL) || (attr->initialized == false) || (detachstate == NULL)) {
904+
if (!__attr_is_initialized(attr) || (detachstate == NULL)) {
892905
return EINVAL;
893906
}
894907

@@ -905,9 +918,8 @@ int pthread_attr_setdetachstate(pthread_attr_t *_attr, int detachstate)
905918
{
906919
struct posix_thread_attr *attr = (struct posix_thread_attr *)_attr;
907920

908-
if ((attr == NULL) || (attr->initialized == false) ||
909-
((detachstate != PTHREAD_CREATE_DETACHED) &&
910-
(detachstate != PTHREAD_CREATE_JOINABLE))) {
921+
if (!__attr_is_initialized(attr) || ((detachstate != PTHREAD_CREATE_DETACHED) &&
922+
(detachstate != PTHREAD_CREATE_JOINABLE))) {
911923
return EINVAL;
912924
}
913925

@@ -924,7 +936,7 @@ int pthread_attr_getschedpolicy(const pthread_attr_t *_attr, int *policy)
924936
{
925937
const struct posix_thread_attr *attr = (const struct posix_thread_attr *)_attr;
926938

927-
if ((attr == NULL) || (attr->initialized == 0U) || (policy == NULL)) {
939+
if (!__attr_is_initialized(attr) || (policy == NULL)) {
928940
return EINVAL;
929941
}
930942

@@ -941,7 +953,7 @@ int pthread_attr_setschedpolicy(pthread_attr_t *_attr, int policy)
941953
{
942954
struct posix_thread_attr *attr = (struct posix_thread_attr *)_attr;
943955

944-
if ((attr == NULL) || (attr->initialized == 0U) || !valid_posix_policy(policy)) {
956+
if (!__attr_is_initialized(attr) || !valid_posix_policy(policy)) {
945957
return EINVAL;
946958
}
947959

@@ -958,7 +970,7 @@ int pthread_attr_getstacksize(const pthread_attr_t *_attr, size_t *stacksize)
958970
{
959971
const struct posix_thread_attr *attr = (const struct posix_thread_attr *)_attr;
960972

961-
if ((attr == NULL) || (attr->initialized == false) || (stacksize == NULL)) {
973+
if (!__attr_is_initialized(attr) || (stacksize == NULL)) {
962974
return EINVAL;
963975
}
964976

@@ -973,13 +985,11 @@ int pthread_attr_getstacksize(const pthread_attr_t *_attr, size_t *stacksize)
973985
*/
974986
int pthread_attr_setstacksize(pthread_attr_t *_attr, size_t stacksize)
975987
{
988+
int ret;
976989
struct posix_thread_attr *attr = (struct posix_thread_attr *)_attr;
977990

978-
if ((attr == NULL) || (attr->initialized == 0U)) {
979-
return EINVAL;
980-
}
981-
982-
if (stacksize == 0 || stacksize < PTHREAD_STACK_MIN || stacksize > PTHREAD_STACK_MAX) {
991+
if (!__attr_is_initialized(attr) || stacksize < PTHREAD_STACK_MIN ||
992+
stacksize > PTHREAD_STACK_MAX) {
983993
return EINVAL;
984994
}
985995

@@ -996,8 +1006,7 @@ int pthread_attr_getstack(const pthread_attr_t *_attr, void **stackaddr, size_t
9961006
{
9971007
const struct posix_thread_attr *attr = (const struct posix_thread_attr *)_attr;
9981008

999-
if ((attr == NULL) || (attr->initialized == false) || (stackaddr == NULL) ||
1000-
(stacksize == NULL)) {
1009+
if (!__attr_is_initialized(attr) || (stackaddr == NULL) || (stacksize == NULL)) {
10011010
return EINVAL;
10021011
}
10031012

@@ -1010,7 +1019,7 @@ int pthread_attr_getguardsize(const pthread_attr_t *ZRESTRICT _attr, size_t *ZRE
10101019
{
10111020
struct posix_thread_attr *const attr = (struct posix_thread_attr *)_attr;
10121021

1013-
if (attr == NULL || guardsize == NULL || !attr->initialized) {
1022+
if (!__attr_is_initialized(attr) || guardsize == NULL) {
10141023
return EINVAL;
10151024
}
10161025

@@ -1023,7 +1032,7 @@ int pthread_attr_setguardsize(pthread_attr_t *_attr, size_t guardsize)
10231032
{
10241033
struct posix_thread_attr *const attr = (struct posix_thread_attr *)_attr;
10251034

1026-
if (attr == NULL || !attr->initialized || guardsize > PTHREAD_GUARD_MAX) {
1035+
if (!__attr_is_initialized(attr) || guardsize > PTHREAD_GUARD_MAX) {
10271036
return EINVAL;
10281037
}
10291038

@@ -1041,7 +1050,7 @@ int pthread_attr_getschedparam(const pthread_attr_t *_attr, struct sched_param *
10411050
{
10421051
struct posix_thread_attr *attr = (struct posix_thread_attr *)_attr;
10431052

1044-
if ((attr == NULL) || (attr->initialized == false) || (schedparam == NULL)) {
1053+
if (!__attr_is_initialized(attr) || (schedparam == NULL)) {
10451054
return EINVAL;
10461055
}
10471056

@@ -1058,12 +1067,11 @@ int pthread_attr_destroy(pthread_attr_t *_attr)
10581067
{
10591068
struct posix_thread_attr *attr = (struct posix_thread_attr *)_attr;
10601069

1061-
if ((attr != NULL) && (attr->initialized != 0U)) {
1062-
attr->initialized = false;
1063-
return 0;
1070+
if (!__attr_is_initialized(attr)) {
1071+
return EINVAL;
10641072
}
10651073

1066-
return EINVAL;
1074+
return 0;
10671075
}
10681076

10691077
int pthread_setname_np(pthread_t thread, const char *name)

0 commit comments

Comments
 (0)