6
6
7
7
#include <pthread.h>
8
8
9
+ #include <zephyr/logging/log.h>
9
10
#include <zephyr/sys/util.h>
10
11
#include <zephyr/ztest.h>
11
12
12
13
#define N_THR 3
13
- #define STACKSZ (MAX(1024, PTHREAD_STACK_MIN) + CONFIG_TEST_EXTRA_STACK_SIZE)
14
14
15
- K_THREAD_STACK_ARRAY_DEFINE (stack , N_THR , STACKSZ );
16
- pthread_rwlock_t rwlock ;
15
+ LOG_MODULE_REGISTER (posix_rwlock_test );
16
+
17
+ static pthread_rwlock_t rwlock ;
17
18
18
19
static void * thread_top (void * p1 )
19
20
{
20
- pthread_t pthread ;
21
- uint32_t policy , ret = 0U ;
22
- struct sched_param param ;
23
- int id = POINTER_TO_INT (p1 );
24
-
25
- pthread = (pthread_t ) pthread_self ();
26
- pthread_getschedparam (pthread , & policy , & param );
27
- printk ("Thread %d scheduling policy = %d & priority %d started\n" ,
28
- id , policy , param .sched_priority );
21
+ int ret ;
22
+ pthread_t id ;
29
23
24
+ id = (pthread_t )pthread_self ();
30
25
ret = pthread_rwlock_tryrdlock (& rwlock );
31
- if (ret ) {
32
- printk ("Not able to get RD lock on trying, try again\n" );
33
- zassert_false (pthread_rwlock_rdlock (& rwlock ),
34
- "Failed to acquire write lock" );
26
+ if (ret != 0 ) {
27
+ LOG_DBG ("Not able to get RD lock on trying, try again" );
28
+ zassert_ok (pthread_rwlock_rdlock (& rwlock ), "Failed to acquire write lock" );
35
29
}
36
30
37
- printk ("Thread %d got RD lock\n " , id );
31
+ LOG_DBG ("Thread %d got RD lock" , id );
38
32
usleep (USEC_PER_MSEC );
39
- printk ("Thread %d releasing RD lock\n " , id );
40
- zassert_false (pthread_rwlock_unlock (& rwlock ), "Failed to unlock" );
33
+ LOG_DBG ("Thread %d releasing RD lock" , id );
34
+ zassert_ok (pthread_rwlock_unlock (& rwlock ), "Failed to unlock" );
41
35
42
- printk ("Thread %d acquiring WR lock\n " , id );
36
+ LOG_DBG ("Thread %d acquiring WR lock" , id );
43
37
ret = pthread_rwlock_trywrlock (& rwlock );
44
- if (ret != 0U ) {
45
- zassert_false (pthread_rwlock_wrlock (& rwlock ),
46
- "Failed to acquire WR lock" );
38
+ if (ret != 0 ) {
39
+ zassert_ok (pthread_rwlock_wrlock (& rwlock ), "Failed to acquire WR lock" );
47
40
}
48
41
49
- printk ("Thread %d acquired WR lock\n " , id );
42
+ LOG_DBG ("Thread %d acquired WR lock" , id );
50
43
usleep (USEC_PER_MSEC );
51
- printk ("Thread %d releasing WR lock\n " , id );
52
- zassert_false (pthread_rwlock_unlock (& rwlock ), "Failed to unlock" );
53
- pthread_exit ( NULL );
44
+ LOG_DBG ("Thread %d releasing WR lock" , id );
45
+ zassert_ok (pthread_rwlock_unlock (& rwlock ), "Failed to unlock" );
46
+
54
47
return NULL ;
55
48
}
56
49
57
50
ZTEST (posix_apis , test_rw_lock )
58
51
{
59
- int32_t i , ret ;
60
- pthread_attr_t attr [N_THR ];
61
- struct sched_param schedparam ;
52
+ int ret ;
62
53
pthread_t newthread [N_THR ];
63
54
struct timespec time ;
64
55
void * status ;
@@ -75,72 +66,53 @@ ZTEST(posix_apis, test_rw_lock)
75
66
zassert_equal (pthread_rwlock_timedrdlock (& rwlock , & time ), EINVAL );
76
67
zassert_equal (pthread_rwlock_unlock (& rwlock ), EINVAL );
77
68
78
- zassert_false (pthread_rwlock_init (& rwlock , NULL ),
79
- "Failed to create rwlock" );
80
- printk ("\nmain acquire WR lock and 3 threads acquire RD lock\n" );
81
- zassert_false (pthread_rwlock_timedwrlock (& rwlock , & time ),
82
- "Failed to acquire write lock" );
69
+ zassert_ok (pthread_rwlock_init (& rwlock , NULL ), "Failed to create rwlock" );
70
+ LOG_DBG ("main acquire WR lock and 3 threads acquire RD lock" );
71
+ zassert_ok (pthread_rwlock_timedwrlock (& rwlock , & time ), "Failed to acquire write lock" );
83
72
84
73
/* Creating N preemptive threads in increasing order of priority */
85
- for (i = 0 ; i < N_THR ; i ++ ) {
86
- zassert_equal (pthread_attr_init (& attr [i ]), 0 ,
87
- "Unable to create pthread object attrib" );
88
-
89
- /* Setting scheduling priority */
90
- schedparam .sched_priority = i + 1 ;
91
- pthread_attr_setschedparam (& attr [i ], & schedparam );
92
-
93
- /* Setting stack */
94
- pthread_attr_setstack (& attr [i ], & stack [i ][0 ], STACKSZ );
95
-
96
- ret = pthread_create (& newthread [i ], & attr [i ], thread_top ,
97
- INT_TO_POINTER (i ));
98
- zassert_false (ret , "Low memory to thread new thread" );
99
-
74
+ for (int i = 0 ; i < N_THR ; i ++ ) {
75
+ zassert_ok (pthread_create (& newthread [i ], NULL , thread_top , NULL ),
76
+ "Low memory to thread new thread" );
100
77
}
101
78
102
79
/* Delay to give change to child threads to run */
103
80
usleep (USEC_PER_MSEC );
104
- printk ("Parent thread releasing WR lock\n " );
105
- zassert_false (pthread_rwlock_unlock (& rwlock ), "Failed to unlock" );
81
+ LOG_DBG ("Parent thread releasing WR lock" );
82
+ zassert_ok (pthread_rwlock_unlock (& rwlock ), "Failed to unlock" );
106
83
107
84
/* Let child threads acquire RD Lock */
108
85
usleep (USEC_PER_MSEC );
109
- printk ("Parent thread acquiring WR lock again\n " );
86
+ LOG_DBG ("Parent thread acquiring WR lock again" );
110
87
111
88
time .tv_sec = 2 ;
112
89
time .tv_nsec = 0 ;
113
90
ret = pthread_rwlock_timedwrlock (& rwlock , & time );
114
-
115
91
if (ret ) {
116
- zassert_false (pthread_rwlock_wrlock (& rwlock ),
117
- "Failed to acquire write lock" );
92
+ zassert_ok (pthread_rwlock_wrlock (& rwlock ), "Failed to acquire write lock" );
118
93
}
119
94
120
- printk ("Parent thread acquired WR lock again\n " );
95
+ LOG_DBG ("Parent thread acquired WR lock again" );
121
96
usleep (USEC_PER_MSEC );
122
- printk ("Parent thread releasing WR lock again\n " );
123
- zassert_false (pthread_rwlock_unlock (& rwlock ), "Failed to unlock" );
97
+ LOG_DBG ("Parent thread releasing WR lock again" );
98
+ zassert_ok (pthread_rwlock_unlock (& rwlock ), "Failed to unlock" );
124
99
125
- printk ( "\n3 threads acquire WR lock\n " );
126
- printk ("Main thread acquiring RD lock\n " );
100
+ LOG_DBG ( "3 threads acquire WR lock" );
101
+ LOG_DBG ("Main thread acquiring RD lock" );
127
102
128
103
ret = pthread_rwlock_timedrdlock (& rwlock , & time );
129
-
130
104
if (ret != 0 ) {
131
- zassert_false (pthread_rwlock_rdlock (& rwlock ), "Failed to lock" );
105
+ zassert_ok (pthread_rwlock_rdlock (& rwlock ), "Failed to lock" );
132
106
}
133
107
134
- printk ("Main thread acquired RD lock\n " );
108
+ LOG_DBG ("Main thread acquired RD lock" );
135
109
usleep (USEC_PER_MSEC );
136
- printk ("Main thread releasing RD lock\n " );
137
- zassert_false (pthread_rwlock_unlock (& rwlock ), "Failed to unlock" );
110
+ LOG_DBG ("Main thread releasing RD lock" );
111
+ zassert_ok (pthread_rwlock_unlock (& rwlock ), "Failed to unlock" );
138
112
139
- for (i = 0 ; i < N_THR ; i ++ ) {
140
- zassert_false (pthread_join (newthread [i ], & status ),
141
- "Failed to join" );
113
+ for (int i = 0 ; i < N_THR ; i ++ ) {
114
+ zassert_ok (pthread_join (newthread [i ], & status ), "Failed to join" );
142
115
}
143
116
144
- zassert_false (pthread_rwlock_destroy (& rwlock ),
145
- "Failed to destroy rwlock" );
117
+ zassert_ok (pthread_rwlock_destroy (& rwlock ), "Failed to destroy rwlock" );
146
118
}
0 commit comments