samples: restore cpp_synchronization test
Issue: ZEP-2172
Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
diff --git a/samples/cpp_synchronization/Makefile b/samples/cpp_synchronization/Makefile
new file mode 100644
index 0000000..b3884da
--- /dev/null
+++ b/samples/cpp_synchronization/Makefile
@@ -0,0 +1,5 @@
+BOARD ?= qemu_x86
+CONF_FILE = prj.conf
+
+
+include ${ZEPHYR_BASE}/Makefile.inc
diff --git a/samples/cpp_synchronization/README.txt b/samples/cpp_synchronization/README.txt
new file mode 100644
index 0000000..f25f7e8
--- /dev/null
+++ b/samples/cpp_synchronization/README.txt
@@ -0,0 +1,62 @@
+Title: C++ Synchronization
+
+Description:
+The sample project illustrates usage of pure virtual class, member
+functions with different types of arguments, global objects constructor
+invocation.
+
+A simple application demonstrates basic sanity of the kernel. The main thread
+and a cooperative thread take turns printing a greeting message to the console,
+and use timers and semaphores to control the rate at which messages are
+generated. This demonstrates that kernel scheduling, communication, and
+timing are operating correctly.
+
+--------------------------------------------------------------------------------
+
+Building and Running Project:
+
+This kernel project outputs to the console. It can be built and executed
+on QEMU as follows:
+
+ make run
+
+--------------------------------------------------------------------------------
+
+Troubleshooting:
+
+Problems caused by out-dated project information can be addressed by
+issuing one of the following commands then rebuilding the project:
+
+ make clean # discard results of previous builds
+ # but keep existing configuration info
+or
+ make pristine # discard results of previous builds
+ # and restore pre-defined configuration info
+
+--------------------------------------------------------------------------------
+
+Sample Output:
+
+Create semaphore 0x001042b0
+Create semaphore 0x001042c4
+main: Hello World!
+coop_thread_entry: Hello World!
+main: Hello World!
+coop_thread_entry: Hello World!
+main: Hello World!
+coop_thread_entry: Hello World!
+main: Hello World!
+coop_thread_entry: Hello World!
+main: Hello World!
+coop_thread_entry: Hello World!
+main: Hello World!
+coop_thread_entry: Hello World!
+main: Hello World!
+coop_thread_entry: Hello World!
+main: Hello World!
+coop_thread_entry: Hello World!
+main: Hello World!
+coop_thread_entry: Hello World!
+main: Hello World!
+
+<repeats endlessly>
diff --git a/samples/cpp_synchronization/prj.conf b/samples/cpp_synchronization/prj.conf
new file mode 100644
index 0000000..fa7da80
--- /dev/null
+++ b/samples/cpp_synchronization/prj.conf
@@ -0,0 +1 @@
+CONFIG_CPLUSPLUS=y
diff --git a/samples/cpp_synchronization/src/Makefile b/samples/cpp_synchronization/src/Makefile
new file mode 100644
index 0000000..00066e1
--- /dev/null
+++ b/samples/cpp_synchronization/src/Makefile
@@ -0,0 +1 @@
+obj-y = main.o
diff --git a/samples/cpp_synchronization/src/main.cpp b/samples/cpp_synchronization/src/main.cpp
new file mode 100644
index 0000000..c95207d
--- /dev/null
+++ b/samples/cpp_synchronization/src/main.cpp
@@ -0,0 +1,157 @@
+/*
+ * Copyright (c) 2015-2016 Wind River Systems, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file C++ Synchronization demo. Uses basic C++ functionality.
+ */
+
+#include <stdio.h>
+#include <zephyr.h>
+#include <arch/cpu.h>
+#include <misc/printk.h>
+
+/**
+ * @class semaphore the basic pure virtual semaphore class
+ */
+class semaphore {
+public:
+ virtual int wait(void) = 0;
+ virtual int wait(int timeout) = 0;
+ virtual void give(void) = 0;
+};
+
+/* specify delay between greetings (in ms); compute equivalent in ticks */
+#define SLEEPTIME 500
+#define STACKSIZE 2000
+
+struct k_thread coop_thread;
+char __stack coop_stack[STACKSIZE];
+
+/*
+ * @class cpp_semaphore
+ * @brief nano semaphore
+ *
+ * Class derives from the pure virtual semaphore class and
+ * implements it's methods for the nanokernel semaphore
+ */
+class cpp_semaphore: public semaphore {
+protected:
+ struct k_sem _sema_internal;
+public:
+ cpp_semaphore();
+ virtual ~cpp_semaphore() {}
+ virtual int wait(void);
+ virtual int wait(int timeout);
+ virtual void give(void);
+};
+
+/*
+ * @brief cpp_semaphore basic constructor
+ */
+cpp_semaphore::cpp_semaphore()
+{
+ printk("Create semaphore %p\n", this);
+ k_sem_init(&_sema_internal, 0, UINT_MAX);
+}
+
+/*
+ * @brief wait for a semaphore
+ *
+ * Test a semaphore to see if it has been signaled. If the signal
+ * count is greater than zero, it is decremented.
+ *
+ * @return 1 when semaphore is available
+ */
+int cpp_semaphore::wait(void)
+{
+ k_sem_take(&_sema_internal, K_FOREVER);
+ return 1;
+}
+
+/*
+ * @brief wait for a semaphore within a specified timeout
+ *
+ * Test a semaphore to see if it has been signaled. If the signal
+ * count is greater than zero, it is decremented. The function
+ * waits for timeout specified
+ *
+ * @param timeout the specified timeout in ticks
+ *
+ * @return 1 if semaphore is available, 0 if timed out
+ */
+int cpp_semaphore::wait(int timeout)
+{
+ return k_sem_take(&_sema_internal, timeout);
+}
+
+/**
+ *
+ * @brief Signal a semaphore
+ *
+ * This routine signals the specified semaphore.
+ *
+ * @return N/A
+ */
+void cpp_semaphore::give(void)
+{
+ k_sem_give(&_sema_internal);
+}
+
+cpp_semaphore sem_main;
+cpp_semaphore sem_coop;
+
+void coop_thread_entry(void)
+{
+ struct k_timer timer;
+
+ k_timer_init(&timer, NULL, NULL);
+
+ while (1) {
+ /* wait for main thread to let us have a turn */
+ sem_coop.wait();
+
+ /* say "hello" */
+ printk("%s: Hello World!\n", __FUNCTION__);
+
+ /* wait a while, then let main thread have a turn */
+ k_timer_start(&timer, SLEEPTIME, 0);
+ k_timer_status_sync(&timer);
+ sem_main.give();
+ }
+}
+
+void main(void)
+{
+ struct k_timer timer;
+
+ k_thread_create(&coop_thread, coop_stack, STACKSIZE,
+ (k_thread_entry_t) coop_thread_entry,
+ NULL, NULL, NULL, K_PRIO_COOP(7), 0, 0);
+ k_timer_init(&timer, NULL, NULL);
+
+ while (1) {
+ /* say "hello" */
+ printk("%s: Hello World!\n", __FUNCTION__);
+
+ /* wait a while, then let coop thread have a turn */
+ k_timer_start(&timer, SLEEPTIME, 0);
+ k_timer_status_sync(&timer);
+ sem_coop.give();
+
+ /* Wait for coop thread to let us have a turn */
+ sem_main.wait();
+ }
+}
diff --git a/samples/cpp_synchronization/testcase.ini b/samples/cpp_synchronization/testcase.ini
new file mode 100644
index 0000000..a1fa5b6
--- /dev/null
+++ b/samples/cpp_synchronization/testcase.ini
@@ -0,0 +1,4 @@
+[test]
+build_only = true
+tags = apps
+filter = ZEPHYR_GCC_VARIANT != "issm"