Skip to content

Commit bd66391

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

File tree

4 files changed

+274
-8
lines changed

4 files changed

+274
-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
ifneq ($(CONFIG_TLS_NELEM),0)
7069
CSRCS += specific.c
@@ -97,6 +96,10 @@ endif
9796
ifeq ($(CONFIG_SCHED_WORKQUEUE),y)
9897
CSRCS += wqueue.c
9998
endif
99+
100+
ifeq ($(CONFIG_PRIORITY_INHERITANCE),y)
101+
CSRCS += prioinherit.c
102+
endif
100103
endif # CONFIG_DISABLE_PTHREAD
101104

102105
ifneq ($(CONFIG_DISABLE_MQUEUE),y)
@@ -118,12 +121,6 @@ CSRCS += vfork.c
118121
endif
119122
endif
120123

121-
ifneq ($(CONFIG_DISABLE_PTHREAD),y)
122-
ifeq ($(CONFIG_PRIORITY_INHERITANCE),y)
123-
CSRCS += prioinherit.c
124-
endif # CONFIG_PRIORITY_INHERITANCE
125-
endif # CONFIG_DISABLE_PTHREAD
126-
127124
ifeq ($(CONFIG_ARCH_SETJMP_H),y)
128125
CSRCS += setjmp.c
129126
endif

testing/ostest/ostest.h

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

243243
void priority_inheritance(void);
244244

245+
/* schedlock.c **************************************************************/
246+
247+
void sched_lock_test(void);
248+
245249
/* vfork.c ******************************************************************/
246250

247251
#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
@@ -564,6 +564,12 @@ static int user_main(int argc, char *argv[])
564564
check_test_memory_usage();
565565
#endif /* CONFIG_PRIORITY_INHERITANCE && !CONFIG_DISABLE_PTHREAD */
566566

567+
#ifndef CONFIG_DISABLE_PTHREAD
568+
printf("\nuser_main: scheduler lock test\n");
569+
sched_lock_test();
570+
check_test_memory_usage();
571+
#endif
572+
567573
#if defined(CONFIG_ARCH_HAVE_VFORK) && defined(CONFIG_SCHED_WAITPID)
568574
printf("\nuser_main: vfork() test\n");
569575
vfork_test();

