Skip to content

Commit 0c4870a

Browse files
ycsincarlescufi
authored andcommitted
lib: posix: mutex: implement pthread_mutexattr_setprotocol
Implement and test `pthread_mutexattr_setprotocol()`. Signed-off-by: Yong Cong Sin <[email protected]>
1 parent 50f47a4 commit 0c4870a

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

include/zephyr/posix/pthread.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ int pthread_condattr_setclock(pthread_condattr_t *att, clockid_t clock_id);
183183
* FIXME: Only PRIO_NONE is supported. Implement other protocols.
184184
*/
185185
#define PTHREAD_PRIO_NONE 0
186+
#define PTHREAD_PRIO_INHERIT 1
187+
#define PTHREAD_PRIO_PROTECT 2
186188

187189
/**
188190
* @brief POSIX threading compatibility API

lib/posix/options/mutex.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,29 @@ int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr,
315315
return 0;
316316
}
317317

318+
/**
319+
* @brief Set protocol attribute for mutex.
320+
*
321+
* See IEEE 1003.1
322+
*/
323+
int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol)
324+
{
325+
if (attr == NULL) {
326+
return EINVAL;
327+
}
328+
329+
switch (protocol) {
330+
case PTHREAD_PRIO_NONE:
331+
return 0;
332+
case PTHREAD_PRIO_INHERIT:
333+
return ENOTSUP;
334+
case PTHREAD_PRIO_PROTECT:
335+
return ENOTSUP;
336+
default:
337+
return EINVAL;
338+
}
339+
}
340+
318341
int pthread_mutexattr_init(pthread_mutexattr_t *attr)
319342
{
320343
struct pthread_mutexattr *const a = (struct pthread_mutexattr *)attr;

tests/posix/common/src/mutex.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ static void test_mutex_common(int type, void *(*entry)(void *arg))
6363
zassert_not_ok(pthread_mutexattr_getprotocol(NULL, &protocol));
6464
zassert_not_ok(pthread_mutexattr_getprotocol(&mut_attr, NULL));
6565
zassert_not_ok(pthread_mutexattr_getprotocol(NULL, NULL));
66+
67+
zassert_not_ok(pthread_mutexattr_setprotocol(&mut_attr, PTHREAD_PRIO_INHERIT));
68+
zassert_not_ok(pthread_mutexattr_setprotocol(&mut_attr, PTHREAD_PRIO_PROTECT));
69+
zassert_ok(pthread_mutexattr_setprotocol(&mut_attr, PTHREAD_PRIO_NONE));
6670
zassert_ok(pthread_mutexattr_getprotocol(&mut_attr, &protocol),
6771
"reading mutex protocol is failed");
6872
zassert_ok(pthread_mutexattr_destroy(&mut_attr));

tests/posix/headers/src/pthread_h.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ ZTEST(posix_headers, test_pthread_h)
128128
zassert_not_null(pthread_mutexattr_gettype);
129129
zassert_not_null(pthread_mutexattr_init);
130130
/* zassert_not_null(pthread_mutexattr_setprioceiling); */ /* not implemented */
131-
/* zassert_not_null(pthread_mutexattr_setprotocol); */ /* not implemented */
131+
zassert_not_null(pthread_mutexattr_setprotocol);
132132
/* zassert_not_null(pthread_mutexattr_setpshared); */ /* not implemented */
133133
/* zassert_not_null(pthread_mutexattr_setrobust); */ /* not implemented */
134134
zassert_not_null(pthread_mutexattr_settype);

0 commit comments

Comments
 (0)