Skip to content

Commit 2e43d00

Browse files
youvedeep-singhAnas Nashif
authored andcommitted
tests: kernel: posix: pthread_join: Add pthread join test.
Implement pthread_join test application. Signed-off-by: Youvedeep Singh <[email protected]>
1 parent 7903c84 commit 2e43d00

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
2+
project(NONE)
3+
4+
target_include_directories(app PRIVATE $ENV{ZEPHYR_BASE}/include/posix)
5+
FILE(GLOB app_sources src/*.c)
6+
target_sources(app PRIVATE ${app_sources})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_TEST=y
2+
CONFIG_PTHREAD_IPC=y
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (c) 2018 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <tc_util.h>
8+
#include <pthread.h>
9+
10+
#define N_THR 3
11+
#define STACKSZ 1024
12+
#define ONE_SECOND 1
13+
#define THREAD_PRIORITY 2
14+
15+
K_THREAD_STACK_ARRAY_DEFINE(stacks, N_THR, STACKSZ);
16+
int exit_count;
17+
18+
void *thread_top(void *p1)
19+
{
20+
pthread_t pthread;
21+
u32_t policy, seconds;
22+
struct sched_param param;
23+
24+
pthread = (pthread_t) pthread_self();
25+
pthread_getschedparam(pthread, &policy, &param);
26+
TC_PRINT("Thread %d scheduling policy = %d & priority %d started\n",
27+
(s32_t) p1, policy, param.priority);
28+
seconds = (s32_t)p1;
29+
sleep(seconds * ONE_SECOND);
30+
exit_count++;
31+
TC_PRINT("Exiting thread %d\n", (s32_t)p1);
32+
pthread_exit(p1);
33+
return NULL;
34+
}
35+
36+
static bool is_sched_prio_valid(int prio, int policy)
37+
{
38+
if (prio < sched_get_priority_min(policy) ||
39+
prio > sched_get_priority_max(policy)) {
40+
return false;
41+
}
42+
43+
return true;
44+
}
45+
46+
void main(void)
47+
{
48+
s32_t i, ret, status = TC_FAIL;
49+
pthread_attr_t attr[N_THR];
50+
struct sched_param schedparam;
51+
pthread_t newthread[N_THR];
52+
u32_t detachstate, schedpolicy = SCHED_RR;
53+
void *retval;
54+
size_t stack_size;
55+
56+
TC_START("POSIX pthread join API");
57+
/* Creating threads with lowest application priority */
58+
for (i = 0; i < N_THR; i++) {
59+
pthread_attr_init(&attr[i]);
60+
61+
/* Setting pthread as JOINABLE */
62+
pthread_attr_getdetachstate(&attr[i], &detachstate);
63+
if (detachstate != PTHREAD_CREATE_JOINABLE) {
64+
pthread_attr_setdetachstate(&attr[i],
65+
PTHREAD_CREATE_JOINABLE);
66+
}
67+
68+
/* Setting premptive scheduling policy */
69+
pthread_attr_getschedpolicy(&attr[i], &schedpolicy);
70+
if (schedpolicy != SCHED_RR) {
71+
schedpolicy = SCHED_RR;
72+
pthread_attr_setschedpolicy(&attr[i], schedpolicy);
73+
}
74+
75+
/* Setting scheduling priority */
76+
pthread_attr_getschedparam(&attr[i], &schedparam);
77+
if (schedparam.priority != THREAD_PRIORITY) {
78+
schedparam.priority = THREAD_PRIORITY;
79+
if (is_sched_prio_valid(schedparam.priority,
80+
schedpolicy)) {
81+
pthread_attr_setschedparam(&attr[i],
82+
&schedparam);
83+
} else {
84+
TC_ERROR("Scheduling priority invalid\n");
85+
goto done;
86+
}
87+
}
88+
89+
/* Setting stack */
90+
pthread_attr_getstacksize(&attr[i], &stack_size);
91+
if (stack_size != STACKSZ) {
92+
stack_size = STACKSZ;
93+
pthread_attr_setstack(&attr[i], &stacks[i][0],
94+
stack_size);
95+
}
96+
97+
ret = pthread_create(&newthread[i], &attr[i], thread_top,
98+
(void *)i);
99+
if (ret != 0) {
100+
TC_ERROR("Number of threads exceeds Maximum Limit\n");
101+
goto done;
102+
}
103+
104+
pthread_attr_destroy(&attr[i]);
105+
}
106+
107+
for (i = 0; i < N_THR; i++) {
108+
TC_PRINT("Waiting for pthread %d to Join\n", i);
109+
pthread_join(newthread[i], &retval);
110+
TC_PRINT("Pthread %d joined to %s\n", i, __func__);
111+
}
112+
113+
/* Test PASS if all threads have exited before main exit */
114+
status = exit_count == N_THR ? TC_PASS : TC_FAIL;
115+
done:
116+
TC_END_REPORT(status);
117+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
tests:
2+
test:
3+
tags: core

0 commit comments

Comments
 (0)