lib: posix: mutex: implement pthread_mutexattr_setprotocol

Implement and test `pthread_mutexattr_setprotocol()`.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
This commit is contained in:
Yong Cong Sin 2024-06-17 14:59:07 +08:00 committed by Carles Cufí
parent 50f47a44b7
commit 0c4870a0f0
4 changed files with 30 additions and 1 deletions

View File

@ -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

View File

@ -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;

View File

@ -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));

View File

@ -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);