Merge branch "preempt" in "trunk".
[bertos.git] / bertos / kern / msg_test.c
index d7e8c7d846c76ed2e8f8dbd826145e5766e97e21..bcb20ea288ef89e94da0569664108d4421915346 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$: 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 <cfg/cfg_timer.h>
+#include "cfg/cfg_timer.h"
 #include <cfg/debug.h>
 #include <cfg/test.h>
 #include <cfg/compiler.h>
 
 #include <kern/msg.h>
 #include <kern/proc.h>
-#include <kern/irq.h>
+#include <kern/signal.h>
+
+#include <mware/event.h>
 
 #include <drv/timer.h>
 
+/*
+ * In the nightly build test, signals are disables, so this
+ * code won't compile.
+ * Since this code is used when we run "make check" it will be
+ * compiled and therefor tested there.
+ */
+#if CONFIG_KERN_SIGNALS
+
+
 // Global settings for the test.
 #define MAX_GLOBAL_COUNT            11040
-#define TEST_TIME_OUT_MS               10
+#define TEST_TIME_OUT_MS             5000
 #define DELAY                           5
 
 // Settings for the test message.
 /*
  * These macros generate the code needed to create the test process functions.
  */
-#define RECV_PROC(num, sig) static void receiver_proc##num(void) \
-    { \
-        TestMsg *rec_msg; \
-               for (;;) \
-        { \
-            sig_wait(sig); \
-            kprintf("Proc[%d]..get message\n", num); \
-            rec_msg = containerof(msg_get(&test_port##num), TestMsg, msg); \
-                       timer_delay(rec_msg->delay); \
-            rec_msg->result += rec_msg->val;  \
-            kprintf("Proc[%d]..process message val[%d],delay[%d],res[%d]\n", num, rec_msg->val, rec_msg->delay, rec_msg->result); \
-            msg_reply(&rec_msg->msg); \
-            kprintf("Proc[%d] reply\n", num); \
-        } \
-    }
+#define RECV_PROC(num, sig) \
+static NORETURN void receiver_proc##num(void) \
+{ \
+       TestMsg *rec_msg; \
+       for(;;) \
+       { \
+               sig_wait(sig); \
+               kprintf("Proc[%d]..get message\n", num); \
+               rec_msg = containerof(msg_get(&test_port##num), TestMsg, msg); \
+               timer_delay(rec_msg->delay); \
+               rec_msg->result += rec_msg->val; \
+               kprintf("Proc[%d]..process message val[%d],delay[%d],res[%d]\n", num, rec_msg->val, rec_msg->delay, rec_msg->result); \
+               msg_reply(&rec_msg->msg); \
+               process_num++; \
+               kprintf("Proc[%d] reply\n", num); \
+       } \
+}
 
 #define SEND_MSG(num) \
        do { \
                msg_put(&test_port##num, &msg##num.msg); \
        } while(0)
 
-#define RECV_STACK(num) static cpu_stack_t receiver_stack##num[CONFIG_KERN_MINSTACKSIZE / sizeof(cpu_stack_t)]
+#define RECV_STACK(num) PROC_DEFINE_STACK(receiver_stack##num, KERN_MINSTACKSIZE * 2)
 #define RECV_INIT_PROC(num) proc_new(receiver_proc##num, NULL, sizeof(receiver_stack##num), receiver_stack##num)
-#define RECV_INIT_MSG(num, proc,sig) msg_initPort(&test_port##num, event_createSignal(proc, sig))
+#define RECV_INIT_MSG(num, proc, sig) msg_initPort(&test_port##num, event_createSignal(proc, sig))
 
 // A test message with the parameters and a result.
 typedef struct
@@ -115,6 +135,7 @@ typedef struct
 
 // Global count to check if the test is going ok.
 static int count = 0;
+static int process_num;
 
 // Our message port.
 static MsgPort test_port0;
@@ -172,7 +193,6 @@ int msg_testRun(void)
        TestMsg msg4;
        TestMsg msg5;
        TestMsg *reply;
-       ticks_t start_time = timer_clock();
 
        // Allocate and start the test process
     struct Process *recv0 = RECV_INIT_PROC(0);
@@ -205,6 +225,7 @@ int msg_testRun(void)
        // Send and wait the message
        for (int i = 0; i < 23; ++i)
     {
+               process_num = 0;
                SEND_MSG(0);
                SEND_MSG(1);
                SEND_MSG(2);
@@ -213,14 +234,24 @@ int msg_testRun(void)
                SEND_MSG(5);
                while(1)
                {
-                       if(sig_waitTimeout(SIG_SINGLE, TEST_TIME_OUT_MS) && SIG_SINGLE)
+                       sigmask_t sigs = sig_waitTimeout(SIG_SINGLE, ms_to_ticks(TEST_TIME_OUT_MS));
+                       if (sigs & SIG_SINGLE)
                        {
                                // Wait for a reply...
-                               reply = containerof(msg_get(&test_portMain), TestMsg, msg);
-                               if(reply == NULL)
-                                       break;
-                               count += reply->result;
-                               kprintf("Main recv[%d] count[%d]\n", reply->result, count);
+                               while ((reply = (TestMsg *)msg_get(&test_portMain)))
+                               {
+                                       count += reply->result;
+                                       kprintf("Main recv[%d] count[%d]\n", reply->result, count);
+                               }
+                       }
+                       
+                       if (process_num == 6)
+                               break;
+
+                       if (sigs & SIG_TIMEOUT)
+                       {
+                               kputs("Main: sig timeout\n");
+                               goto error;
                        }
                }
     }
@@ -230,7 +261,8 @@ int msg_testRun(void)
                kprintf("Message test finished..ok!\n");
                return 0;
        }
-
+       
+error:
        kprintf("Message test finished..fail!\n");
        return -1;
 }
@@ -239,12 +271,6 @@ int msg_testSetup(void)
 {
        kdbg_init();
 
-       #if CONFIG_KERN_PREEMPT
-               kprintf("Init Interrupt (preempt mode)..");
-               irq_init();
-               kprintf("Done.\n");
-       #endif
-
        kprintf("Init Timer..");
        timer_init();
        kprintf("Done.\n");
@@ -262,3 +288,5 @@ int msg_testTearDown(void)
 }
 
 TEST_MAIN(msg);
+
+#endif /* CONFIG_KERN_SIGNALS */