Skip to content

Commit c8aa657

Browse files
youvedeep-singhAnas Nashif
authored andcommitted
kernel: POSIX: Compatibility layer for pthread APIs.
This patch provides pthread APIs for POSIX 1003.1 PSE52 standard. Signed-off-by: Youvedeep Singh <[email protected]>
1 parent abc94b8 commit c8aa657

File tree

8 files changed

+731
-6
lines changed

8 files changed

+731
-6
lines changed

arch/posix/include/posix_cheats.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
#define pthread_condattr_t zap_pthread_condattr_t
3131
#define pthread_barrier_t zap_pthread_barrier_t
3232
#define pthread_barrierattr_t zap_pthread_barrierattr_t
33+
#define pthread_attr_t zap_pthread_attr_t
3334

35+
/* Condition variables */
3436
#define pthread_cond_init(...) zap_pthread_cond_init(__VA_ARGS__)
3537
#define pthread_cond_destroy(...) zap_pthread_cond_destroy(__VA_ARGS__)
3638
#define pthread_cond_signal(...) zap_pthread_cond_signal(__VA_ARGS__)
@@ -39,6 +41,7 @@
3941
#define pthread_cond_timedwait(...) zap_pthread_cond_timedwait(__VA_ARGS__)
4042
#define pthread_condattr_init(...) zap_pthread_condattr_init(__VA_ARGS__)
4143
#define pthread_condattr_destroy(...) zap_pthread_condattr_destroy(__VA_ARGS__)
44+
/* Mutex */
4245
#define pthread_mutex_init(...) zap_pthread_mutex_init(__VA_ARGS__)
4346
#define pthread_mutex_destroy(...) zap_pthread_mutex_destroy(__VA_ARGS__)
4447
#define pthread_mutex_lock(...) zap_pthread_mutex_lock(__VA_ARGS__)
@@ -48,13 +51,45 @@
4851
#define pthread_mutexattr_init(...) zap_pthread_mutexattr_init(__VA_ARGS__)
4952
#define pthread_mutexattr_destroy(...) \
5053
zap_pthread_mutexattr_destroy(__VA_ARGS__)
54+
/* Barrier */
5155
#define pthread_barrier_wait(...) zap_pthread_barrier_wait(__VA_ARGS__)
5256
#define pthread_barrier_init(...) zap_pthread_barrier_init(__VA_ARGS__)
5357
#define pthread_barrier_destroy(...) zap_pthread_barrier_destroy(__VA_ARGS__)
5458
#define pthread_barrierattr_init(...) zap_pthread_barrierattr_init(__VA_ARGS__)
5559
#define pthread_barrierattr_destroy(...) \
5660
zap_pthread_barrierattr_destroy(__VA_ARGS__)
5761

62+
/* pthread */
63+
#define pthread_attr_init(...) zap_pthread_attr_init(__VA_ARGS__)
64+
#define pthread_attr_destroy(...) zap_pthread_attr_destroy(__VA_ARGS__)
65+
#define pthread_attr_getschedparam(...) \
66+
zap_pthread_attr_getschedparam(__VA_ARGS__)
67+
#define pthread_attr_getstack(...) zap_pthread_attr_getstack(__VA_ARGS__)
68+
#define pthread_attr_getstacksize(...) \
69+
zap_pthread_attr_getstacksize(__VA_ARGS__)
70+
#define pthread_equal(...) zap_pthread_equal(__VA_ARGS__)
71+
#define pthread_self(...) zap_pthread_self(__VA_ARGS__)
72+
#define pthread_getschedparam(...) zap_pthread_getschedparam(__VA_ARGS__)
73+
#define pthread_exit(...) zap_pthread_exit(__VA_ARGS__)
74+
#define pthread_join(...) zap_pthread_join(__VA_ARGS__)
75+
#define pthread_detach(...) zap_pthread_detach(__VA_ARGS__)
76+
#define pthread_cancel(...) zap_pthread_cancel(__VA_ARGS__)
77+
#define pthread_attr_getdetachstate(...) \
78+
zap_pthread_attr_getdetachstate(__VA_ARGS__)
79+
#define pthread_attr_setdetachstate(...) \
80+
zap_pthread_attr_setdetachstate(__VA_ARGS__)
81+
#define pthread_attr_setschedparam(...) \
82+
zap_pthread_attr_setschedparam(__VA_ARGS__)
83+
#define pthread_attr_setschedpolicy(...)\
84+
zap_pthread_attr_setschedpolicy(__VA_ARGS__)
85+
#define pthread_attr_getschedpolicy(...)\
86+
zap_pthread_attr_getschedpolicy(__VA_ARGS__)
87+
88+
#define pthread_attr_setstack(...) zap_pthread_attr_setstack(__VA_ARGS__)
89+
#define pthread_create(...) zap_pthread_create(__VA_ARGS__)
90+
#define pthread_setcancelstate(...) zap_pthread_setcancelstate(__VA_ARGS__)
91+
#define pthread_setschedparam(...) zap_pthread_setschedparam(__VA_ARGS__)
92+
5893
#endif /* CONFIG_ARCH_POSIX */
5994

