Skip to content

Commit 08644a3

Browse files
committed
testing/ostest: add test case for task priority change with locked scheduler
Signed-off-by: Petro Karashchenko <[email protected]>
1 parent c156dec commit 08644a3

File tree

4 files changed

+289
-8
lines changed

4 files changed

+289
-8
lines changed

testing/ostest/Makefile

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ endif
6363

6464
ifneq ($(CONFIG_DISABLE_PTHREAD),y)
6565
CSRCS += cancel.c cond.c mutex.c timedmutex.c sem.c semtimed.c barrier.c
66-
CSRCS += timedwait.c
67-
CSRCS += pthread_rwlock.c pthread_rwlock_cancel.c
66+
CSRCS += timedwait.c pthread_rwlock.c pthread_rwlock_cancel.c schedlock.c
6867

6968
ifeq ($(CONFIG_SCHED_WAITPID),y)
7069
CSRCS += pthread_exit.c
@@ -101,6 +100,10 @@ endif
101100
ifeq ($(CONFIG_SCHED_WORKQUEUE),y)
102101
CSRCS += wqueue.c
103102
endif
103+
104+
ifeq ($(CONFIG_PRIORITY_INHERITANCE),y)
105+
CSRCS += prioinherit.c
106+
endif
104107
endif # CONFIG_DISABLE_PTHREAD
105108

106109
ifneq ($(CONFIG_DISABLE_MQUEUE),y)
@@ -122,12 +125,6 @@ CSRCS += vfork.c
122125
endif
123126
endif
124127

125-
ifneq ($(CONFIG_DISABLE_PTHREAD),y)
126-
ifeq ($(CONFIG_PRIORITY_INHERITANCE),y)
127-
CSRCS += prioinherit.c
128-
endif # CONFIG_PRIORITY_INHERITANCE
129-
endif # CONFIG_DISABLE_PTHREAD
130-
131128
ifeq ($(CONFIG_ARCH_SETJMP_H),y)
132129
CSRCS += setjmp.c
133130
endif

testing/ostest/ostest.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ void barrier_test(void);
248248

249249
void priority_inheritance(void);
250250

251+
/* schedlock.c **************************************************************/
252+
253+
void sched_lock_test(void);
254+
251255
/* vfork.c ******************************************************************/
252256

253257
#if defined(CONFIG_ARCH_HAVE_VFORK) && defined(CONFIG_SCHED_WAITPID)

testing/ostest/ostest_main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,12 @@ static int user_main(int argc, char *argv[])
572572
check_test_memory_usage();
573573
#endif /* CONFIG_PRIORITY_INHERITANCE && !CONFIG_DISABLE_PTHREAD */
574574

575+
#ifndef CONFIG_DISABLE_PTHREAD
576+
printf("\nuser_main: scheduler lock test\n");
577+
sched_lock_test();
578+
check_test_memory_usage();
579+
#endif
580+
575581
#if defined(CONFIG_ARCH_HAVE_VFORK) && defined(CONFIG_SCHED_WAITPID)
576582
printf("\nuser_main: vfork() test\n");
577583
vfork_test();

