diff --git a/include/zephyr/posix/pthread.h b/include/zephyr/posix/pthread.h index eaa9ccb66b0..eb640a511fd 100644 --- a/include/zephyr/posix/pthread.h +++ b/include/zephyr/posix/pthread.h @@ -183,6 +183,8 @@ int pthread_condattr_setclock(pthread_condattr_t *att, clockid_t clock_id); * FIXME: Only PRIO_NONE is supported. Implement other protocols. */ #define PTHREAD_PRIO_NONE 0 +#define PTHREAD_PRIO_INHERIT 1 +#define PTHREAD_PRIO_PROTECT 2 /** * @brief POSIX threading compatibility API diff --git a/lib/posix/options/mutex.c b/lib/posix/options/mutex.c index 7e4599016da..a1c057b6cb2 100644 --- a/lib/posix/options/mutex.c +++ b/lib/posix/options/mutex.c @@ -315,6 +315,29 @@ int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr, return 0; } +/** + * @brief Set protocol attribute for mutex. + * + * See IEEE 1003.1 + */ +int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol) +{ + if (attr == NULL) { + return EINVAL; + } + + switch (protocol) { + case PTHREAD_PRIO_NONE: + return 0; + case PTHREAD_PRIO_INHERIT: + return ENOTSUP; + case PTHREAD_PRIO_PROTECT: + return ENOTSUP; + default: + return EINVAL; + } +} + int pthread_mutexattr_init(pthread_mutexattr_t *attr) { struct pthread_mutexattr *const a = (struct pthread_mutexattr *)attr; diff --git a/tests/posix/common/src/mutex.c b/tests/posix/common/src/mutex.c index 0fe24f89888..60172a3c2f6 100644 --- a/tests/posix/common/src/mutex.c +++ b/tests/posix/common/src/mutex.c @@ -63,6 +63,10 @@ static void test_mutex_common(int type, void *(*entry)(void *arg)) zassert_not_ok(pthread_mutexattr_getprotocol(NULL, &protocol)); zassert_not_ok(pthread_mutexattr_getprotocol(&mut_attr, NULL)); zassert_not_ok(pthread_mutexattr_getprotocol(NULL, NULL)); + + zassert_not_ok(pthread_mutexattr_setprotocol(&mut_attr, PTHREAD_PRIO_INHERIT)); + zassert_not_ok(pthread_mutexattr_setprotocol(&mut_attr, PTHREAD_PRIO_PROTECT)); + zassert_ok(pthread_mutexattr_setprotocol(&mut_attr, PTHREAD_PRIO_NONE)); zassert_ok(pthread_mutexattr_getprotocol(&mut_attr, &protocol), "reading mutex protocol is failed"); zassert_ok(pthread_mutexattr_destroy(&mut_attr)); diff --git a/tests/posix/headers/src/pthread_h.c b/tests/posix/headers/src/pthread_h.c index 8f1286222d5..df6a57066fe 100644 --- a/tests/posix/headers/src/pthread_h.c +++ b/tests/posix/headers/src/pthread_h.c @@ -128,7 +128,7 @@ ZTEST(posix_headers, test_pthread_h) zassert_not_null(pthread_mutexattr_gettype); zassert_not_null(pthread_mutexattr_init); /* zassert_not_null(pthread_mutexattr_setprioceiling); */ /* not implemented */ - /* zassert_not_null(pthread_mutexattr_setprotocol); */ /* not implemented */ + zassert_not_null(pthread_mutexattr_setprotocol); /* zassert_not_null(pthread_mutexattr_setpshared); */ /* not implemented */ /* zassert_not_null(pthread_mutexattr_setrobust); */ /* not implemented */ zassert_not_null(pthread_mutexattr_settype);