6095
#endif

include/posix/pthread.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#ifndef __PTHREAD_H__
88
#define __PTHREAD_H__
99

10+
#include <kernel.h>
1011
#ifdef CONFIG_NEWLIB_LIBC
1112
#include <time.h>
1213
#else
@@ -20,6 +21,46 @@ struct timespec {
2021
#endif /* CONFIG_NEWLIB_LIBC */
2122

2223
#include "sys/types.h"
24+
#include "sched.h"
25+
26+
enum pthread_state {
27+
/* The thread is running and joinable. */
28+
PTHREAD_JOINABLE = 0,
29+
/* The thread is running and detached. */
30+
PTHREAD_DETACHED,
31+
/* A joinable thread exited and its return code is available. */
32+
PTHREAD_EXITED,
33+
/* The thread structure is unallocated and available for reuse. */
34+
PTHREAD_TERMINATED
35+
};
36+
37+
struct posix_thread {
38+
struct k_thread thread;
39+
40+
/* Exit status */
41+
void *retval;
42+
43+
/* Pthread cancellation */
44+
int cancel_state;
45+
int cancel_pending;
46+
struct k_sem cancel_lock_sem;
47+
pthread_mutex_t cancel_lock;
48+
49+
/* Pthread State */
50+
enum pthread_state state;
51+
pthread_mutex_t state_lock;
52+
struct k_sem state_lock_sem;
53+
pthread_cond_t state_cond;
54+
};
55+
56+
/* Pthread detach/joinable */
57+
#define PTHREAD_CREATE_JOINABLE 0
58+
#define PTHREAD_CREATE_DETACHED 1
59+
60+
/* Pthread cancellation */
61+
#define _PTHREAD_CANCEL_POS 0
62+
#define PTHREAD_CANCEL_ENABLE (0 << _PTHREAD_CANCEL_POS)
63+
#define PTHREAD_CANCEL_DISABLE (1 << _PTHREAD_CANCEL_POS)
2364

2465
static inline s32_t _ts_to_ms(const struct timespec *to)
2566
{
@@ -359,4 +400,57 @@ int pthread_barrierattr_getpshared(const pthread_barrierattr_t *, int *);
359400
int pthread_barrierattr_setpshared(pthread_barrierattr_t *, int);
360401
*/
361402

403+
/* Base Pthread related APIs */
404+
405+
/**
406+
* @brief Obtain ID of the calling thread.
407+
*
408+
* The results of calling this API from threads not created with
409+
* pthread_create() are undefined.
410+
*
411+
* See IEEE 1003.1
412+
*/
413+
static inline pthread_t pthread_self(void)
414+
{
415+
return (pthread_t)k_current_get();
416+
}
417+
418+
419+
/**
420+
* @brief Compare thread IDs.
421+
*
422+
* See IEEE 1003.1
423+
*/
424+
static inline int pthread_equal(pthread_t pt1, pthread_t pt2)
425+
{
426+
return (pt1 == pt2);
427+
}
428+
429+
int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
430+
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
431+
int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy);
432+
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
433+
int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
434+
int pthread_attr_init(pthread_attr_t *attr);
435+
int pthread_attr_destroy(pthread_attr_t *attr);
436+
int pthread_attr_getschedparam(const pthread_attr_t *attr,
437+
struct sched_param *schedparam);
438+
int pthread_getschedparam(pthread_t pthread, int *policy,
439+
struct sched_param *param);
440+
int pthread_attr_getstack(const pthread_attr_t *attr,
441+
void **stackaddr, size_t *stacksize);
442+
int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
443+
size_t stacksize);
444+
void pthread_exit(void *retval);
445+
int pthread_join(pthread_t thread, void **status);
446+
int pthread_cancel(pthread_t pthread);
447+
int pthread_detach(pthread_t thread);
448+
int pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
449+
void *(*threadroutine)(void *), void *arg);
450+
int pthread_setcancelstate(int state, int *oldstate);
451+
int pthread_attr_setschedparam(pthread_attr_t *attr,
452+
const struct sched_param *schedparam);
453+
int pthread_setschedparam(pthread_t pthread, int policy,
454+
const struct sched_param *param);
455+
362456
#endif /* __PTHREAD_H__ */