testing/ostest/schedlock.c

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
/****************************************************************************
2+
* apps/testing/ostest/schedlock.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/* Scheduler lock test consists of two test cases that use 2 threads with
22+
* high (H) and low (L) priorities. The H sets variable to predefined value
23+
* while L tries to clear the variable. The H manipulates with with thread
24+
* priorities while scheduler is locked. The test is pass if variable set by
25+
* H is still set to predefined value after thread priority is changed.
26+
*
27+
* Test case 1. The H thread locks scheduler and increase priority of L
28+
* to the H+1 priority. The test case passes if H is still
29+
* running after priority of L is increased.
30+
* Test case 2. The H thread locks scheduler and decreases it's own
31+
* priority to L-1. The test case passes if H is still
32+
* running after it's priority is decreased.
33+
*
34+
*/
35+
36+
/****************************************************************************
37+
* Included Files
38+
****************************************************************************/
39+
40+
#include <stdio.h>
41+
#include <unistd.h>
42+
#include <pthread.h>
43+
#include <errno.h>
44+
#include <sched.h>
45+
#include <unistd.h>
46+
#include <stdint.h>
47+
48+
#ifdef CONFIG_ARCH_SIM
49+
# include <nuttx/arch.h>
50+
#endif
51+
52+
#include <sys/wait.h>
53+
54+
#include "ostest.h"
55+
56+
static pthread_t g_lowpri;
57+
static pthread_t g_highpri;
58+
static volatile bool g_locked;
59+
static volatile bool g_pass;
60+
61+
/****************************************************************************
62+
* Private Functions
63+
****************************************************************************/
64+
65+
/****************************************************************************
66+
* Name: highpri_thread
67+
****************************************************************************/
68+
69+
static FAR void *highpri_thread(FAR void *parameter)
70+
{
71+
struct sched_param param;
72+
int policy;
73+
bool self = (bool)(uintptr_t)parameter;
74+
pthread_t thread = self ? 0 : g_lowpri;
75+
76+
usleep(100);
77+
78+
pthread_getschedparam(0, &policy, &param);
79+
if (self)
80+
{
81+
param.sched_priority -= 2;
82+
}
83+
else
84+
{
85+
param.sched_priority += 2;
86+
}
87+
88+
sched_lock();
89+
90+
g_locked = true;
91+
92+
pthread_setschedprio(thread, param.sched_priority);
93+
94+
/* Test pass if g_locked was not cleared by lowpri thread while scheduler
95+
* is locked
96+
*/
97+
98+
g_pass = g_locked;
99+
100+
sched_unlock();
101+
102+
return NULL;
103+
}
104+
105+
/****************************************************************************
106+
* Name: lowpri_thread
107+
****************************************************************************/
108+
109+
static FAR void *lowpri_thread(FAR void *parameter)
110+
{
111+
/* Wait until highpri thread starts the scheduler lock test */
112+
113+
while (!g_locked)
114+
{
115+
#ifdef CONFIG_ARCH_SIM
116+
/* The simulator doesn't have any mechanism to do asynchronous
117+
* pre-emption (basically because it doesn't have any
118+
* interrupts/asynchronous events). The simulator does "fake" a timer
119+
* interrupt in up_idle() -- the idle thread that only executes when
120+
* nothing else is running. In the simulator, we cannot suspend the
121+
* middle priority task, or we wouldn't have the test that we want.
122+
* So, we have no option but to pump the fake clock here by calling
123+
* up_idle(). Sigh!
124+
*/
125+
126+
up_idle();
127+
#endif
128+
}
129+
130+
g_locked = false;
131+
132+
return NULL;
133+
}
134+
135+
/****************************************************************************
136+
* Public Functions
137+
****************************************************************************/
138+
139+
/****************************************************************************
140+
* Name: sched_lock_test
141+
****************************************************************************/
142+
143+
void sched_lock_test(void)
144+
{
145+
int i;
146+
147+
for (i = 0; i < 2; i++)
148+
{
149+
pthread_attr_t attr;
150+
struct sched_param sparam;
151+
int status;
152+
int highprio;
153+
int lowprio;
154+
#ifdef CONFIG_SMP
155+
cpu_set_t cpu_mask;
156+
157+
CPU_ZERO(&cpu_mask);
158+
CPU_SET(0, &cpu_mask);
159+
#endif
160+
161+
status = sched_getparam(0, &sparam);
162+
if (status != 0)
163+
{
164+
printf("sched_lock: ERROR sched_getparam failed\n");
165+
ASSERT(false);
166+
sparam.sched_priority = PTHREAD_DEFAULT_PRIORITY;
167+
}
168+
169+
highprio = sparam.sched_priority - 2;
170+
lowprio = highprio - 1;
171+
172+
/* Start the low priority thread */
173+
174+
printf("sched_lock: Starting lowpri_thread at %d\n", lowprio);
175+
status = pthread_attr_init(&attr);
176+
if (status != 0)
177+
{
178+
printf("sched_lock: ERROR pthread_attr_init failed, status=%d\n",
179+
status);
180+
ASSERT(false);
181+
}
182+
183+
sparam.sched_priority = lowprio;
184+
status = pthread_attr_setschedparam(&attr, &sparam);
185+
if (status != OK)
186+
{
187+
printf("sched_lock: "
188+
"ERROR pthread_attr_setschedparam failed, status=%d\n",
189+
status);
190+
ASSERT(false);
191+
}
192+
else
193+
{
194+
printf("sched_lock: Set lowpri_thread priority to %d\n",
195+
sparam.sched_priority);
196+
}
197+
198+
#ifdef CONFIG_SMP
199+
pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpu_mask);
200+
#endif
201+
202+
FFLUSH();
203+
204+
status = pthread_create(&g_lowpri, &attr, lowpri_thread, NULL);
205+
if (status != 0)
206+
{
207+
printf("sched_lock: ERROR pthread_create failed, status=%d\n",
208+
status);
209+
ASSERT(false);
210+
}
211+
212+
/* Start the high priority thread */
213+
214+
printf("sched_lock: Starting highpri_thread at %d\n", highprio);
215+
status = pthread_attr_init(&attr);
216+
if (status != 0)
217+
{
218+
printf("sched_lock: ERROR pthread_attr_init failed, status=%d\n",
219+
status);
220+
ASSERT(false);
221+
}
222+
223+
sparam.sched_priority = highprio;
224+
status = pthread_attr_setschedparam(&attr, &sparam);
225+
if (status != OK)
226+
{
227+
printf("sched_lock: "
228+
"ERROR pthread_attr_setschedparam failed, status=%d\n",
229+
status);
230+
ASSERT(false);
231+
}
232+
else
233+
{
234+
printf("sched_lock: Set highpri_thread priority to %d\n",
235+
sparam.sched_priority);
236+
}
237+
238+
#ifdef CONFIG_SMP
239+
pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpu_mask);
240+
#endif
241+
242+
FFLUSH();
243+
244+
status = pthread_create(&g_highpri, &attr, highpri_thread,
245+
(FAR void *)(uintptr_t)(i == 0));
246+
if (status != 0)
247+
{
248+
printf("sched_lock: ERROR pthread_create failed, status=%d\n",
249+
status);
250+
ASSERT(false);
251+
}
252+
253+
printf("sched_lock: Waiting...\n");
254+
sleep(1);
255+
256+
pthread_join(g_highpri, NULL);
257+
pthread_join(g_lowpri, NULL);
258+
259+
if (!g_pass)
260+
{
261+
printf("sched_lock: ERROR: FAIL pre-emption occurred "
262+
"while scheduler was locked.\n");
263+
ASSERT(false);
264+
}
265+
else
266+
{
267+
printf("sched_lock: PASSED No pre-emption occurred "
268+
"while scheduler was locked.\n");
269+
}
270+
}
271+
272+
printf("sched_lock: Finished\n");
273+
FFLUSH();
274+
}

0 commit comments

Comments
 (0)