Skip to content

Commit 80e3f4a

Browse files
Christopher Friedtcfriedt
authored andcommitted
tests: posix: common: rwlock: remove overspecified pthread_attr_t
Much of tests/posix/common still overspecifies pthread_attr_t options. Default thread attributes are perfectly fine for the vast majority of spawned threads in this testsuite, so simply use a NULL pthread_attr_t* argument. This fixes piles of bugs because we have not properly used pthread_attr_destroy() appropriately. Signed-off-by: Christopher Friedt <[email protected]>
1 parent e14f362 commit 80e3f4a

File tree

1 file changed

+43
-71
lines changed

1 file changed

+43
-71
lines changed

tests/posix/common/src/rwlock.c

Lines changed: 43 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,50 @@
66

77
#include <pthread.h>
88

9+
#include <zephyr/logging/log.h>
910
#include <zephyr/sys/util.h>
1011
#include <zephyr/ztest.h>
1112

1213
#define N_THR 3
13-
#define STACKSZ (MAX(1024, PTHREAD_STACK_MIN) + CONFIG_TEST_EXTRA_STACK_SIZE)
1414

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;
1718

1819
static void *thread_top(void *p1)
1920
{
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;
2923

24+
id = (pthread_t)pthread_self();
3025
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");
3529
}
3630

37-
printk("Thread %d got RD lock\n", id);
31+
LOG_DBG("Thread %d got RD lock", id);
3832
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");
4135

42-
printk("Thread %d acquiring WR lock\n", id);
36+
LOG_DBG("Thread %d acquiring WR lock", id);
4337
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");
4740
}
4841

49-
printk("Thread %d acquired WR lock\n", id);
42+
LOG_DBG("Thread %d acquired WR lock", id);
5043
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+
5447
return NULL;
5548
}
5649

5750
ZTEST(posix_apis, test_rw_lock)
5851
{
59-
int32_t i, ret;
60-
pthread_attr_t attr[N_THR];
61-
struct sched_param schedparam;
52+
int ret;
6253
pthread_t newthread[N_THR];
6354
struct timespec time;
6455
void *status;
@@ -75,72 +66,53 @@ ZTEST(posix_apis, test_rw_lock)
7566
zassert_equal(pthread_rwlock_timedrdlock(&rwlock, &time), EINVAL);
7667
zassert_equal(pthread_rwlock_unlock(&rwlock), EINVAL);
7768

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");
8372

8473
/* 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");
10077
}
10178

10279
/* Delay to give change to child threads to run */
10380
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");
10683

10784
/* Let child threads acquire RD Lock */
10885
usleep(USEC_PER_MSEC);
109-
printk("Parent thread acquiring WR lock again\n");
86+
LOG_DBG("Parent thread acquiring WR lock again");
11087

11188
time.tv_sec = 2;
11289
time.tv_nsec = 0;
11390
ret = pthread_rwlock_timedwrlock(&rwlock, &time);
114-
11591
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");
11893
}
11994

120-
printk("Parent thread acquired WR lock again\n");
95+
LOG_DBG("Parent thread acquired WR lock again");
12196
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");
12499

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");
127102

128103
ret = pthread_rwlock_timedrdlock(&rwlock, &time);
129-
130104
if (ret != 0) {
131-
zassert_false(pthread_rwlock_rdlock(&rwlock), "Failed to lock");
105+
zassert_ok(pthread_rwlock_rdlock(&rwlock), "Failed to lock");
132106
}
133107

134-
printk("Main thread acquired RD lock\n");
108+
LOG_DBG("Main thread acquired RD lock");
135109
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");
138112

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");
142115
}
143116

144-
zassert_false(pthread_rwlock_destroy(&rwlock),
145-
"Failed to destroy rwlock");
117+
zassert_ok(pthread_rwlock_destroy(&rwlock), "Failed to destroy rwlock");
146118
}

0 commit comments

Comments
 (0)