]> Devoid-pointer.net GitWeb - LFSBench.git/commitdiff
Simplify application termination code
authorMichal Malý <madcatxster@devoid-pointer.net>
Wed, 9 Jul 2014 12:44:03 +0000 (14:44 +0200)
committerMichal Malý <madcatxster@devoid-pointer.net>
Wed, 9 Jul 2014 12:44:03 +0000 (14:44 +0200)
CMakeLists.txt
appwindow.c
fpscounter.c
fpscounter.h
keyblogger.c
main.c
shared.h [new file with mode: 0644]
sharedstructs.h [deleted file]

index 566204ac17d94269a15390a14a25c4feb889fb65..362f71ea8a96b1b9eb2d2d6fc6e3b657320679dd 100644 (file)
@@ -11,7 +11,8 @@ set(lfsLinBench_SRCS
     appwindow.c
     fpscounter.c
     keyblogger.c
-    main.c)
+    main.c
+    shared.c)
 
 add_executable(lfsLinBench ${lfsLinBench_SRCS})
-target_link_libraries(lfsLinBench ${X11_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} m cap)
\ No newline at end of file
+target_link_libraries(lfsLinBench ${X11_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} m cap)
index a8492130004dbc53975422d072bd3bd6f6a338d1..24163ed2312b80fa1e469a2c05105b0e912a1aa1 100644 (file)
@@ -16,7 +16,7 @@
  */
 
 #include "appwindow.h"
-#include "sharedstructs.h"
+#include "shared.h"
 
 #include <X11/Xutil.h>
 #include <X11/keysym.h>
@@ -176,10 +176,9 @@ void* window_event_loop(void* args)
       case KeyPress:
        if (Xev.xkey.keycode == KC_Q) {
          run_loop = 0;
-         pthread_mutex_lock(etd->exit_mutex);
-         *(etd->pexit_app) = 1;
-         pthread_mutex_unlock(etd->exit_mutex);
+         stop_app(etd);
        }
+       break;
       case Expose:
        redraw_window();
        break;
@@ -189,9 +188,7 @@ void* window_event_loop(void* args)
       case ClientMessage:
        if (Xev.xclient.data.l[0] == wmDeleteWindow) {
          run_loop = 0;
-         pthread_mutex_lock(etd->exit_mutex);
-         *(etd->pexit_app) = 1;
-         pthread_mutex_unlock(etd->exit_mutex);
+         stop_app(etd);
        }
        break;
       default:
@@ -199,10 +196,8 @@ void* window_event_loop(void* args)
     }
 
     /* Check if we should exit the loop */
-    pthread_mutex_lock(etd->exit_mutex);
-    if (*(etd->pexit_app) == 1)
+    if (is_app_stopped(etd))
       run_loop = 0;
-    pthread_mutex_unlock(etd->exit_mutex);
   }
 
   return NULL;
index fb160717eef5b77d98f3d5baccbc29ff9b94ac48..c30cd02f247d95733b3cb91b0e60118b88dbac80 100644 (file)
@@ -78,12 +78,8 @@ int init_fps_counter(exit_thrdata* etd)
     if (lfs_pid == 0)
       lfs_pid = find_pid_by_name(noexe);
 
-    pthread_mutex_lock(etd->exit_mutex);
-    if (*(etd->pexit_app) == 1) {
-      pthread_mutex_unlock(etd->exit_mutex);
+    if (is_app_stopped(etd))
       goto out;
-    }
-    pthread_mutex_unlock(etd->exit_mutex);
     sleep(1);
   }
   append_window_text("LFS process found.", 0);
@@ -351,4 +347,4 @@ static int stop_process()
   }
   waitpid(lfs_pid, NULL, 0);
   return 0;
-}
\ No newline at end of file
+}
index 0ca18622a118c01eee30c3a8e9041e5a0f5dcd80..8975d75c9d95ecba754013f15321a33da2b23780 100644 (file)
@@ -1,7 +1,7 @@
 #ifndef _FPSCOUNTER_H
 #define _FPSCOUNTER_H
 
-#include "sharedstructs.h"
+#include "shared.h"
 #include <fcntl.h>
 #include <unistd.h>
 
@@ -9,4 +9,4 @@ int init_fps_counter(exit_thrdata* etd);
 int start_stop_benchmark();
 void stop_benchmark();
 
-#endif
\ No newline at end of file
+#endif
index 4f7be439b4e48d16f1b8c0ebcd404ca08d9d5323..abc63500ac9c4ec2d7672282109b47004ab04a9f 100644 (file)
@@ -1,7 +1,7 @@
 #include "appwindow.h"
 #include "keyblogger.h"
 #include "fpscounter.h"
