Skip to content

Commit 26d597c

Browse files
committed
feature: implement simple spinlock test
Two threads use share variable and the variable should be same as excepted. Signed-off-by: TaiJu Wu <[email protected]>
1 parent 9921571 commit 26d597c

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

testing/ostest/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ if(CONFIG_TESTING_OSTEST)
136136
list(APPEND SRCS smp_call.c)
137137
endif()
138138

139+
if(CONFIG_SPINLOCK)
140+
list(APPEND SRCS spinlock.c)
141+
endif()
142+
139143
target_sources(apps PRIVATE ${SRCS})
140144

141145
nuttx_add_application(NAME ostest SRCS ostest_main.c)

testing/ostest/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,8 @@ ifeq ($(CONFIG_SMP_CALL),y)
137137
CSRCS += smp_call.c
138138
endif
139139

140+
ifeq ($(CONFIG_SPINLOCK),y)
141+
CSRCS += spinlock.c
142+
endif
143+
140144
include $(APPDIR)/Application.mk

testing/ostest/ostest.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,12 @@ void priority_inheritance(void);
256256

257257
void sched_lock_test(void);
258258

259+
/* spinlock.c ***************************************************************/
260+
261+
#if defined(CONFIG_SPINLOCK)
262+
void spinlock_test(void);
263+
#endif
264+
259265
/* vfork.c ******************************************************************/
260266

261267
#if defined(CONFIG_ARCH_HAVE_FORK) && defined(CONFIG_SCHED_WAITPID)

testing/ostest/ostest_main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,12 @@ static int user_main(int argc, char *argv[])
602602
smp_call_test();
603603
#endif
604604

605+
#if defined(CONFIG_SPINLOCK)
606+
printf("\nuser_main: spinlock test\n");
607+
spinlock_test();
608+
check_test_memory_usage();
609+
#endif
610+
605611
/* Compare memory usage at time ostest_main started until
606612
* user_main exits. These should not be identical, but should
607613
* be similar enough that we can detect any serious OS memory

testing/ostest/spinlock.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/****************************************************************************
2+
* apps/testing/ostest/spinlock.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 <assert.h>
27+
#include <errno.h>
28+
#include <nuttx/spinlock.h>
29+
30+
#include "ostest.h"
31+
32+
/****************************************************************************
33+
* Pre-processor Definitions
34+
****************************************************************************/
35+
36+
/****************************************************************************
37+
* Private Data
38+
****************************************************************************/
39+
40+
static spinlock_t lock = SP_UNLOCKED;
41+
42+
static pthread_t g_thread1;
43+
static pthread_t g_thread2;
44+
45+
static int g_result = 0;
46+
47+
static FAR void thread_spinlock(FAR void *parameter)
48+
{
49+
int pid = *(int *)parameter;
50+
51+
for (int i = 0; i < 10; i++)
52+
{
53+
printf("pid %d get lock g_result:%d\n", pid, g_result);
54+
spin_lock(&lock);
55+
g_result++;
56+
spin_unlock(&lock);
57+
printf("pid %d release lock g_result:%d\n", pid, g_result);
58+
}
59+
}
60+
61+
/****************************************************************************
62+
* Public Functions
63+
****************************************************************************/
64+
65+
void spinlock_test(void)
66+
{
67+
int status;
68+
g_result = 0;
69+
70+
status = pthread_create(&g_thread1, NULL,
71+
(void *)thread_spinlock, &g_thread1);
72+
if (status != 0)
73+
{
74+
printf("spinlock_test: ERROR pthread_create failed, status=%d\n",
75+
status);
76+
ASSERT(false);
77+
}
78+
79+
status = pthread_create(&g_thread2, NULL,
80+
(void *)thread_spinlock, &g_thread2);
81+
if (status != 0)
82+
{
83+
printf("spinlock_test: ERROR pthread_create failed, status=%d\n",
84+
status);
85+
ASSERT(false);
86+
}
87+
88+
pthread_join(g_thread1, NULL);
89+
pthread_join(g_thread2, NULL);
90+
91+
assert(g_result == 20);
92+
}

0 commit comments

Comments
 (0)