Better detection of concurrent ADC access.
[bertos.git] / bertos / kern / proc_test.c
index 0553881b78a0cbaee898ed67a544309d34da3681..ad30f711153ca120e232b18336e5afd300555d9f 100644 (file)
  *
  * \version $Id$
  * \author Daniele Basile <asterix@develer.com>
+ *
+ * $test$: cp bertos/cfg/cfg_proc.h $cfgdir/
+ * $test$: echo  "#undef CONFIG_KERN" >> $cfgdir/cfg_proc.h
+ * $test$: echo "#define CONFIG_KERN 1" >> $cfgdir/cfg_proc.h
+ * $test$: echo  "#undef CONFIG_KERN_PRI" >> $cfgdir/cfg_proc.h
+ * $test$: echo "#define CONFIG_KERN_PRI 1" >> $cfgdir/cfg_proc.h
+ * $test$: cp bertos/cfg/cfg_monitor.h $cfgdir/
+ * $test$: echo  "#undef CONFIG_KERN_MONITOR" >> $cfgdir/cfg_monitor.h
+ * $test$: echo "#define CONFIG_KERN_MONITOR 1" >> $cfgdir/cfg_monitor.h
+ * $test$: cp bertos/cfg/cfg_signal.h $cfgdir/
+ * $test$: echo  "#undef CONFIG_KERN_SIGNALS" >> $cfgdir/cfg_signal.h
+ * $test$: echo "#define CONFIG_KERN_SIGNALS 1" >> $cfgdir/cfg_signal.h
  */
 
 #include <kern/proc.h>
@@ -45,8 +57,6 @@
 
 
 // Global settings for the test.
-#define MAX_GLOBAL_COUNT             1024
-#define TEST_TIME_OUT_MS             6000
 #define DELAY                           5
 
 // Settings for the test process.
@@ -90,17 +100,15 @@ unsigned int t8_count = 0;
  */
 #define PROC_TEST(num) static void proc_test##num(void) \
 { \
-       unsigned int local_count = 0; \
-       \
        for (int i = 0; i < INC_PROC_T##num; ++i) \
        { \
                t##num##_count++; \
-               kprintf("> Process[%d]: count[%d]\n", num, t##num##_count); \
+               kputs("> Process[" #num "]\n"); \
                timer_delay(DELAY_PROC_T##num); \
        } \
-} \
+}
 
-#define PROC_TEST_STACK(num)  static cpu_stack_t proc_test##num##_stack[CONFIG_KERN_MINSTACKSIZE / sizeof(cpu_stack_t)];
+#define PROC_TEST_STACK(num)  PROC_DEFINE_STACK(proc_test##num##_stack, KERN_MINSTACKSIZE);
 #define PROC_TEST_INIT(num)   proc_new(proc_test##num, NULL, sizeof(proc_test##num##_stack), proc_test##num##_stack);
 
 // Define process
@@ -123,12 +131,33 @@ PROC_TEST_STACK(6)
 PROC_TEST_STACK(7)
 PROC_TEST_STACK(8)
 
+// Define params to test priority
+#define PROC_PRI_TEST(num) static void proc_pri_test##num(void) \
+{ \
+       struct Process *main_proc = (struct Process *) proc_currentUserData(); \
+       kputs("> Process: " #num "\n"); \
+       sig_signal(main_proc, SIG_USER##num); \
+}
+
+// Default priority is 0
+#define PROC_PRI_TEST_INIT(num, proc)  \
+do { \
+       struct Process *p = proc_new(proc_pri_test##num, (proc), sizeof(proc_test##num##_stack), proc_test##num##_stack); \
+       proc_setPri(p, num + 1); \
+} while (0)
+
+PROC_TEST_STACK(0)
+PROC_PRI_TEST(0)
+PROC_PRI_TEST(1)
+PROC_PRI_TEST(2)
+
 
 /**
  * Process scheduling test
  */
 int proc_testRun(void)
 {
+       int ret_value = 0;
        kprintf("Run Process test..\n");
 
        //Init the process tests
@@ -159,13 +188,59 @@ int proc_testRun(void)
                t8_count == INC_PROC_T8)
        {
                kputs("> Main: process test finished..ok!\n");
-               return 0;
+               ret_value = 0;
+       }
+       else
+       {
+               kputs("> Main: process test..fail!\n");
+               ret_value = -1;
        }
 
-       kputs("> Main: process test..fail!\n");
+#if CONFIG_KERN_SIGNALS & CONFIG_KERN_PRI
+       // test process priority
+       // main process must have the higher priority to check signals received
+       proc_setPri(proc_current(), 10);
+
+       struct Process *curr = proc_current();
+       // the order in which the processes are created is important!
+       PROC_PRI_TEST_INIT(0, curr);
+       PROC_PRI_TEST_INIT(1, curr);
+       PROC_PRI_TEST_INIT(2, curr);
+
+       // signals must be: USER2, 1, 0 in order
+       sigmask_t signals = sig_wait(SIG_USER0 | SIG_USER1 | SIG_USER2);
+       if (!(signals & SIG_USER2))
+               goto priority_fail;
+
+       signals = sig_wait(SIG_USER0 | SIG_USER1 | SIG_USER2);
+       if (!(signals & SIG_USER1))
+               goto priority_fail;
+
+       signals = sig_wait(SIG_USER0 | SIG_USER1 | SIG_USER2);
+       if (!(signals & SIG_USER0))
+               goto priority_fail;
+
+       // All processes must have quit by now, but just in case...
+       signals = sig_waitTimeout(SIG_USER0 | SIG_USER1 | SIG_USER2, 200);
+       if (signals & (SIG_USER0 | SIG_USER1 | SIG_USER2))
+               goto priority_fail;
+
+       if (signals & SIG_TIMEOUT)
+       {
+               kputs("Priority test successfull.\n");
+       }
+
+       return ret_value;
+
+priority_fail:
+       kputs("Priority test failed.\n");
        return -1;
-}
 
+#endif
+
+       return ret_value;
+
+}
 
 int proc_testSetup(void)
 {