include/posix/sched.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2018 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#ifndef __POSIX_SCHED_H__
7+
#define __POSIX_SCHED_H__
8+
9+
#ifdef __cplusplus
10+
extern "C" {
11+
#endif
12+
13+
#include_next <sched.h>
14+
15+
/* Cooperative scheduling policy */
16+
#ifndef SCHED_FIFO
17+
#define SCHED_FIFO 0
18+
#endif /* SCHED_FIFO */
19+
20+
/* Priority based prempetive scheduling policy */
21+
#ifndef SCHED_RR
22+
#define SCHED_RR 1
23+
#endif /* SCHED_RR */
24+
25+
struct sched_param {
26+
int priority; /* Thread execution priority */
27+
};
28+
29+
#ifdef __cplusplus
30+
}
31+
#endif
32+
33+
#endif /* __POSIX_SCHED_H__ */

include/posix/sys/types.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,24 @@
1212
extern "C" {
1313
#endif
1414

15-
#ifdef CONFIG_NEWLIB_LIBC
1615
#include_next <sys/types.h>
17-
#endif /* CONFIG_NEWLIB_LIBC */
1816

1917
#ifdef CONFIG_PTHREAD_IPC
18+
19+
/* Thread attributes */
20+
typedef struct pthread_attr_t {
21+
int priority;
22+
void *stack;
23+
size_t stacksize;
24+
u32_t flags;
25+
u32_t delayedstart;
26+
u32_t schedpolicy;
27+
s32_t detachstate;
28+
u32_t initialized;
29+
} pthread_attr_t;
30+
31+
typedef void *pthread_t;
32+
2033
/* Mutex */
2134
typedef struct pthread_mutex {
2235
struct k_sem *sem;
@@ -26,7 +39,7 @@ typedef struct pthread_mutexattr {
2639
int unused;
2740
} pthread_mutexattr_t;
2841

29-
/* Confition Variables */
42+
/* Condition variables */
3043
typedef struct pthread_cond {
3144
_wait_q_t wait_q;
3245
} pthread_cond_t;

kernel/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ add_library(kernel
2525
smp.c
2626
)
2727

28+
if (CONFIG_PTHREAD_IPC)
2829
target_include_directories(kernel PRIVATE ${PROJECT_SOURCE_DIR}/include/posix)
30+
endif (CONFIG_PTHREAD_IPC)
2931
target_sources_ifdef(CONFIG_INT_LATENCY_BENCHMARK kernel PRIVATE int_latency_bench.c)
3032
target_sources_ifdef(CONFIG_STACK_CANARIES kernel PRIVATE compiler_stack_protect.c)
3133
target_sources_ifdef(CONFIG_SYS_CLOCK_EXISTS kernel PRIVATE timer.c)

kernel/Kconfig

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,19 @@ config PTHREAD_IPC
500500
prompt "POSIX pthread IPC API"
501501
default n
502502
help
503-
This enables a mostly-standards-compliant implementation of
504-
the pthread mutex, condition variable and barrier IPC
505-
mechanisms.
503+
This enables a mostly-standards-compliant implementation of
504+
the pthread mutex, condition variable and barrier IPC
505+
mechanisms.
506+
507+
if PTHREAD_IPC
508+
config MAX_PTHREAD_COUNT
509+
int
510+
prompt "Maximum pthread count in POSIX application"
511+
default 5
512+
range 0 255
513+
help
514+
Mention maximum number of threads in POSIX compliant application.
515+
endif
506516
endmenu
507517

508518
menu "Security Options"

kernel/posix/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ target_sources(kernel PRIVATE posix/pthread_common.c)
22
target_sources(kernel PRIVATE posix/pthread_cond.c)
33
target_sources(kernel PRIVATE posix/pthread_mutex.c)
44
target_sources(kernel PRIVATE posix/pthread_barrier.c)
5+
target_sources(kernel PRIVATE posix/pthread.c)

0 commit comments

Comments
 (0)