From a1c828c484230f3fc3e85b0084a2ef07c952b76a Mon Sep 17 00:00:00 2001
From: patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>
Date: Fri, 26 Jun 2009 17:46:51 +0000
Subject: [PATCH] integrating NXFLAT

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1962 42af7a65-404d-4744-a932-0658087f49c3
---
 examples/nxflat/tests/mutex/mutex.c     | 46 ++++++++++++++++++-------
 examples/nxflat/tests/pthread/pthread.c |  2 +-
 examples/nxflat/tests/task/task.c       | 28 ++++++++++-----
 include/nxflat.h                        | 10 +++---
 sched/task_start.c                      |  2 +-
 5 files changed, 60 insertions(+), 28 deletions(-)

diff --git a/examples/nxflat/tests/mutex/mutex.c b/examples/nxflat/tests/mutex/mutex.c
index 8e528da00b..f5a84d14b5 100644
--- a/examples/nxflat/tests/mutex/mutex.c
+++ b/examples/nxflat/tests/mutex/mutex.c
@@ -39,6 +39,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <pthread.h>
 
 /****************************************************************************
@@ -49,23 +50,29 @@ static pthread_mutex_t mut;
 static volatile int my_mutex = 0;
 static unsigned long nloops[2] = {0, 0};
 static unsigned long nerrors[2] = {0, 0};
+static volatile boolean bendoftest;
 
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
 
 /* NOTE: it is necessary for functions that are referred to by function pointers
- *  pointer to be declared with global scope (at least for ARM).  Otherwise,
+ * pointer to be declared with global scope (at least for ARM).  Otherwise,
  * a relocation type that is not supported by NXFLAT is generated by GCC.
  */
 
 void thread_func(void *parameter)
 {
-  int my_id = (int)parameter;
+  int my_id  = (int)parameter;
   int my_ndx = my_id - 1;
-  volatile int i;
-    
-  for (;;)
+  int i;
+
+  /* Loop 20 times.  There is a 100 MS delay in the loop so this should
+   * take about 2 seconds.  The main thread will stop this thread after
+   * 2 seconds by setting bendoftest in any event.
+   */
+
+  for (i = 0; i < 20 && !bendoftest; i++);
     {
       if ((pthread_mutex_lock(&mut)) != 0)
 	{
@@ -80,8 +87,8 @@ void thread_func(void *parameter)
 	  nerrors[my_ndx]++;
 	}
 
-      my_mutex = 1;	
-      for (i = 0; i < 1000; i++);
+      my_mutex = 1;
+      usleep(100000);
       my_mutex = 0;
 	
       if ((pthread_mutex_unlock(&mut)) != 0)
@@ -99,9 +106,8 @@ void thread_func(void *parameter)
 
 int main(int argc, char **argv)
 {
-  pthread_t thread1, thread2;
-
-  printf("Starting threads\n");
+  pthread_t thread1;
+  pthread_t thread2;
 
   /* Initialize the mutex */
 
@@ -109,20 +115,34 @@ int main(int argc, char **argv)
 
   /* Start two thread instances */
 
-  if ((pthread_create(&thread1, NULL, (void*)&thread_func, (void*)1)) != 0)
+  printf("Starting thread 1\n");
+  bendoftest = FALSE;
+  if ((pthread_create(&thread1, NULL, (void*)thread_func, (void*)1)) != 0)
     {
       fprintf(stderr, "Error in thread#1 creation\n");
     }
 
-  if ((pthread_create(&thread2, NULL, (void*)&thread_func, (void*)2)) != 0)
+  printf("Starting thread 2\n");
+  if ((pthread_create(&thread2, NULL, (void*)thread_func, (void*)2)) != 0)
     {
       fprintf(stderr, "Error in thread#2 creation\n");
     }
 
-  printf("Press control-C to terminate the example\n");
+  /* Wait a bit for the threads to do their thing. */
+
+  sleep(2);
 
+  /* Then ask them politely to stop running */
+
+  printf("Stopping threads\n");
+  bendoftest = TRUE;
   pthread_join(thread1, NULL);
   pthread_join(thread2, NULL);
+
+  printf("\tThread1\tThread2\n");
+  printf("Loops\t%ld\t%ld\n", nloops[0], nloops[1]);
+  printf("Errors\t%ld\t%ld\n", nerrors[0], nerrors[1]);
+
   return 0;
 }
 
diff --git a/examples/nxflat/tests/pthread/pthread.c b/examples/nxflat/tests/pthread/pthread.c
index 6de82a46dc..019ec453e1 100644
--- a/examples/nxflat/tests/pthread/pthread.c
+++ b/examples/nxflat/tests/pthread/pthread.c
@@ -76,7 +76,7 @@ enum exit_values_e
  ****************************************************************************/
 
 /* NOTE: it is necessary for functions that are referred to by function pointers
- *  pointer to be declared with global scope (at least for ARM).  Otherwise,
+ * pointer to be declared with global scope (at least for ARM).  Otherwise,
  * a relocation type that is not supported by NXFLAT is generated by GCC.
  */
 
diff --git a/examples/nxflat/tests/task/task.c b/examples/nxflat/tests/task/task.c
index 6083d68bd8..63e8188ed5 100644
--- a/examples/nxflat/tests/task/task.c
+++ b/examples/nxflat/tests/task/task.c
@@ -54,12 +54,16 @@ static char child_name[] = "child";
 static char child_arg[] = "Hello from your parent!";
 static sem_t g_sem;
 
+#if CONFIG_TASK_NAME_SIZE == 0
+static char no_name[] = "<noname>";
+#endif
+
 /****************************************************************************
  * Privite Functions
  ****************************************************************************/
 
 /* NOTE: it is necessary for functions that are referred to by function pointers
- *  pointer to be declared with global scope (at least for ARM).  Otherwise,
+ * pointer to be declared with global scope (at least for ARM).  Otherwise,
  * a relocation type that is not supported by NXFLAT is generated by GCC.
  */
 
@@ -74,15 +78,23 @@ static sem_t g_sem;
       printf("Child: Exit-ting with status=2\n");
       exit(2);
     }
-
   printf("Child: argv[0]=\"%s\"\n", argv[0]);
 
-  if (strcmp(argv[0], child_name) != 0)
+#if CONFIG_TASK_NAME_SIZE == 0
+  if (strcmp(argv[0], no_name) != 0)
+    {
+      printf("Child: expected argv[0] to be \"%s\"\n", no_name);
+      printf("Child: Exit-ting with status=3\n");
+      exit(3);
+    }
+#else
+  if (strncmp(argv[0], child_name, CONFIG_TASK_NAME_SIZE) != 0)
     {
       printf("Child: expected argv[0] to be \"%s\"\n", child_name);
       printf("Child: Exit-ting with status=3\n");
       exit(3);
     }
+#endif
 
   printf("Child: argv[1]=\"%s\"\n", argv[1]);
 
@@ -106,7 +118,7 @@ int main(int argc, char **argv)
 {
   pid_t parent_pid = getpid();
   char *child_argv[2];
-  int ret;
+  pid_t child_pid;
 
   printf("Parent: Started, pid=%d\n", parent_pid);
 
@@ -116,15 +128,15 @@ int main(int argc, char **argv)
 
   child_argv[0] = child_arg;
   child_argv[1] = 0;
-  ret = task_create(child_name, 50, 512, child_task, (const char**)child_argv);
-  if (ret != 0)
+  child_pid = task_create(child_name, 50, 512, child_task, (const char**)child_argv);
+  if (child_pid < 0)
     {
       printf("Parent: task_create failed: %d\n", errno);
     }
 
-  printf("Parent: Waiting for child\n");
+  printf("Parent: Waiting for child (pid=%d)\n", child_pid);
   sem_wait(&g_sem);
-  printf("Parent: Exiting\n");
+  printf("Parent: Exit-ing\n");
   sem_destroy(&g_sem);
   return 0;
 }
diff --git a/include/nxflat.h b/include/nxflat.h
index 14f0718063..a582057210 100644
--- a/include/nxflat.h
+++ b/include/nxflat.h
@@ -47,8 +47,8 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-#define NXFLAT_MAX_STRING_SIZE 64    /* Largest size of string (w/zterminator) */
-#define NXFLAT_MAGIC          "NxFT" /* NXFLAT magic number */
+#define NXFLAT_MAX_STRING_SIZE 64     /* Largest size of string (w/zterminator) */
+#define NXFLAT_MAGIC          "NxFT"  /* NXFLAT magic number */
 
 /****************************************************************************
  * Public Types
@@ -69,7 +69,7 @@ struct nxflat_hdr_s
    * this magic number.
    */
 
-  char  h_magic[4];
+  char h_magic[4];
 
   /* The following fields provide the memory map for the nxflat binary.
    *
@@ -116,7 +116,7 @@ struct nxflat_hdr_s
   /* Imported symbol table (NOTE no symbols are exported):
    *
    * h_importsymbols - Offset to the beginning of an array of imported
-   *                   symbol structures (struct nxflat_import).  The
+   *                   symbol structures (struct nxflat_import_s).  The
    *                   h_importsymbols offset is relative to the
    *                   beginning of the file.  Each entry of the
    *                   array contains an uint32 offset (again from
@@ -149,7 +149,7 @@ struct nxflat_reloc_s
 
 /* Pack the type and the offset into one 32-bit value */
 
-#define NXFLAT_RELOC(t,o)       (((u_int32_t)((t) & 3) << 30) | ((o) & 0x1fffffff))
+#define NXFLAT_RELOC(t,o)       (((u_int32_t)((t) & 3) << 30) | ((o) & 0x3fffffff))
 
 /* The top three bits of the relocation info is the relocation type (see the
  * NXFLAT_RELOC_TYPE_* definitions below.  This is an unsigned value.
diff --git a/sched/task_start.c b/sched/task_start.c
index b06fbb981e..72e96516e2 100644
--- a/sched/task_start.c
+++ b/sched/task_start.c
@@ -1,7 +1,7 @@
 /****************************************************************************
  * task_start.c
  *
- *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * Redistribution and use in source and binary forms, with or without
-- 
GitLab