]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - linux/timer.c
Disable pristine-tar option in gbp.conf, since there is no pristine-tar branch.
[bcachefs-tools-debian] / linux / timer.c
index b67a54ac224d5008ea3f436576874080b3291380..7d519a4d85830108b8b0166ada31222d5fe6e59a 100644 (file)
@@ -93,9 +93,11 @@ do {                                                                 \
                                                                        \
        BUG_ON(_i >= (h)->used);                                        \
        (h)->used--;                                                    \
-       heap_swap(h, _i, (h)->used);                                    \
-       heap_sift_down(h, _i, cmp);                                     \
-       heap_sift(h, _i, cmp);                                          \
+       if ((_i) < (h)->used) {                                         \
+               heap_swap(h, _i, (h)->used);                            \
+               heap_sift_down(h, _i, cmp);                             \
+               heap_sift(h, _i, cmp);                                  \
+       }                                                               \
 } while (0)
 
 #define heap_pop(h, d, cmp)                                            \
@@ -244,6 +246,8 @@ out:
        return idx >= 0;
 }
 
+static bool timer_thread_stop = false;
+
 static int timer_thread(void *arg)
 {
        struct pending_timer *p;
@@ -253,7 +257,7 @@ static int timer_thread(void *arg)
 
        pthread_mutex_lock(&timer_lock);
 
-       while (1) {
+       while (!timer_thread_stop) {
                now = jiffies;
                p = heap_peek(&pending_timers);
 
@@ -273,7 +277,7 @@ static int timer_thread(void *arg)
                        BUG_ON(!timer_running());
 
                        pthread_mutex_unlock(&timer_lock);
-                       timer->function(timer->data);
+                       timer->function(timer);
                        pthread_mutex_lock(&timer_lock);
 
                        timer_seq++;
@@ -295,14 +299,31 @@ static int timer_thread(void *arg)
        return 0;
 }
 
+struct task_struct *timer_task;
+
 __attribute__((constructor(103)))
 static void timers_init(void)
 {
-       struct task_struct *p;
-
        heap_init(&pending_timers, 64);
        BUG_ON(!pending_timers.data);
 
-       p = kthread_run(timer_thread, NULL, "timers");
-       BUG_ON(IS_ERR(p));
+       timer_task = kthread_run(timer_thread, NULL, "timers");
+       BUG_ON(IS_ERR(timer_task));
+}
+
+__attribute__((destructor(103)))
+static void timers_cleanup(void)
+{
+       get_task_struct(timer_task);
+
+       pthread_mutex_lock(&timer_lock);
+       timer_thread_stop = true;
+       pthread_cond_signal(&timer_cond);
+       pthread_mutex_unlock(&timer_lock);
+
+       int ret = kthread_stop(timer_task);
+       BUG_ON(ret);
+
+       put_task_struct(timer_task);
+       timer_task = NULL;
 }