Skip to content

Commit dbb6511

Browse files
youvedeep-singhAnas Nashif
authored andcommitted
tests: posix: pthread_cancel: Added some APIs in test.
Added some APIs in test to increase code coverage. Signed-off-by: Youvedeep Singh <[email protected]>
1 parent d60ef8b commit dbb6511

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

tests/posix/pthread_cancel/src/pthread_cancel.c

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,23 @@ void *thread_top(void *p1)
2121
pthread_t self;
2222
int oldstate;
2323
int val = (u32_t) p1;
24+
struct sched_param param;
25+
26+
param.priority = N_THR - (s32_t) p1;
27+
28+
self = pthread_self();
29+
/* Change priority of thread */
30+
zassert_false(pthread_setschedparam(self, SCHED_RR, &param),
31+
"Unable to set thread priority");
2432

2533
if (val % 2) {
2634
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
2735
}
2836

29-
self = pthread_self();
37+
if (val >= 2) {
38+
pthread_detach(self);
39+
}
40+
3041
printk("Cancelling thread %d\n", (s32_t) p1);
3142
pthread_cancel(self);
3243
printk("Thread %d could not be cancelled\n", (s32_t) p1);
@@ -48,29 +59,29 @@ void test_pthread_cancel(void)
4859
printk("POSIX thread cancel APIs\n");
4960
/* Creating 4 threads with lowest application priority */
5061
for (i = 0; i < N_THR; i++) {
51-
pthread_attr_init(&attr[i]);
62+
ret = pthread_attr_init(&attr[i]);
63+
if (ret != 0) {
64+
zassert_false(pthread_attr_destroy(&attr[i]),
65+
"Unable to destroy pthread object attrib\n");
66+
zassert_false(pthread_attr_init(&attr[i]),
67+
"Unable to create pthread object attrib\n");
68+
}
5269

53-
if (i == 0) {
70+
if (i == 1) {
5471
pthread_attr_setdetachstate(&attr[i],
5572
PTHREAD_CREATE_JOINABLE);
56-
} else if (i == 1) {
57-
pthread_attr_setdetachstate(&attr[i],
58-
PTHREAD_CREATE_JOINABLE);
59-
} else if (i == 2) {
60-
pthread_attr_setdetachstate(&attr[i],
61-
PTHREAD_CREATE_DETACHED);
62-
} else if (i == 3) {
73+
} else if (i == 2) {
6374
pthread_attr_setdetachstate(&attr[i],
6475
PTHREAD_CREATE_DETACHED);
6576
}
77+
6678
schedparam.priority = 2;
6779
pthread_attr_setschedparam(&attr[i], &schedparam);
6880
pthread_attr_setstack(&attr[i], &stacks[i][0], STACKSZ);
6981
ret = pthread_create(&newthread[i], &attr[i], thread_top,
7082
(void *)i);
7183

72-
/*TESTPOINT: Check if thread is created successfully*/
73-
zassert_false(ret, "Number of threads exceeds max limit\n");
84+
zassert_false(ret, "Not enough space to create new thread\n");
7485
}
7586

7687
for (i = 0; i < N_THR; i++) {
@@ -79,12 +90,10 @@ void test_pthread_cancel(void)
7990
printk("Pthread %d joined to %s\n", i, __func__);
8091
}
8192

82-
printk("Pthread join test over\n");
93+
printk("Pthread join test over %d\n", exit_count);
8394

8495
/* Test PASS if all threads have exited before main exit */
8596
zassert_equal(exit_count, 1, "pthread_cancel test failed\n");
86-
87-
sleep(ONE_SECOND);
8897
}
8998

9099
void test_main(void)

0 commit comments

Comments
 (0)