[examples-v2 commit] Add some Livermore pthread examples (GCI 2013/2018)

Joel Sherrill joel at rtems.org
Sun Dec 9 22:46:32 UTC 2018


Module:    examples-v2
Branch:    master
Commit:    ee11ed7f8d16902deb8dce9c351569c082d5cdfe
Changeset: http://git.rtems.org/examples-v2/commit/?id=ee11ed7f8d16902deb8dce9c351569c082d5cdfe

Author:    Marçal Comajoan Cara <mcomajoancara at gmail.com>
Date:      Sun Dec  9 15:16:30 2018 +0100

Add some Livermore pthread examples (GCI 2013/2018)

The initial conversion was done by Christopher Kerl as part of GCI 2013.
Finally merged as part of GCI 2018.

This is part of the Livermore pthread examples from
https://computing.llnl.gov/tutorials/pthreads/

Closes #2087.

---

 posix_api/Makefile                   |  1 +
 posix_api/livermore/Makefile         |  8 ++++
 posix_api/livermore/pthread/Makefile | 23 +++++++++++
 posix_api/livermore/pthread/README   |  5 +++
 posix_api/livermore/pthread/init.c   | 80 ++++++++++++++++++++++++++++++++++++
 posix_api/livermore/pthread/wscript  | 13 ++++++
 posix_api/livermore/wscript          | 10 +++++
 posix_api/wscript                    |  1 +
 8 files changed, 141 insertions(+)

diff --git a/posix_api/Makefile b/posix_api/Makefile
index 8af26dc..601246c 100644
--- a/posix_api/Makefile
+++ b/posix_api/Makefile
@@ -14,4 +14,5 @@ ifeq ($(RTEMS_HAS_POSIX_API),yes)
   SUBDIRS += psx_pthread_report
   SUBDIRS += psx_rwlock_report
   SUBDIRS += psx_sched_report
+  SUBDIRS += livermore
 endif
