aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorJ08nY2018-02-03 20:09:25 +0100
committerJ08nY2018-02-03 20:09:25 +0100
commited7e99ebc7c50523e5a2c6f21c8f89028348da71 (patch)
tree8e153607291099ebbaf0833a0e0db09dc9a5ed2c /src/util
parentcfdddb2a57ad77f485eb4be1a52efe5ffe19a220 (diff)
downloadecgen-ed7e99ebc7c50523e5a2c6f21c8f89028348da71.tar.gz
ecgen-ed7e99ebc7c50523e5a2c6f21c8f89028348da71.tar.zst
ecgen-ed7e99ebc7c50523e5a2c6f21c8f89028348da71.zip
Fix Valgrind warnings about timeout struct allocations.
- Make them dynamically allocated, per thread, not on stack. - Also fix a small invalid read of deallocated generator point.
Diffstat (limited to 'src/util')
-rw-r--r--src/util/timeout.c21
-rw-r--r--src/util/timeout.h36
2 files changed, 43 insertions, 14 deletions
diff --git a/src/util/timeout.c b/src/util/timeout.c
index d273ecb..21f9591 100644
--- a/src/util/timeout.c
+++ b/src/util/timeout.c
@@ -3,10 +3,12 @@
* Copyright (C) 2017-2018 J08nY
*/
#include "timeout.h"
+#include "util/memory.h"
__thread jmp_buf timeout_ptr;
__thread bool timeout_in = false;
-__thread timer_t timeout_timer;
+__thread timer_t *timeout_timer;
+__thread struct sigevent *sevp;
void timeout_handle(int signum, siginfo_t *siginfo, void *other) {
sigset_t mask;
@@ -17,7 +19,19 @@ void timeout_handle(int signum, siginfo_t *siginfo, void *other) {
if (timeout_in) siglongjmp(timeout_ptr, 1);
}
+void timeout_thread_init() {
+ timeout_timer = try_calloc(sizeof(timer_t));
+ sevp = try_calloc(sizeof(struct sigevent));
+}
+
+void timeout_thread_quit() {
+ try_free(timeout_timer);
+ try_free(sevp);
+}
+
bool timeout_init() {
+ // init for the main thread.
+ timeout_thread_init();
struct sigaction new_action;
new_action.sa_sigaction = timeout_handle;
@@ -26,4 +40,9 @@ bool timeout_init() {
sigaction(SIGALRM, &new_action, NULL);
return true;
+}
+
+void timeout_quit() {
+ // deinit the main thread.
+ timeout_thread_quit();
} \ No newline at end of file
diff --git a/src/util/timeout.h b/src/util/timeout.h
index 7aa3727..8f545dd 100644
--- a/src/util/timeout.h
+++ b/src/util/timeout.h
@@ -17,7 +17,8 @@
extern __thread sigjmp_buf timeout_ptr;
extern __thread bool timeout_in;
-extern __thread timer_t timeout_timer;
+extern __thread timer_t *timeout_timer;
+extern __thread struct sigevent *sevp;
/**
* @brief Start a timer that times out after <code>seconds-</code>.
@@ -31,16 +32,16 @@ extern __thread timer_t timeout_timer;
*/
#define timeout_start(seconds) \
if ((seconds) != 0) { \
- struct sigevent sevp; \
- sevp.sigev_notify = SIGEV_THREAD_ID; \
- sevp.sigev_signo = SIGALRM; \
- sevp._sigev_un._tid = (__pid_t)syscall(SYS_gettid); \
+ sevp->sigev_notify = SIGEV_THREAD_ID; \
+ sevp->sigev_signo = SIGALRM; \
+ sevp->sigev_value.sival_int = 0; \
+ sevp->_sigev_un._tid = (__pid_t)syscall(SYS_gettid); \
\
- timer_create(CLOCK_MONOTONIC, &sevp, &timeout_timer); \
+ timer_create(CLOCK_MONOTONIC, sevp, timeout_timer); \
struct itimerspec timer_time = { \
.it_interval = {.tv_sec = 0, .tv_nsec = 0}, \
.it_value = {.tv_sec = (seconds), .tv_nsec = 0}}; \
- timer_settime(timeout_timer, 0, &timer_time, NULL); \
+ timer_settime(*timeout_timer, 0, &timer_time, NULL); \
timeout_in = true; \
}; \
if ((seconds) != 0 && sigsetjmp(timeout_ptr, 1) == 1)
@@ -48,18 +49,27 @@ extern __thread timer_t timeout_timer;
/**
* @brief Stop a timer.
*/
-#define timeout_stop() \
- { \
- if (timeout_in) { \
- timeout_in = false; \
- timer_delete(timeout_timer); \
- } \
+#define timeout_stop() \
+ { \
+ if (timeout_in) { \
+ timeout_in = false; \
+ timer_delete(*timeout_timer); \
+ } \
}
+void timeout_thread_init();
+
+void timeout_thread_quit();
+
/**
* @brief Initialize the timeout system.
* @return whether the initalization was successful
*/
bool timeout_init();
+/**
+ * @brief Deinitialize the timeout system.
+ */
+void timeout_quit();
+
#endif // ECGEN_TIMEOUT_H