]> rtime.felk.cvut.cz Git - can-benchmark.git/blobdiff - rtems/gw/cangw/gw.c
Enables conditional compilation for gateway itself.
[can-benchmark.git] / rtems / gw / cangw / gw.c
index 87dc7560ea184d96af590cc0c69f19a922d4f5cb..2a08a0c0ae63c4e164389c428d27421630e537a4 100644 (file)
@@ -1,6 +1,6 @@
 #include <system_def.h>\r
-#include "system.h"\r
 #include "app_def.h"\r
+#include "system.h"\r
 #include <stdio.h>\r
 #include <stdlib.h>\r
 #include <rtems/untar.h>\r
@@ -20,6 +20,7 @@
 #define CAN_GW_A_TO_B_MODE 1\r
 #define CAN_GW_B_TO_A_MODE 2\r
 #define CAN_GW_BAUDRATE 1000000\r
+#define CAN_GW_TASK_HIGH_PRIO 250 /* POSIX API actually has priorities the "right way around" that is, higher is indeed more important */\r
 \r
 /* counters for informational purposes */\r
 unsigned long int total_1 = 0, total_2 = 0;\r
@@ -51,10 +52,12 @@ static void fd_closer(void* arg){
 * This function implements the main thread loop and enough decision logic so that it knows which device to take messages from and which device to send the messages to.\r
 * \r
 */\r
-void* CAN_GW_thread(void* arg){\r
+static void* CAN_GW_thread(void* arg){\r
     int fd_in, fd_out;\r
     int res;\r
-    unsigned long int *total, *succ, *err;\r
+#ifndef BENCH_BUILD\r
+    unsigned long int *total = NULL, *succ = NULL, *err = NULL; /* Makes compiler shut up about warnings. */\r
+#endif\r
     \r
     struct can_message canmsg;\r
     struct mscan_rx_parms rxparms;\r
@@ -73,19 +76,25 @@ void* CAN_GW_thread(void* arg){
     if (((int)arg) == CAN_GW_A_TO_B_MODE){\r
         fd_in = open(MSCAN_A_DEV_NAME, O_RDWR);\r
         fd_out = open(MSCAN_B_DEV_NAME, O_RDWR);\r
-        total = &total_1;\r
-        succ = &succ_1;\r
-        err = &err_1;\r
     } else if (((int)arg) == CAN_GW_B_TO_A_MODE){\r
         fd_in = open(MSCAN_B_DEV_NAME, O_RDWR);\r
         fd_out = open(MSCAN_A_DEV_NAME, O_RDWR);\r
+    } else {\r
+        /* Invalid argument, terminate self. */\r
+        return NULL;\r
+    }\r
+\r
+#ifndef BENCH_BUILD /* Overhead decrease for benching builds.*/\r
+    if (((int)arg) == CAN_GW_A_TO_B_MODE){\r
+        total = &total_1;\r
+        succ = &succ_1;\r
+        err = &err_1;\r
+    } else {\r
         total = &total_2;\r
         succ = &succ_2;\r
         err = &err_2;\r
-    } else {\r
-        /* Invalid argument, terminates itself. */\r
-        rtems_task_delete(RTEMS_SELF);\r
     }\r
+#endif\r
     \r
     /* CANs are set to proper baudrate outside of these task due to potential race condition. */\r
 \r
@@ -105,12 +114,17 @@ void* CAN_GW_thread(void* arg){
             txparms.tx_idx = canmsg.mess_id;\r
             res = write(fd_out, &txparms, sizeof(txparms));\r
             if (res < 0) {\r
+#ifndef BENCH_BUILD /* This is the simplest way to remove all the counting */\r
                 /* Retry? Decide later. */\r
                 (*err)++;\r
             } else {\r
                 (*succ)++;\r
             }\r
             (*total)++;\r
+#else\r
+            } /* Turns it into empty loop. */\r
+#endif\r
+\r
         }\r
     }\r
     return NULL;\r
@@ -119,22 +133,35 @@ void* CAN_GW_thread(void* arg){
 \r
 static int start_tasks(){\r
     int res;\r
+\r
     \r
-    res = pthread_create(&CAN_A_to_B_thread, NULL, CAN_GW_thread, (void*)CAN_GW_A_TO_B_MODE);\r
+    pthread_attr_t attributes;\r
+    struct sched_param parm;    \r
+    parm.sched_priority = CAN_GW_TASK_HIGH_PRIO;\r
+    pthread_attr_init(&attributes);\r
+#ifdef HIGH_PRIO /* Without setting PTHREAD_EXPLICIT_SCHED, thread is created with parameters inherited from this thread. */\r
+    res = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED);\r
+#endif\r
+    pthread_attr_setschedparam(&attributes, &parm);\r
+\r
+    res = pthread_create(&CAN_A_to_B_thread, &attributes, CAN_GW_thread, (void*)CAN_GW_A_TO_B_MODE);\r
     if (res != 0){\r
         /* No cleanup is needed here. */\r
         return 1;\r
     }\r
 \r
-    res = pthread_create(&CAN_B_to_A_thread, NULL, CAN_GW_thread, (void*)CAN_GW_B_TO_A_MODE);\r
+    res = pthread_create(&CAN_B_to_A_thread, &attributes, CAN_GW_thread, (void*)CAN_GW_B_TO_A_MODE);\r
     if (res != 0){\r
         /* First thread needs to be aborted before return. */\r
         pthread_cancel(CAN_A_to_B_thread);\r
         return 1;\r
     }\r
     \r
-//    pthread_detach(CAN_B_to_A_thread);\r
-//    pthread_detach(CAN_A_to_B_thread);\r
+    pthread_attr_destroy(&attributes);\r
+    \r
+    /* detach is needed so that the threads call clean up handlers automatically. */\r
+    pthread_detach(CAN_B_to_A_thread);\r
+    pthread_detach(CAN_A_to_B_thread);\r
 \r
     /* Threads are started and running at this point. */\r
     return 0;\r
@@ -170,13 +197,13 @@ static int end_tasks(){
     printf("Attempting to stop thread 1\n");\r
     res = pthread_cancel(CAN_A_to_B_thread);\r
     if (res != 0){\r
-        printf("Failed./n");\r
+        printf("Failed.\n");\r
         /* Not sure what to do with error here, will have to figure out later. */\r
     }\r
     printf("Attempting to stop thread 2\n");\r
     res = pthread_cancel(CAN_B_to_A_thread);\r
     if (res != 0){\r
-        printf("Failed./n");\r
+        printf("Failed.\n");\r
         /* Not sure what to do with error here, will have to figure out later. */\r
     }\r
     sleep(1);\r
@@ -194,7 +221,7 @@ static int set_baudrate(int baudrate, int* fd1, int* fd2){
     memset(&ctrl_parms, 0, sizeof(ctrl_parms));\r
     ctrl_parms.ctrl_can_bitrate = baudrate;\r
 \r
-    printf("Attempting to set bitrate %u for fd.\n",  ctrl_parms.ctrl_can_bitrate);\r
+    printf("Attempting to set bitrate %"PRIu32" for fd.\n",  ctrl_parms.ctrl_can_bitrate);\r
     \r
     fd = open(MSCAN_A_DEV_NAME, O_RDWR);\r
     if (fd < 0){\r