diff --git a/posix_api/livermore/Makefile b/posix_api/livermore/Makefile
new file mode 100644
index 0000000..d8695a1
--- /dev/null
+++ b/posix_api/livermore/Makefile
@@ -0,0 +1,8 @@
+include $(RTEMS_MAKEFILE_PATH)/Makefile.inc
+include $(RTEMS_CUSTOM)
+include $(RTEMS_SHARE)/make/directory.cfg
+
+# If the POSIX API isn't enabled, we can't build these
+ifeq ($(RTEMS_HAS_POSIX_API),yes)
+  SUBDIRS  = pthread
+endif
diff --git a/posix_api/livermore/pthread/Makefile b/posix_api/livermore/pthread/Makefile
new file mode 100644
index 0000000..fdd13dc
--- /dev/null
+++ b/posix_api/livermore/pthread/Makefile
@@ -0,0 +1,23 @@
+#
+#  RTEMS_MAKEFILE_PATH is typically set in an environment variable
+#
+
+PGM=${ARCH}/pthread.exe
+
+# optional managers required
+MANAGERS=all
+
+# C source names
+CSRCS = init.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)
\ No newline at end of file
diff --git a/posix_api/livermore/pthread/README b/posix_api/livermore/pthread/README
new file mode 100644
index 0000000..17cefd4
--- /dev/null
+++ b/posix_api/livermore/pthread/README
@@ -0,0 +1,5 @@
+This is a simple example which illustrates how to use the
+pthreads adapted from the Lawrence Livermore POSIX Pthreads
+tutorial: ​https://computing.llnl.gov/tutorials/pthreads/.
+
+Thanks to Christopher Kerl for providing the code.
diff --git a/posix_api/livermore/pthread/init.c b/posix_api/livermore/pthread/init.c
new file mode 100644
index 0000000..bbc296b
--- /dev/null
+++ b/posix_api/livermore/pthread/init.c
@@ -0,0 +1,80 @@
+/*
+ *  COPYRIGHT (c) 1989-2012.
+ *  On-Line Applications Research Corporation (OAR).
+ *
+ *  The license and distribution terms for this file may be
+ *  found in the file LICENSE in this distribution or at
+ *  http://www.rtems.com/license/LICENSE.
+ */
+
+//Adapted from Lawrence Livermore Pthread Tutorial #1
+//https://computing.llnl.gov/tutorials/pthreads
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <bsp.h> /* for device driver prototypes */
+#include <stdio.h>
+#include <stdlib.h>
+#include <pthread.h>
+
+#define NUM_THREADS    5
+
+/* forward declarations to avoid warnings */
+void *POSIX_Init(void *argument);
+void *Print_Hello(void *threadid);
+
+void *Print_Hello(void *threadid)
+{
+   long tid;
+   tid = (long)threadid;
+   printf("Hello World! It's me, thread #%ld!\n", tid);
+
+   //You must include the return 'NULL;' in your threads to make the compiler happy
+   pthread_exit(NULL);
+   return NULL;
+}
+
+
+void *POSIX_Init(void *argument)
+{
+   pthread_t threads[NUM_THREADS];
+   int rc;
+   long t;
+
+   //Creates threads
+   for(t=0; t<NUM_THREADS; t++){
+      printf("In main: creating thread %ld\n", t);
+      rc = pthread_create(&threads[t], NULL, Print_Hello, (void *)t);
+      if (rc){
+         printf("ERROR; return code from pthread_create() is %d\n", rc);
+         exit(-1);
+      }
+   }
+
+
+   //Joins the child threads up to the main one.
+   for(t=0; t<NUM_THREADS; t++){
+      pthread_join(threads[t],NULL);
+   }
+   /* Last thing that main() should do */
+   exit(NULL);
+}
+
+/* NOTICE: the clock driver is explicitly disabled */
+#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
+#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
+
+#define CONFIGURE_MAXIMUM_TASKS            1
+#define CONFIGURE_USE_DEVFS_AS_BASE_FILESYSTEM
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//NOTICE: These configuration variable MUST be initialized before using Pthreads. The will not work if you do not.
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+#define CONFIGURE_MAXIMUM_POSIX_THREADS 6                                                                    /////
+#define CONFIGURE_POSIX_INIT_THREAD_TABLE                                                                    /////
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+#define CONFIGURE_INIT
+#include <rtems/confdefs.h>
diff --git a/posix_api/livermore/pthread/wscript b/posix_api/livermore/pthread/wscript
new file mode 100644
index 0000000..6011a02
--- /dev/null
+++ b/posix_api/livermore/pthread/wscript
@@ -0,0 +1,13 @@
+# Copyright 2018 Marçal Comajoan Cara
+#
+# This file's license is 2-clause BSD as in this distribution's LICENSE.2 file.
+#
+
+import rtems_waf.rtems as rtems
+
+def build(bld):
+    rtems.build(bld)
+
+    bld(features = 'c cprogram',
+        target = 'pthread.exe',
+        source = ['init.c'])
diff --git a/posix_api/livermore/wscript b/posix_api/livermore/wscript
new file mode 100644
index 0000000..da99c30
--- /dev/null
+++ b/posix_api/livermore/wscript
@@ -0,0 +1,10 @@
+# Copyright 2018 Marçal Comajoan Cara
+#
+# This file's license is 2-clause BSD as in this distribution's LICENSE.2 file.
+#
+
+import rtems_waf.rtems as rtems
+
+def build(bld):
+    if rtems.check_posix(bld):
+        bld.recurse('pthread')
diff --git a/posix_api/wscript b/posix_api/wscript
index 66a351f..912063b 100644
--- a/posix_api/wscript
+++ b/posix_api/wscript
@@ -17,3 +17,4 @@ def build(bld):
         bld.recurse('psx_pthread_report')
         bld.recurse('psx_rwlock_report')
         bld.recurse('psx_sched_report')
+        bld.recurse('livermore')




More information about the vc mailing list