summaryrefslogtreecommitdiffstats
path: root/c11/c11_cndvar01
diff options
context:
space:
mode:
authorJoel Sherrill <joel@rtems.org>2017-10-05 08:59:43 -0500
committerJoel Sherrill <joel@rtems.org>2017-10-12 14:17:52 -0500
commit45bf0f4be0f73506b8abe6d092ac6f5b84c3d9fc (patch)
treea5b3ac778ba6fcd0bb098187b5cc9696484e9ab6 /c11/c11_cndvar01
parentpsx_sched_report: Add RTEMS configuration to address build issue. (diff)
downloadrtems-examples-45bf0f4be0f73506b8abe6d092ac6f5b84c3d9fc.tar.bz2
Add C11 Threading Examples
Diffstat (limited to 'c11/c11_cndvar01')
-rw-r--r--c11/c11_cndvar01/Makefile20
-rw-r--r--c11/c11_cndvar01/rtems_config.c53
-rw-r--r--c11/c11_cndvar01/test.c141
-rw-r--r--c11/c11_cndvar01/wscript14
4 files changed, 228 insertions, 0 deletions
diff --git a/c11/c11_cndvar01/Makefile b/c11/c11_cndvar01/Makefile
new file mode 100644
index 0000000..f28b952
--- /dev/null
+++ b/c11/c11_cndvar01/Makefile
@@ -0,0 +1,20 @@
+#
+# RTEMS_MAKEFILE_PATH is typically set in an environment variable
+#
+
+PGM=${ARCH}/c11_cndvar01.exe
+
+# C source names
+CSRCS = rtems_config.c test.c
+COBJS = $(CSRCS:%.c=${ARCH}/%.o)
+
+include $(RTEMS_MAKEFILE_PATH)/Makefile.inc
+include $(RTEMS_CUSTOM)
+include $(PROJECT_ROOT)/make/leaf.cfg
+
+OBJS= $(COBJS) $(CXXOBJS) $(ASOBJS)
+
+all: ${ARCH} $(PGM)
+
+$(PGM): $(OBJS)
+ $(make-exe)
diff --git a/c11/c11_cndvar01/rtems_config.c b/c11/c11_cndvar01/rtems_config.c
new file mode 100644
index 0000000..a596695
--- /dev/null
+++ b/c11/c11_cndvar01/rtems_config.c
@@ -0,0 +1,53 @@
+/**
+ * @brief RTEMS Configuration for C11 Mutex Example
+ */
+
+/*
+ * This file's license is 2-clause BSD as in this distribution's
+ * LICENSE.2 file.
+ */
+
+#include <stdlib.h>
+
+int main(int argc, char **argv);
+
+static char *argv_list[] = {
+ "c11-cndvar01",
+ ""
+};
+
+static void *POSIX_Init(void *arg)
+{
+ int rc;
+
+ (void) arg; /* deliberately ignored */
+
+ /*
+ * Initialize optional services
+ */
+
+ /*
+ * Could get arguments from command line or have a static set.
+ */
+ rc = main(1, argv_list);
+
+ exit(rc);
+ return NULL;
+}
+
+#include <bsp.h> /* for device driver prototypes */
+
+/* NOTICE: the clock driver is explicitly disabled */
+#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
+#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
+
+#define CONFIGURE_POSIX_INIT_THREAD_TABLE
+
+#define CONFIGURE_UNLIMITED_OBJECTS
+#define CONFIGURE_UNIFIED_WORK_AREAS
+#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 32
+
+#define CONFIGURE_MINIMUM_TASK_STACK_SIZE (64 * 1024)
+
+#define CONFIGURE_INIT
+#include <rtems/confdefs.h>
diff --git a/c11/c11_cndvar01/test.c b/c11/c11_cndvar01/test.c
new file mode 100644
index 0000000..451c7c2
--- /dev/null
+++ b/c11/c11_cndvar01/test.c
@@ -0,0 +1,141 @@
+/**
+ * @brief C11 Threads Example
+ */
+
+/*
+ * This file's license is 2-clause BSD as in this distribution's
+ * LICENSE.2 file.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <threads.h>
+#include <assert.h>
+
+static thrd_t thread1;
+static mtx_t buffer_mutex;
+static cnd_t buffer_cnd;
+static int buffer_value;
+
+static int buffer_init(void)
+{
+ int rc;
+
+ puts("buffer - mtx_init()");
+ rc = mtx_init(&buffer_mutex, mtx_timed);
+ assert(rc == thrd_success);
+
+ puts("buffer - cnd_init()");
+ rc = cnd_init(&buffer_cnd);
+ assert(rc == thrd_success);
+
+ buffer_value = -1;
+}
+
+void buffer_destroy(void)
+{
+ puts("buffer - mtx_destroy()" );
+ mtx_destroy(&buffer_mutex);
+
+ puts("buffer - cnd_destroy()" );
+ cnd_destroy(&buffer_cnd);
+}
+
+static int buffer_get(void)
+{
+ int rc;
+ int value;
+
+ // puts("buffer - mtx_lock()");
+ rc = mtx_lock(&buffer_mutex);
+ assert(rc == thrd_success);
+
+ while (buffer_value == -1) {
+ // puts("buffer - cnd_wait()");
+ rc = cnd_wait(&buffer_cnd, &buffer_mutex);
+ assert(rc = thrd_success);
+ }
+
+ value = buffer_value;
+ buffer_value = -1;
+ // puts("buffer - return value");
+
+
+ // puts("buffer - mtx_unlock()");
+ rc = mtx_unlock(&buffer_mutex);
+ assert(rc == thrd_success);
+
+ return value;
+}
+
+static void buffer_put(int value)
+{
+ int rc;
+ int old_value;
+
+ // puts("buffer - mtx_lock()");
+ rc = mtx_lock(&buffer_mutex);
+ assert(rc == thrd_success);
+
+ old_value = buffer_value;
+ buffer_value = value;
+
+ if (old_value == -1) {
+ // puts("buffer - cnd_signal()");
+ rc = cnd_signal(&buffer_cnd);
+ assert(rc = thrd_success);
+ }
+
+ // puts("buffer - mtx_unlock()");
+ rc = mtx_unlock(&buffer_mutex);
+ assert(rc == thrd_success);
+
+}
+
+int Thread1_Body(void *arg)
+{
+ int rc;
+ int value;
+
+ (void) arg;
+
+ puts("Thread1 - Loop for values");
+ while (1) {
+ value = buffer_get();
+ printf("Thread1 value = %d\n", value);
+ }
+
+ return 0;
+}
+
+int main(int argc, char **argv)
+{
+ int rc;
+ int value;
+ struct timespec delay = {1, 0};
+
+ puts("*** START OF C11 CONDITION VARIABLE EXAMPLE ***");
+
+ puts("main - Create Thread1");
+ rc = thrd_create(&thread1, Thread1_Body, &thread1);
+ assert(rc == thrd_success);
+
+ buffer_init();
+
+ puts("main - yield to Thread1" );
+ thrd_yield();
+
+ for (value=1 ; value <= 3 ; value++) {
+ printf("main - put value = %d\n", value * 10);
+ buffer_put(value * 10);
+
+ puts("main - sleep to let Thread1 get the value");
+ rc = thrd_sleep(&delay, NULL);
+ assert(rc == 0);
+ }
+
+ buffer_destroy();
+
+ puts("*** END OF C11 CONDITION VARIABLE EXAMPLE ***");
+ return 0;
+}
diff --git a/c11/c11_cndvar01/wscript b/c11/c11_cndvar01/wscript
new file mode 100644
index 0000000..90e78a1
--- /dev/null
+++ b/c11/c11_cndvar01/wscript
@@ -0,0 +1,14 @@
+# Copyright 2017 Joel Sherrill (joel.sherrill@oarcorp.com)
+#
+# This file's license is 2-clause BSD as in this distribution's LICENSE.2 file.
+#
+
+# Waf build script for an RTEMS Hello
+import rtems_waf.rtems as rtems
+
+def build(bld):
+ rtems.build(bld)
+
+ bld(features = 'c cprogram',
+ target = 'c11_cndvar01.exe',
+ source = ['rtems_config.c', 'test.c'])