Skip to content

Commit 7482406

Browse files
Andrew BoieAnas Nashif
authored andcommitted
samples: restore cpp_synchronization test
Issue: ZEP-2172 Signed-off-by: Andrew Boie <[email protected]>
1 parent 6a2084e commit 7482406

File tree

6 files changed

+230
-0
lines changed

6 files changed

+230
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
BOARD ?= qemu_x86
2+
CONF_FILE = prj.conf
3+
4+
5+
include ${ZEPHYR_BASE}/Makefile.inc
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Title: C++ Synchronization
2+
3+
Description:
4+
The sample project illustrates usage of pure virtual class, member
5+
functions with different types of arguments, global objects constructor
6+
invocation.
7+
8+
A simple application demonstrates basic sanity of the kernel. The main thread
9+
and a cooperative thread take turns printing a greeting message to the console,
10+
and use timers and semaphores to control the rate at which messages are
11+
generated. This demonstrates that kernel scheduling, communication, and
12+
timing are operating correctly.
13+
14+
--------------------------------------------------------------------------------
15+
16+
Building and Running Project:
17+
18+
This kernel project outputs to the console. It can be built and executed
19+
on QEMU as follows:
20+
21+
make run
22+
23+
--------------------------------------------------------------------------------
24+
25+
Troubleshooting:
26+
27+
Problems caused by out-dated project information can be addressed by
28+
issuing one of the following commands then rebuilding the project:
29+
30+
make clean # discard results of previous builds
31+
# but keep existing configuration info
32+
or
33+
make pristine # discard results of previous builds
34+
# and restore pre-defined configuration info
35+
36+
--------------------------------------------------------------------------------
37+
38+
Sample Output:
39+
40+
Create semaphore 0x001042b0
41+
Create semaphore 0x001042c4
42+
main: Hello World!
43+
coop_thread_entry: Hello World!
44+
main: Hello World!
45+
coop_thread_entry: Hello World!
46+
main: Hello World!
47+
coop_thread_entry: Hello World!
48+
main: Hello World!
49+
coop_thread_entry: Hello World!
50+
main: Hello World!
51+
coop_thread_entry: Hello World!
52+
main: Hello World!
53+
coop_thread_entry: Hello World!
54+
main: Hello World!
55+
coop_thread_entry: Hello World!
56+
main: Hello World!
57+
coop_thread_entry: Hello World!
58+
main: Hello World!
59+
coop_thread_entry: Hello World!
60+
main: Hello World!
61+
62+
<repeats endlessly>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_CPLUSPLUS=y
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
obj-y = main.o
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
* Copyright (c) 2015-2016 Wind River Systems, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* @file C++ Synchronization demo. Uses basic C++ functionality.
19+
*/
20+
21+
#include <stdio.h>
22+
#include <zephyr.h>
23+
#include <arch/cpu.h>
24+
#include <misc/printk.h>
25+
26+
/**
27+
* @class semaphore the basic pure virtual semaphore class
28+
*/
29+
class semaphore {
30+
public:
31+
virtual int wait(void) = 0;
32+
virtual int wait(int timeout) = 0;
33+
virtual void give(void) = 0;
34+
};
35+
36+
/* specify delay between greetings (in ms); compute equivalent in ticks */
37+
#define SLEEPTIME 500
38+
#define STACKSIZE 2000
39+
40+
struct k_thread coop_thread;
41+
char __stack coop_stack[STACKSIZE];
42+
43+
/*
44+
* @class cpp_semaphore
45+
* @brief nano semaphore
46+
*
47+
* Class derives from the pure virtual semaphore class and
48+
* implements it's methods for the nanokernel semaphore
49+
*/
50+
class cpp_semaphore: public semaphore {
51+
protected:
52+
struct k_sem _sema_internal;
53+
public:
54+
cpp_semaphore();
55+
virtual ~cpp_semaphore() {}
56+
virtual int wait(void);
57+
virtual int wait(int timeout);
58+
virtual void give(void);
59+
};
60+
61+
/*
62+
* @brief cpp_semaphore basic constructor
63+
*/
64+
cpp_semaphore::cpp_semaphore()
65+
{
66+
printk("Create semaphore %p\n", this);
67+
k_sem_init(&_sema_internal, 0, UINT_MAX);
68+
}
69+
70+
/*
71+
* @brief wait for a semaphore
72+
*
73+
* Test a semaphore to see if it has been signaled. If the signal
74+
* count is greater than zero, it is decremented.
75+
*
76+
* @return 1 when semaphore is available
77+
*/
78+
int cpp_semaphore::wait(void)
79+
{
80+
k_sem_take(&_sema_internal, K_FOREVER);
81+
return 1;
82+
}
83+
84+
/*
85+
* @brief wait for a semaphore within a specified timeout
86+
*
87+
* Test a semaphore to see if it has been signaled. If the signal
88+
* count is greater than zero, it is decremented. The function
89+
* waits for timeout specified
90+
*
91+
* @param timeout the specified timeout in ticks
92+
*
93+
* @return 1 if semaphore is available, 0 if timed out
94+
*/
95+
int cpp_semaphore::wait(int timeout)
96+
{
97+
return k_sem_take(&_sema_internal, timeout);
98+
}
99+
100+
/**
101+
*
102+
* @brief Signal a semaphore
103+
*
104+
* This routine signals the specified semaphore.
105+
*
106+
* @return N/A
107+
*/
108+
void cpp_semaphore::give(void)
109+
{
110+
k_sem_give(&_sema_internal);
111+
}
112+
113+
cpp_semaphore sem_main;
114+
cpp_semaphore sem_coop;
115+
116+
void coop_thread_entry(void)
117+
{
118+
struct k_timer timer;
119+
120+
k_timer_init(&timer, NULL, NULL);
121+
122+
while (1) {
123+
/* wait for main thread to let us have a turn */
124+
sem_coop.wait();
125+
126+
/* say "hello" */
127+
printk("%s: Hello World!\n", __FUNCTION__);
128+
129+
/* wait a while, then let main thread have a turn */
130+
k_timer_start(&timer, SLEEPTIME, 0);
131+
k_timer_status_sync(&timer);
132+
sem_main.give();
133+
}
134+
}
135+
136+
void main(void)
137+
{
138+
struct k_timer timer;
139+
140+
k_thread_create(&coop_thread, coop_stack, STACKSIZE,
141+
(k_thread_entry_t) coop_thread_entry,
142+
NULL, NULL, NULL, K_PRIO_COOP(7), 0, 0);
143+
k_timer_init(&timer, NULL, NULL);
144+
145+
while (1) {
146+
/* say "hello" */
147+
printk("%s: Hello World!\n", __FUNCTION__);
148+
149+
/* wait a while, then let coop thread have a turn */
150+
k_timer_start(&timer, SLEEPTIME, 0);
151+
k_timer_status_sync(&timer);
152+
sem_coop.give();
153+
154+
/* Wait for coop thread to let us have a turn */
155+
sem_main.wait();
156+
}
157+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[test]
2+
build_only = true
3+
tags = apps
4+
filter = ZEPHYR_GCC_VARIANT != "issm"

0 commit comments

Comments
 (0)