Skip to content

Commit

Permalink
posix: newlib compatible PTHREAD_CREATE_DETACHED and JOINABLE
Browse files Browse the repository at this point in the history
Define `PTHREAD_CREATE_DETACHED` and
`PTHREAD_CREATE_JOINABLE` to be compatible with the Newlib
definitions.

This is a temporary workaround for zephyrproject-rtos#51211 until Newlib
headers are pulled in.

Signed-off-by: Chris Friedt <[email protected]>
  • Loading branch information
cfriedt committed Nov 21, 2022
1 parent 8286791 commit 8de18b1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions include/zephyr/posix/pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ extern "C" {
#endif

/* Pthread detach/joinable */
#define PTHREAD_CREATE_JOINABLE 1
#define PTHREAD_CREATE_DETACHED 2
#define PTHREAD_CREATE_DETACHED 0
#define PTHREAD_CREATE_JOINABLE 1

/* Pthread cancellation */
#define _PTHREAD_CANCEL_POS 0
Expand Down
8 changes: 4 additions & 4 deletions lib/posix/posix_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ struct posix_cond {
};

enum pthread_state {
/* The thread structure is unallocated and available for reuse. */
PTHREAD_TERMINATED = 0,
/* The thread is running and joinable. */
PTHREAD_JOINABLE = PTHREAD_CREATE_JOINABLE,
/* The thread is running and detached. */
PTHREAD_DETACHED = PTHREAD_CREATE_DETACHED,
/* The thread is running and joinable. */
PTHREAD_JOINABLE = PTHREAD_CREATE_JOINABLE,
/* The thread structure is unallocated and available for reuse. */
PTHREAD_TERMINATED,
/* A joinable thread exited and its return code is available. */
PTHREAD_EXITED
};
Expand Down
16 changes: 16 additions & 0 deletions lib/posix/pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/init.h>
#include <zephyr/kernel.h>
#include <stdio.h>
#include <zephyr/sys/atomic.h>
Expand Down Expand Up @@ -690,3 +691,18 @@ int pthread_getname_np(pthread_t thread, char *name, size_t len)
return 0;
#endif
}

static int posix_thread_pool_init(const struct device *dev)
{
size_t i;

ARG_UNUSED(dev);

for (i = 0; i < CONFIG_MAX_PTHREAD_COUNT; ++i) {
posix_thread_pool[i].state = PTHREAD_EXITED;
}

return 0;
}

SYS_INIT(posix_thread_pool_init, PRE_KERNEL_1, 0);
4 changes: 2 additions & 2 deletions tests/posix/common/src/pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,9 @@ ZTEST(posix_apis, test_posix_pthread_error_condition)
"set scheduling policy error");
zassert_equal(pthread_attr_setschedpolicy(&attr, 2),
EINVAL, "set scheduling policy error");
zassert_false(pthread_attr_setdetachstate(&attr, 1),
zassert_false(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE),
"set detach state error");
zassert_false(pthread_attr_setdetachstate(&attr, 2),
zassert_false(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED),
"set detach state error");
zassert_equal(pthread_attr_setdetachstate(&attr, 3),
EINVAL, "set detach state error");
Expand Down

0 comments on commit 8de18b1

Please sign in to comment.