workqueue: add nice support
[fio.git] / lib / tp.c
index ec52db6da3f98a436a2a09381e42e9e5da35ead7..7462f5bfa824cbfc7fff44a50fe76ee8fe0919a9 100644 (file)
--- a/lib/tp.c
+++ b/lib/tp.c
@@ -11,6 +11,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <pthread.h>
+#include <string.h>
 
 #include "../smalloc.h"
 #include "../log.h"
@@ -21,9 +22,19 @@ static void tp_flush_work(struct flist_head *list)
        struct tp_work *work;
 
        while (!flist_empty(list)) {
+               int prio;
+
                work = flist_entry(list->next, struct tp_work, list);
                flist_del(&work->list);
+
+               prio = work->prio;
+               if (nice(prio) < 0)
+                       log_err("fio: nice %s\n", strerror(errno));
+
                work->fn(work);
+
+               if (nice(prio) < 0)
+                       log_err("fio: nice %s\n", strerror(errno));
        }
 }
 
@@ -63,8 +74,9 @@ void tp_queue_work(struct tp_data *tdat, struct tp_work *work)
 
        pthread_mutex_lock(&tdat->lock);
        flist_add_tail(&work->list, &tdat->work);
-       pthread_cond_signal(&tdat->cv);
        pthread_mutex_unlock(&tdat->lock);
+
+       pthread_cond_signal(&tdat->cv);
 }
 
 void tp_init(struct tp_data **tdatp)
@@ -94,11 +106,12 @@ void tp_exit(struct tp_data **tdatp)
        if (!tdat)
                return;
 
-       tdat->thread_exit = 1;
        pthread_mutex_lock(&tdat->lock);
-       pthread_cond_signal(&tdat->cv);
+       tdat->thread_exit = 1;
        pthread_mutex_unlock(&tdat->lock);
 
+       pthread_cond_signal(&tdat->cv);
+
        pthread_join(tdat->thread, &ret);
 
        sfree(tdat);