]> git.proxmox.com Git - mirror_lxcfs.git/blobdiff - bindings.c
loadavg: restart thread on library reload
[mirror_lxcfs.git] / bindings.c
index b20eaa053b92877683163a27206143c8c9eed36a..d03792bfdcbbf405bc368c00c886980d0cec1bb6 100644 (file)
@@ -97,6 +97,7 @@ struct file_info {
  * 1 means use loadavg, 0 means not use.
  */
 static int loadavg = 0;
+static volatile sig_atomic_t loadavg_stop = 0;
 static int calc_hash(char *name)
 {
        unsigned int hash = 0;
@@ -247,7 +248,7 @@ static struct load_node *del_node(struct load_node *n, int locate)
        return g;
 }
 
-void load_free(void)
+static void load_free(void)
 {
        int i;
        struct load_node *f, *p;
@@ -4500,6 +4501,9 @@ void *load_begin(void *arg)
        clock_t time1, time2;
 
        while (1) {
+               if (loadavg_stop == 1)
+                       return NULL;
+
                time1 = clock();
                for (i = 0; i < LOAD_SIZE; i++) {
                        pthread_mutex_lock(&load_hash[i].lock);
@@ -4536,6 +4540,10 @@ out:                                     f = f->next;
                                }
                        }
                }
+
+               if (loadavg_stop == 1)
+                       return NULL;
+
                time2 = clock();
                usleep(FLUSH_TIME * 1000000 - (int)((time2 - time1) * 1000000 / CLOCKS_PER_SEC));
        }
@@ -4649,6 +4657,26 @@ pthread_t load_daemon(int load_use)
        return pid;
 }
 
+/* Returns 0 on success. */
+int stop_load_daemon(pthread_t pid)
+{
+       int s;
+
+       /* Signal the thread to gracefully stop */
+       loadavg_stop = 1;
+
+       s = pthread_join(pid, NULL); /* Make sure sub thread has been canceled. */
+       if (s != 0) {
+               lxcfs_error("%s\n", "stop_load_daemon error: failed to join");
+               return -1;
+       }
+
+       load_free();
+       loadavg_stop = 0;
+
+       return 0;
+}
+
 static off_t get_procfile_size(const char *which)
 {
        FILE *f = fopen(which, "r");