]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
kvm tools: threadpool exit routine
authorSasha Levin <levinsasha928@gmail.com>
Wed, 5 Sep 2012 08:31:49 +0000 (10:31 +0200)
committerPekka Enberg <penberg@kernel.org>
Wed, 5 Sep 2012 13:22:37 +0000 (16:22 +0300)
Add an exit function for the threadpool which will stop all running threads in the
pool. Also clean up the init code a bit.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
tools/kvm/builtin-run.c
tools/kvm/include/kvm/threadpool.h
tools/kvm/util/threadpool.c

index d0079278add061b0fe7b0589dbb9d53a87f24b30..5e6daa8ae679df32a4c3b39e2688b74e336d26b4 100644 (file)
@@ -1216,7 +1216,12 @@ static int kvm_cmd_run_init(int argc, const char **argv)
                }
        }
 
-       thread_pool__init(nr_online_cpus);
+       r = thread_pool__init(kvm);
+       if (r < 0) {
+               pr_err("thread_pool__init() failed with error %d\n", r);
+               goto fail;
+       }
+
 fail:
        return r;
 }
@@ -1301,6 +1306,10 @@ static void kvm_cmd_run_exit(int guest_ret)
        if (r < 0)
                pr_warning("pci__exit() failed with error %d\n", r);
 
+       r = thread_pool__exit(kvm);
+       if (r < 0)
+               pr_warning("thread_pool__exit() failed with error %d\n", r);
+
        r = kvm__exit(kvm);
        if (r < 0)
                pr_warning("pci__exit() failed with error %d\n", r);
index 768239f94e08f6e263296fdb4a8a2fd39b06fa74..abe46eaaa65fb7416b40aa9ba5c3d31f1d098ef1 100644 (file)
@@ -30,7 +30,8 @@ static inline void thread_pool__init_job(struct thread_pool__job *job, struct kv
        };
 }
 
-int thread_pool__init(unsigned long thread_count);
+int thread_pool__init(struct kvm *kvm);
+int thread_pool__exit(struct kvm *kvm);
 
 void thread_pool__do_job(struct thread_pool__job *job);
 
index bafbcd72009011239d920286f5baf97393c8f11a..6c7566de9d82adcc42267665f527e0c877cf9c8a 100644 (file)
@@ -14,6 +14,7 @@ static LIST_HEAD(head);
 
 static pthread_t       *threads;
 static long            threadcount;
+static bool            running;
 
 static struct thread_pool__job *thread_pool__job_pop_locked(void)
 {
@@ -76,15 +77,16 @@ static void *thread_pool__threadfunc(void *param)
 {
        pthread_cleanup_push(thread_pool__threadfunc_cleanup, NULL);
 
-       for (;;) {
+       while (running) {
                struct thread_pool__job *curjob;
 
                mutex_lock(&job_mutex);
-               while ((curjob = thread_pool__job_pop_locked()) == NULL)
+               while (running && (curjob = thread_pool__job_pop_locked()) == NULL)
                        pthread_cond_wait(&job_cond, &job_mutex);
                mutex_unlock(&job_mutex);
 
-               thread_pool__handle_job(curjob);
+               if (running)
+                       thread_pool__handle_job(curjob);
        }
 
        pthread_cleanup_pop(0);
@@ -116,9 +118,12 @@ static int thread_pool__addthread(void)
        return res;
 }
 
-int thread_pool__init(unsigned long thread_count)
+int thread_pool__init(struct kvm *kvm)
 {
        unsigned long i;
+       unsigned int thread_count = sysconf(_SC_NPROCESSORS_ONLN);
+
+       running = true;
 
        for (i = 0; i < thread_count; i++)
                if (thread_pool__addthread() < 0)
@@ -127,6 +132,26 @@ int thread_pool__init(unsigned long thread_count)
        return i;
 }
 
+int thread_pool__exit(struct kvm *kvm)
+{
+       int i;
+       void *NUL = NULL;
+
+       running = false;
+
+       for (i = 0; i < threadcount; i++) {
+               mutex_lock(&job_mutex);
+               pthread_cond_signal(&job_cond);
+               mutex_unlock(&job_mutex);
+       }
+
+       for (i = 0; i < threadcount; i++) {
+               pthread_join(threads[i], NUL);
+       }
+
+       return 0;
+}
+
 void thread_pool__do_job(struct thread_pool__job *job)
 {
        struct thread_pool__job *jobinfo = job;