8 #include "../smalloc.h"
12 static void tp_flush_work(struct flist_head *list)
16 while (!flist_empty(list)) {
17 work = flist_entry(list->next, struct tp_work, list);
18 flist_del(&work->list);
23 static void *tp_thread(void *data)
25 struct tp_data *tdat = data;
26 struct flist_head work_list;
28 INIT_FLIST_HEAD(&work_list);
31 pthread_mutex_lock(&tdat->lock);
33 if (!tdat->thread_exit && flist_empty(&tdat->work))
34 pthread_cond_wait(&tdat->cv, &tdat->lock);
36 if (!flist_empty(&tdat->work))
37 flist_splice_tail_init(&tdat->work, &work_list);
39 pthread_mutex_unlock(&tdat->lock);
41 if (flist_empty(&work_list)) {
42 if (tdat->thread_exit)
47 tp_flush_work(&work_list);
53 void tp_queue_work(struct tp_data *tdat, struct tp_work *work)
57 pthread_mutex_lock(&tdat->lock);
58 flist_add_tail(&work->list, &tdat->work);
59 pthread_cond_signal(&tdat->cv);
60 pthread_mutex_unlock(&tdat->lock);
63 void tp_init(struct tp_data **tdatp)
71 *tdatp = tdat = smalloc(sizeof(*tdat));
72 pthread_mutex_init(&tdat->lock, NULL);
73 INIT_FLIST_HEAD(&tdat->work);
74 pthread_cond_init(&tdat->cv, NULL);
75 pthread_cond_init(&tdat->sleep_cv, NULL);
77 ret = pthread_create(&tdat->thread, NULL, tp_thread, tdat);
79 log_err("fio: failed to create tp thread\n");
82 void tp_exit(struct tp_data **tdatp)
84 struct tp_data *tdat = *tdatp;
90 tdat->thread_exit = 1;
91 pthread_mutex_lock(&tdat->lock);
92 pthread_cond_signal(&tdat->cv);
93 pthread_mutex_unlock(&tdat->lock);
95 pthread_join(tdat->thread, &ret);