29
29
POSIX_TO_ZEPHYR_PRIORITY(K_LOWEST_APPLICATION_THREAD_PRIO, DEFAULT_PTHREAD_POLICY)
30
30
#define DEFAULT_PTHREAD_POLICY (IS_ENABLED(CONFIG_PREEMPT_ENABLED) ? SCHED_RR : SCHED_FIFO)
31
31
32
+ #define PTHREAD_STACK_MAX BIT(CONFIG_POSIX_PTHREAD_ATTR_STACKSIZE_BITS)
33
+ #define PTHREAD_GUARD_MAX BIT_MASK(CONFIG_POSIX_PTHREAD_ATTR_GUARDSIZE_BITS)
34
+
32
35
LOG_MODULE_REGISTER (pthread , CONFIG_PTHREAD_LOG_LEVEL );
33
36
34
37
#ifdef CONFIG_DYNAMIC_THREAD_STACK_SIZE
@@ -37,28 +40,14 @@ LOG_MODULE_REGISTER(pthread, CONFIG_PTHREAD_LOG_LEVEL);
37
40
#define DYNAMIC_STACK_SIZE 0
38
41
#endif
39
42
40
- /* The maximum allowed stack size (for this implementation) */
41
- #define PTHREAD_STACK_MAX (UINT16_MAX + 1)
42
-
43
- static inline size_t __get_attr_stacksize (const struct pthread_attr * attr )
43
+ static inline size_t __get_attr_stacksize (const struct posix_thread_attr * attr )
44
44
{
45
45
return attr -> stacksize + 1 ;
46
46
}
47
47
48
- static inline void __set_attr_stacksize (struct pthread_attr * attr , size_t size )
49
- {
50
- attr -> stacksize = size - 1 ;
51
- }
52
-
53
- static inline size_t __get_attr_guardsize (const struct pthread_attr * attr )
48
+ static inline void __set_attr_stacksize (struct posix_thread_attr * attr , size_t stacksize )
54
49
{
55
- return (attr -> guardsize_msbit * BIT (16 )) | attr -> guardsize ;
56
- }
57
-
58
- static inline void __set_attr_guardsize (struct pthread_attr * attr , size_t size )
59
- {
60
- attr -> guardsize_msbit = size == PTHREAD_STACK_MAX ;
61
- attr -> guardsize = size & BIT_MASK (16 );
50
+ attr -> stacksize = stacksize - 1 ;
62
51
}
63
52
64
53
struct __pthread_cleanup {
@@ -76,7 +65,7 @@ enum posix_thread_qid {
76
65
POSIX_THREAD_DONE_Q ,
77
66
};
78
67
79
- /* only 2 bits in struct pthread_attr for schedpolicy */
68
+ /* only 2 bits in struct posix_thread_attr for schedpolicy */
80
69
BUILD_ASSERT (SCHED_OTHER < BIT (2 ) && SCHED_FIFO < BIT (2 ) && SCHED_RR < BIT (2 ));
81
70
82
71
BUILD_ASSERT ((PTHREAD_CREATE_DETACHED == 0 || PTHREAD_CREATE_JOINABLE == 0 ) &&
@@ -85,6 +74,9 @@ BUILD_ASSERT((PTHREAD_CREATE_DETACHED == 0 || PTHREAD_CREATE_JOINABLE == 0) &&
85
74
BUILD_ASSERT ((PTHREAD_CANCEL_ENABLE == 0 || PTHREAD_CANCEL_DISABLE == 0 ) &&
86
75
(PTHREAD_CANCEL_ENABLE == 1 || PTHREAD_CANCEL_DISABLE == 1 ));
87
76
77
+ BUILD_ASSERT (CONFIG_POSIX_PTHREAD_ATTR_STACKSIZE_BITS + CONFIG_POSIX_PTHREAD_ATTR_GUARDSIZE_BITS <=
78
+ 32 );
79
+
88
80
static void posix_thread_recycle (void );
89
81
static sys_dlist_t ready_q = SYS_DLIST_STATIC_INIT (& ready_q );
90
82
static sys_dlist_t run_q = SYS_DLIST_STATIC_INIT (& run_q );
@@ -93,18 +85,6 @@ static struct posix_thread posix_thread_pool[CONFIG_MAX_PTHREAD_COUNT];
93
85
static struct k_spinlock pthread_pool_lock ;
94
86
static int pthread_concurrency ;
95
87
96
- static const struct pthread_attr init_pthread_attrs = {
97
- .stack = NULL ,
98
- .stacksize = 0 ,
99
- .guardsize = (BIT_MASK (16 ) & CONFIG_POSIX_PTHREAD_ATTR_GUARDSIZE_DEFAULT ),
100
- .priority = DEFAULT_PTHREAD_PRIORITY ,
101
- .schedpolicy = DEFAULT_PTHREAD_POLICY ,
102
- .guardsize_msbit = (BIT (16 ) & CONFIG_POSIX_PTHREAD_ATTR_GUARDSIZE_DEFAULT ),
103
- .initialized = true,
104
- .cancelstate = PTHREAD_CANCEL_ENABLE ,
105
- .detachstate = PTHREAD_CREATE_JOINABLE ,
106
- };
107
-
108
88
/*
109
89
* We reserve the MSB to mark a pthread_t as initialized (from the
110
90
* perspective of the application). With a linear space, this means that
@@ -262,7 +242,7 @@ static int32_t posix_to_zephyr_priority(uint32_t priority, int policy)
262
242
*/
263
243
int pthread_attr_setschedparam (pthread_attr_t * _attr , const struct sched_param * schedparam )
264
244
{
265
- struct pthread_attr * attr = (struct pthread_attr * )_attr ;
245
+ struct posix_thread_attr * attr = (struct posix_thread_attr * )_attr ;
266
246
int priority = schedparam -> sched_priority ;
267
247
268
248
if (attr == NULL || !attr -> initialized ||
@@ -282,7 +262,7 @@ int pthread_attr_setschedparam(pthread_attr_t *_attr, const struct sched_param *
282
262
*/
283
263
int pthread_attr_setstack (pthread_attr_t * _attr , void * stackaddr , size_t stacksize )
284
264
{
285
- struct pthread_attr * attr = (struct pthread_attr * )_attr ;
265
+ struct posix_thread_attr * attr = (struct posix_thread_attr * )_attr ;
286
266
287
267
if (stackaddr == NULL ) {
288
268
LOG_ERR ("NULL stack address" );
@@ -299,7 +279,7 @@ int pthread_attr_setstack(pthread_attr_t *_attr, void *stackaddr, size_t stacksi
299
279
return 0 ;
300
280
}
301
281
302
- static bool pthread_attr_is_valid (const struct pthread_attr * attr )
282
+ static bool pthread_attr_is_valid (const struct posix_thread_attr * attr )
303
283
{
304
284
/* auto-alloc thread stack */
305
285
if (attr == NULL ) {
@@ -438,19 +418,19 @@ int pthread_create(pthread_t *th, const pthread_attr_t *_attr, void *(*threadrou
438
418
k_spinlock_key_t key ;
439
419
pthread_barrier_t barrier ;
440
420
struct posix_thread * t = NULL ;
441
- struct pthread_attr attr_storage = init_pthread_attrs ;
442
- struct pthread_attr * attr = (struct pthread_attr * )_attr ;
421
+ struct posix_thread_attr attr_storage ;
422
+ struct posix_thread_attr * attr = (struct posix_thread_attr * )_attr ;
443
423
444
424
if (!pthread_attr_is_valid (attr )) {
445
425
return EINVAL ;
446
426
}
447
427
448
428
if (attr == NULL ) {
449
429
attr = & attr_storage ;
430
+ (void )pthread_attr_init ((pthread_attr_t * )attr );
450
431
BUILD_ASSERT (DYNAMIC_STACK_SIZE <= PTHREAD_STACK_MAX );
451
432
__set_attr_stacksize (attr , DYNAMIC_STACK_SIZE );
452
- attr -> stack = k_thread_stack_alloc (__get_attr_stacksize (attr ) +
453
- __get_attr_guardsize (attr ),
433
+ attr -> stack = k_thread_stack_alloc (__get_attr_stacksize (attr ) + attr -> guardsize ,
454
434
k_is_user_context () ? K_USER : 0 );
455
435
if (attr -> stack == NULL ) {
456
436
LOG_ERR ("Unable to allocate stack of size %u" , DYNAMIC_STACK_SIZE );
@@ -505,7 +485,7 @@ int pthread_create(pthread_t *th, const pthread_attr_t *_attr, void *(*threadrou
505
485
}
506
486
507
487
/* spawn the thread */
508
- k_thread_create (& t -> thread , attr -> stack , attr -> stacksize , zephyr_thread_wrapper ,
488
+ k_thread_create (& t -> thread , attr -> stack , __get_attr_stacksize ( attr ) , zephyr_thread_wrapper ,
509
489
(void * )arg , threadroutine ,
510
490
IS_ENABLED (CONFIG_PTHREAD_CREATE_BARRIER ) ? UINT_TO_POINTER (barrier ) : NULL ,
511
491
posix_to_zephyr_priority (attr -> priority , attr -> schedpolicy ), 0 , K_NO_WAIT );
@@ -680,14 +660,16 @@ int pthread_setschedparam(pthread_t pthread, int policy, const struct sched_para
680
660
*/
681
661
int pthread_attr_init (pthread_attr_t * _attr )
682
662
{
683
- struct pthread_attr * const attr = (struct pthread_attr * )_attr ;
663
+ struct posix_thread_attr * const attr = (struct posix_thread_attr * )_attr ;
684
664
685
665
if (attr == NULL ) {
686
666
LOG_ERR ("Invalid attr pointer" );
687
667
return ENOMEM ;
688
668
}
689
669
690
- (void )memcpy (attr , & init_pthread_attrs , sizeof (struct pthread_attr ));
670
+ * attr = (struct posix_thread_attr ){0 };
671
+ attr -> guardsize = CONFIG_POSIX_PTHREAD_ATTR_GUARDSIZE_DEFAULT ;
672
+ attr -> initialized = true;
691
673
692
674
return 0 ;
693
675
}
@@ -883,7 +865,7 @@ int pthread_detach(pthread_t pthread)
883
865
*/
884
866
int pthread_attr_getdetachstate (const pthread_attr_t * _attr , int * detachstate )
885
867
{
886
- const struct pthread_attr * attr = (const struct pthread_attr * )_attr ;
868
+ const struct posix_thread_attr * attr = (const struct posix_thread_attr * )_attr ;
887
869
888
870
if ((attr == NULL ) || (attr -> initialized == false)) {
889
871
return EINVAL ;
@@ -900,7 +882,7 @@ int pthread_attr_getdetachstate(const pthread_attr_t *_attr, int *detachstate)
900
882
*/
901
883
int pthread_attr_setdetachstate (pthread_attr_t * _attr , int detachstate )
902
884
{
903
- struct pthread_attr * attr = (struct pthread_attr * )_attr ;
885
+ struct posix_thread_attr * attr = (struct posix_thread_attr * )_attr ;
904
886
905
887
if ((attr == NULL ) || (attr -> initialized == false) ||
906
888
((detachstate != PTHREAD_CREATE_DETACHED ) &&
@@ -919,7 +901,7 @@ int pthread_attr_setdetachstate(pthread_attr_t *_attr, int detachstate)
919
901
*/
920
902
int pthread_attr_getschedpolicy (const pthread_attr_t * _attr , int * policy )
921
903
{
922
- const struct pthread_attr * attr = (const struct pthread_attr * )_attr ;
904
+ const struct posix_thread_attr * attr = (const struct posix_thread_attr * )_attr ;
923
905
924
906
if ((attr == NULL ) || (attr -> initialized == 0U )) {
925
907
return EINVAL ;
@@ -936,7 +918,7 @@ int pthread_attr_getschedpolicy(const pthread_attr_t *_attr, int *policy)
936
918
*/
937
919
int pthread_attr_setschedpolicy (pthread_attr_t * _attr , int policy )
938
920
{
939
- struct pthread_attr * attr = (struct pthread_attr * )_attr ;
921
+ struct posix_thread_attr * attr = (struct posix_thread_attr * )_attr ;
940
922
941
923
if ((attr == NULL ) || (attr -> initialized == 0U ) || !valid_posix_policy (policy )) {
942
924
return EINVAL ;
@@ -953,7 +935,7 @@ int pthread_attr_setschedpolicy(pthread_attr_t *_attr, int policy)
953
935
*/
954
936
int pthread_attr_getstacksize (const pthread_attr_t * _attr , size_t * stacksize )
955
937
{
956
- const struct pthread_attr * attr = (const struct pthread_attr * )_attr ;
938
+ const struct posix_thread_attr * attr = (const struct posix_thread_attr * )_attr ;
957
939
958
940
if ((attr == NULL ) || (attr -> initialized == false)) {
959
941
return EINVAL ;
@@ -970,7 +952,7 @@ int pthread_attr_getstacksize(const pthread_attr_t *_attr, size_t *stacksize)
970
952
*/
971
953
int pthread_attr_setstacksize (pthread_attr_t * _attr , size_t stacksize )
972
954
{
973
- struct pthread_attr * attr = (struct pthread_attr * )_attr ;
955
+ struct posix_thread_attr * attr = (struct posix_thread_attr * )_attr ;
974
956
975
957
if ((attr == NULL ) || (attr -> initialized == 0U )) {
976
958
return EINVAL ;
@@ -991,7 +973,7 @@ int pthread_attr_setstacksize(pthread_attr_t *_attr, size_t stacksize)
991
973
*/
992
974
int pthread_attr_getstack (const pthread_attr_t * _attr , void * * stackaddr , size_t * stacksize )
993
975
{
994
- const struct pthread_attr * attr = (const struct pthread_attr * )_attr ;
976
+ const struct posix_thread_attr * attr = (const struct posix_thread_attr * )_attr ;
995
977
996
978
if ((attr == NULL ) || (attr -> initialized == false)) {
997
979
return EINVAL ;
@@ -1004,26 +986,26 @@ int pthread_attr_getstack(const pthread_attr_t *_attr, void **stackaddr, size_t
1004
986
1005
987
int pthread_attr_getguardsize (const pthread_attr_t * ZRESTRICT _attr , size_t * ZRESTRICT guardsize )
1006
988
{
1007
- struct pthread_attr * const attr = (struct pthread_attr * )_attr ;
989
+ struct posix_thread_attr * const attr = (struct posix_thread_attr * )_attr ;
1008
990
1009
991
if (attr == NULL || guardsize == NULL || !attr -> initialized ) {
1010
992
return EINVAL ;
1011
993
}
1012
994
1013
- * guardsize = __get_attr_guardsize ( attr ) ;
995
+ * guardsize = attr -> guardsize ;
1014
996
1015
997
return 0 ;
1016
998
}
1017
999
1018
1000
int pthread_attr_setguardsize (pthread_attr_t * _attr , size_t guardsize )
1019
1001
{
1020
- struct pthread_attr * const attr = (struct pthread_attr * )_attr ;
1002
+ struct posix_thread_attr * const attr = (struct posix_thread_attr * )_attr ;
1021
1003
1022
- if (attr == NULL || !attr -> initialized || guardsize > PTHREAD_STACK_MAX ) {
1004
+ if (attr == NULL || !attr -> initialized || guardsize > PTHREAD_GUARD_MAX ) {
1023
1005
return EINVAL ;
1024
1006
}
1025
1007
1026
- __set_attr_guardsize ( attr , guardsize ) ;
1008
+ attr -> guardsize = guardsize ;
1027
1009
1028
1010
return 0 ;
1029
1011
}
@@ -1035,7 +1017,7 @@ int pthread_attr_setguardsize(pthread_attr_t *_attr, size_t guardsize)
1035
1017
*/
1036
1018
int pthread_attr_getschedparam (const pthread_attr_t * _attr , struct sched_param * schedparam )
1037
1019
{
1038
- struct pthread_attr * attr = (struct pthread_attr * )_attr ;
1020
+ struct posix_thread_attr * attr = (struct posix_thread_attr * )_attr ;
1039
1021
1040
1022
if ((attr == NULL ) || (attr -> initialized == false)) {
1041
1023
return EINVAL ;
@@ -1052,7 +1034,7 @@ int pthread_attr_getschedparam(const pthread_attr_t *_attr, struct sched_param *
1052
1034
*/
1053
1035
int pthread_attr_destroy (pthread_attr_t * _attr )
1054
1036
{
1055
- struct pthread_attr * attr = (struct pthread_attr * )_attr ;
1037
+ struct posix_thread_attr * attr = (struct posix_thread_attr * )_attr ;
1056
1038
1057
1039
if ((attr != NULL ) && (attr -> initialized != 0U )) {
1058
1040
attr -> initialized = false;
0 commit comments