-#include "sharedstructs.h"
+#include "shared.h"
 #include <linux/input.h>
 
 #define EV_RELEASED 0
@@ -37,10 +37,8 @@ void* get_keyb_events(void* args)
       }
     }
 
-    pthread_mutex_lock(etd->exit_mutex);
-    if (*(etd->pexit_app) == 1)
+    if (is_app_stopped(etd))
       run_loop = 0;
-    pthread_mutex_unlock(etd->exit_mutex);
   }
 
   return NULL;
diff --git a/main.c b/main.c
index eccd386a4ce6ae8139ff59bb5cd4a7e31adf609c..fffd8ec8680e450b04327e49131625b27739305b 100644 (file)
--- a/main.c
+++ b/main.c
@@ -18,7 +18,7 @@
 #include "appwindow.h"
 #include "fpscounter.h"
 #include "keyblogger.h"
-#include "sharedstructs.h"
+#include "shared.h"
 #include <sys/capability.h>
 #include <unistd.h>
 
@@ -28,6 +28,7 @@ pthread_mutex_t exit_mutex;
 
 char* evdev_path;
 int exit_app;
+exit_thrdata exdata;
 
 int main(int argc, char** argv)
 { 
@@ -74,15 +75,11 @@ int main(int argc, char** argv)
   }
 
   /* Setup threading data */
-  exit_thrdata etd;
-  exit_app = 0;
-  if (pthread_mutex_init(&exit_mutex, NULL) != 0) {
+  if (pthread_mutex_init(&exdata.exit_mutex, NULL) != 0) {
     fprintf(stderr, "CRITICAL: Cannot create exit_mutex!");
     goto bail_out_1;
   }
-
-  etd.exit_mutex = &exit_mutex;
-  etd.pexit_app = &exit_app;
+  exdata.exit_app = 0;
 
   if (XInitThreads() <= 0) {
       fprintf(stderr, "Unable to initialize X11 threads\n");
@@ -92,7 +89,7 @@ int main(int argc, char** argv)
   if (create_window() < 0)
     goto bail_out_2;
   /* Start main window event loop */
-  if (pthread_create(&win_event_loop, NULL, &window_event_loop, &etd) != 0) {
+  if (pthread_create(&win_event_loop, NULL, &window_event_loop, &exdata) != 0) {
     fprintf(stderr, "CRITICAL: Cannot start main window event loop!\n");
     goto bail_out_2;
   }
@@ -100,10 +97,10 @@ int main(int argc, char** argv)
   if (init_keyb_logging(evdev_path) != 0)
     goto bail_out_3;
 
-  if (init_fps_counter(&etd) != 0)
+  if (init_fps_counter(&exdata) != 0)
     goto bail_out_3;
 
-  if (pthread_create(&kb_event_loop, NULL, &get_keyb_events, &etd) != 0) {
+  if (pthread_create(&kb_event_loop, NULL, &get_keyb_events, &exdata) != 0) {
     fprintf(stderr, "CRITICAL: Cannot start boundkeys event loop!\n");
     goto bail_out_3;
   }
@@ -120,9 +117,10 @@ int main(int argc, char** argv)
   return EXIT_SUCCESS;
 
 bail_out_3:
+  stop_app(&exdata);
   pthread_join(win_event_loop, NULL); 
 bail_out_2:
-  pthread_mutex_destroy(&exit_mutex);
+  pthread_mutex_destroy(&exdata.exit_mutex);
 bail_out_1:
   free(evdev_path);
   return EXIT_FAILURE;
diff --git a/shared.h b/shared.h
new file mode 100644 (file)
index 0000000..3724869
--- /dev/null
+++ b/shared.h
@@ -0,0 +1,14 @@
+#ifndef _SHAREDSTRUCTS_H
+#define _SHAREDSTRUCTS_H
+
+#include <pthread.h>
+
+typedef struct {
+  pthread_mutex_t exit_mutex;
+  int exit_app;
+} exit_thrdata;
+
+int is_app_stopped(exit_thrdata* exdata);
+void stop_app(exit_thrdata* exdata);
+
+#endif
diff --git a/sharedstructs.h b/sharedstructs.h
deleted file mode 100644 (file)
index 13c74df..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-#ifndef _SHAREDSTRUCTS_H
-#define _SHAREDSTRUCTS_H
-
-#include <pthread.h>
-
-typedef struct {
-  pthread_mutex_t* exit_mutex;
-  int* pexit_app;
-} exit_thrdata;
-
-#endif
\ No newline at end of file