testing/ostest/schedlock.c

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
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+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <stdio.h>
26+
#include <unistd.h>
27+
#include <pthread.h>
28+
#include <errno.h>
29+
#include <sched.h>
30+
#include <unistd.h>
31+
#include <stdint.h>
32+
33+
#ifdef CONFIG_ARCH_SIM
34+
# include <nuttx/arch.h>
35+
#endif
36+
37+
#include <sys/wait.h>
38+
39+
#include "ostest.h"
40+
41+
static pthread_t g_lowpri;
42+
static pthread_t g_highpri;
43+
static volatile bool g_locked;
44+
static volatile bool g_pass;
45+
46+
/****************************************************************************
47+
* Private Functions
48+
****************************************************************************/
49+
50+
/****************************************************************************
51+
* Name: highpri_thread
52+
****************************************************************************/
53+
54+
static FAR void *highpri_thread(FAR void *parameter)
55+
{
56+
struct sched_param param;
57+
int policy;
58+
bool self = (bool)(uintptr_t)parameter;
59+
pthread_t thread = self ? 0 : g_lowpri;
60+
61+
usleep(100);
62+
63+
pthread_getschedparam(0, &policy, &param);
64+
if (self)
65+
{
66+
param.sched_priority -= 2;
67+
}
68+
else
69+
{
70+
param.sched_priority += 2;
71+
}
72+
73+
sched_lock();
74+
75+
g_locked = true;
76+
77+
pthread_setschedprio(thread, param.sched_priority);
78+
79+
/* Test pass if g_locked was not cleared by lowpri thread while scheduler
80+
* is locked
81+
*/
82+
83+
g_pass = g_locked;
84+
85+
sched_unlock();
86+
87+
return NULL;
88+
}
89+
90+
/****************************************************************************
91+
* Name: lowpri_thread
92+
****************************************************************************/
93+
94+
static FAR void *lowpri_thread(FAR void *parameter)
95+
{
96+
/* Wait until highpri thread starts the scheduler lock test */
97+
98+
while (!g_locked)
99+
{
100+
#ifdef CONFIG_ARCH_SIM
101+
/* The simulator doesn't have any mechanism to do asynchronous
102+
* pre-emption (basically because it doesn't have any
103+
* interrupts/asynchronous events). The simulator does "fake" a timer
104+
* interrupt in up_idle() -- the idle thread that only executes when
105+
* nothing else is running. In the simulator, we cannot suspend the
106+
* middle priority task, or we wouldn't have the test that we want.
107+
* So, we have no option but to pump the fake clock here by calling
108+
* up_idle(). Sigh!
109+
*/
110+
111+
up_idle();
112+
#endif
113+
}
114+
115+
g_locked = false;
116+
117+
return NULL;
118+
}
119+
120+
/****************************************************************************
121+
* Public Functions
122+
****************************************************************************/
123+
124+
/****************************************************************************
125+
* Name: sched_lock_test
126+
****************************************************************************/
127+
128+
void sched_lock_test(void)
129+
{
130+
int i;
131+
132+
for (i = 0; i < 2; i++)
133+
{
134+
pthread_attr_t attr;
135+
struct sched_param sparam;
136+
int status;
137+
int highprio;
138+
int lowprio;
139+
#ifdef CONFIG_SMP
140+
cpu_set_t cpu_mask;
141+
142+
CPU_ZERO(&cpu_mask);
143+
CPU_SET(0, &cpu_mask);
144+
#endif
145+
146+
status = sched_getparam(0, &sparam);
147+
if (status != 0)
148+
{
149+
printf("sched_lock: ERROR sched_getparam failed\n");
150+
ASSERT(false);
151+
sparam.sched_priority = PTHREAD_DEFAULT_PRIORITY;
152+
}
153+
154+
highprio = sparam.sched_priority - 2;
155+
lowprio = highprio - 1;
156+
157+
/* Start the low priority thread */
158+
159+
printf("sched_lock: Starting lowpri_thread at %d\n", lowprio);
160+
status = pthread_attr_init(&attr);
161+
if (status != 0)
162+
{
163+
printf("sched_lock: ERROR pthread_attr_init failed, status=%d\n",
164+
status);
165+
ASSERT(false);
166+
}
167+
168+
sparam.sched_priority = lowprio;
169+
status = pthread_attr_setschedparam(&attr, &sparam);
170+
if (status != OK)
171+
{
172+
printf("sched_lock: "
173+
"ERROR pthread_attr_setschedparam failed, status=%d\n",
174+
status);
175+
ASSERT(false);
176+
}
177+
else
178+
{
179+
printf("sched_lock: Set lowpri_thread priority to %d\n",
180+
sparam.sched_priority);
181+
}
182+
183+
#ifdef CONFIG_SMP
184+
pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpu_mask);
185+
#endif
186+
187+
FFLUSH();
188+
189+
status = pthread_create(&g_lowpri, &attr, lowpri_thread, NULL);
190+
if (status != 0)
191+
{
192+
printf("sched_lock: ERROR pthread_create failed, status=%d\n",
193+
status);
194+
ASSERT(false);
195+
}
196+
197+
/* Start the high priority thread */
198+
199+
printf("sched_lock: Starting highpri_thread at %d\n", highprio);
200+
status = pthread_attr_init(&attr);
201+
if (status != 0)
202+
{
203+
printf("sched_lock: ERROR pthread_attr_init failed, status=%d\n",
204+
status);
205+
ASSERT(false);
206+
}
207+
208+
sparam.sched_priority = highprio;
209+
status = pthread_attr_setschedparam(&attr, &sparam);
210+
if (status != OK)
211+
{
212+
printf("sched_lock: "
213+
"ERROR pthread_attr_setschedparam failed, status=%d\n",
214+
status);
215+
ASSERT(false);
216+
}
217+
else
218+
{
219+
printf("sched_lock: Set highpri_thread priority to %d\n",
220+
sparam.sched_priority);
221+
}
222+
223+
#ifdef CONFIG_SMP
224+
pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpu_mask);
225+
#endif
226+
227+
FFLUSH();
228+
229+
status = pthread_create(&g_highpri, &attr, highpri_thread,
230+
(FAR void *)(uintptr_t)(i == 0));
231+
if (status != 0)
232+
{
233+
printf("sched_lock: ERROR pthread_create failed, status=%d\n",
234+
status);
235+
ASSERT(false);
236+
}
237+
238+
printf("sched_lock: Waiting...\n");
239+
sleep(1);
240+
241+
pthread_join(g_highpri, NULL);
242+
pthread_join(g_lowpri, NULL);
243+
244+
if (!g_pass)
245+
{
246+
printf("sched_lock: ERROR: FAIL pre-emption occurred "
247+
"while scheduler was locked.\n");
248+
ASSERT(false);
249+
}
250+
else
251+
{
252+
printf("sched_lock: PASSED No pre-emption occurred "
253+
"while scheduler was locked.\n");
254+
}
255+
}
256+
257+
printf("sched_lock: Finished\n");
258+
FFLUSH();
259+
}

0 commit comments

Comments
 (0)