[PATCH] Add pthread example

Marçal Comajoan Cara mcomajoancara at gmail.com
Sun Dec 9 15:44:21 UTC 2018


Closes #2087.

This work was part of GCI 2018.
---
 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  | 14 +++++
 posix_api/livermore/wscript          | 10 ++++
 posix_api/wscript                    |  1 +
 8 files changed, 142 insertions(+)
 create mode 100644 posix_api/livermore/Makefile
 create mode 100644 posix_api/livermore/pthread/Makefile
 create mode 100644 posix_api/livermore/pthread/README
 create mode 100644 posix_api/livermore/pthread/init.c
 create mode 100644 posix_api/livermore/pthread/wscript
 create mode 100644 posix_api/livermore/wscript

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..b77e733
--- /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..8e0f266
--- /dev/null
+++ b/posix_api/livermore/pthread/wscript
@@ -0,0 +1,14 @@
+# 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')
-- 
2.19.2



More information about the devel mailing list