From: Michal MalĂ˝ Date: Wed, 9 Jul 2014 12:44:03 +0000 (+0200) Subject: Simplify application termination code X-Git-Url: https://gitweb.devoid-pointer.net/?a=commitdiff_plain;h=41777d97499653eb4514c087e3b30910cf5c90cf;p=LFSBench.git Simplify application termination code --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 566204a..362f71e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/appwindow.c b/appwindow.c index a849213..24163ed 100644 --- a/appwindow.c +++ b/appwindow.c @@ -16,7 +16,7 @@ */ #include "appwindow.h" -#include "sharedstructs.h" +#include "shared.h" #include #include @@ -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; diff --git a/fpscounter.c b/fpscounter.c index fb16071..c30cd02 100644 --- a/fpscounter.c +++ b/fpscounter.c @@ -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 +} diff --git a/fpscounter.h b/fpscounter.h index 0ca1862..8975d75 100644 --- a/fpscounter.h +++ b/fpscounter.h @@ -1,7 +1,7 @@ #ifndef _FPSCOUNTER_H #define _FPSCOUNTER_H -#include "sharedstructs.h" +#include "shared.h" #include #include @@ -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 diff --git a/keyblogger.c b/keyblogger.c index 4f7be43..abc6350 100644 --- a/keyblogger.c +++ b/keyblogger.c @@ -1,7 +1,7 @@ #include "appwindow.h" #include "keyblogger.h" #include "fpscounter.h" -#include "sharedstructs.h" +#include "shared.h" #include #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 eccd386..fffd8ec 100644 --- 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 #include @@ -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 index 0000000..3724869 --- /dev/null +++ b/shared.h @@ -0,0 +1,14 @@ +#ifndef _SHAREDSTRUCTS_H +#define _SHAREDSTRUCTS_H + +#include + +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 index 13c74df..0000000 --- a/sharedstructs.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef _SHAREDSTRUCTS_H -#define _SHAREDSTRUCTS_H - -#include - -typedef struct { - pthread_mutex_t* exit_mutex; - int* pexit_app; -} exit_thrdata; - -#endif \ No newline at end of file