async-thread.h 4.9 KB
Newer Older
1 2
/*
 * Copyright (C) 2007 Oracle.  All rights reserved.
3
 * Copyright (C) 2014 Fujitsu.  All rights reserved.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License v2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 */

#ifndef __BTRFS_ASYNC_THREAD_
#define __BTRFS_ASYNC_THREAD_

struct btrfs_worker_thread;

/*
 * This is similar to a workqueue, but it is meant to spread the operations
 * across all available cpus instead of just the CPU that was used to
 * queue the work.  There is also some batching introduced to try and
 * cut down on context switches.
 *
 * By default threads are added on demand up to 2 * the number of cpus.
 * Changing struct btrfs_workers->max_workers is one way to prevent
 * demand creation of kthreads.
 *
 * the basic model of these worker threads is to embed a btrfs_work
 * structure in your own data struct, and use container_of in a
 * work function to get back to your data struct.
 */
struct btrfs_work {
	/*
C
Chris Mason 已提交
41
	 * func should be set to the function you want called
42
	 * your work struct is passed as the only arg
C
Chris Mason 已提交
43 44 45 46
	 *
	 * ordered_func must be set for work sent to an ordered work queue,
	 * and it is called to complete a given work item in the same
	 * order they were sent to the queue.
47 48
	 */
	void (*func)(struct btrfs_work *work);
C
Chris Mason 已提交
49 50
	void (*ordered_func)(struct btrfs_work *work);
	void (*ordered_free)(struct btrfs_work *work);
51 52 53 54 55 56 57 58 59 60

	/*
	 * flags should be set to zero.  It is used to make sure the
	 * struct is only inserted once into the list.
	 */
	unsigned long flags;

	/* don't touch these */
	struct btrfs_worker_thread *worker;
	struct list_head list;
C
Chris Mason 已提交
61
	struct list_head order_list;
62 63 64 65 66 67
};

struct btrfs_workers {
	/* current number of running workers */
	int num_workers;

68 69
	int num_workers_starting;

70 71 72
	/* max number of workers allowed.  changed by btrfs_start_workers */
	int max_workers;

C
Chris Mason 已提交
73 74 75
	/* once a worker has this many requests or fewer, it is idle */
	int idle_thresh;

C
Chris Mason 已提交
76 77 78
	/* force completions in the order they were queued */
	int ordered;

79 80 81 82 83
	/* more workers required, but in an interrupt handler */
	int atomic_start_pending;

	/*
	 * are we allowed to sleep while starting workers or are we required
84 85
	 * to start them at a later time?  If we can't sleep, this indicates
	 * which queue we need to use to schedule thread creation.
86
	 */
87
	struct btrfs_workers *atomic_worker_start;
88

C
Chris Mason 已提交
89 90 91 92
	/* list with all the work threads.  The workers on the idle thread
	 * may be actively servicing jobs, but they haven't yet hit the
	 * idle thresh limit above.
	 */
93
	struct list_head worker_list;
C
Chris Mason 已提交
94
	struct list_head idle_list;
95

C
Chris Mason 已提交
96 97 98 99 100
	/*
	 * when operating in ordered mode, this maintains the list
	 * of work items waiting for completion
	 */
	struct list_head order_list;
101
	struct list_head prio_order_list;
C
Chris Mason 已提交
102

103 104
	/* lock for finding the next worker thread to queue on */
	spinlock_t lock;
105

106 107 108
	/* lock for the ordered lists */
	spinlock_t order_lock;

C
Chris Mason 已提交
109
	/* extra name for this worker, used for current->name */
110
	char *name;
111 112

	int stopping;
113 114
};

115 116
void btrfs_queue_worker(struct btrfs_workers *workers, struct btrfs_work *work);
int btrfs_start_workers(struct btrfs_workers *workers);
117
void btrfs_stop_workers(struct btrfs_workers *workers);
118 119
void btrfs_init_workers(struct btrfs_workers *workers, char *name, int max,
			struct btrfs_workers *async_starter);
120
void btrfs_requeue_work(struct btrfs_work *work);
121
void btrfs_set_work_high_prio(struct btrfs_work *work);
122 123

struct btrfs_workqueue_struct;
124 125
/* Internal use only */
struct __btrfs_workqueue_struct;
126 127 128 129 130 131 132 133 134

struct btrfs_work_struct {
	void (*func)(struct btrfs_work_struct *arg);
	void (*ordered_func)(struct btrfs_work_struct *arg);
	void (*ordered_free)(struct btrfs_work_struct *arg);

	/* Don't touch things below */
	struct work_struct normal_work;
	struct list_head ordered_list;
135
	struct __btrfs_workqueue_struct *wq;
136 137 138 139 140 141 142 143 144 145 146 147 148 149
	unsigned long flags;
};

struct btrfs_workqueue_struct *btrfs_alloc_workqueue(char *name,
						     int flags,
						     int max_active);
void btrfs_init_work(struct btrfs_work_struct *work,
		     void (*func)(struct btrfs_work_struct *),
		     void (*ordered_func)(struct btrfs_work_struct *),
		     void (*ordered_free)(struct btrfs_work_struct *));
void btrfs_queue_work(struct btrfs_workqueue_struct *wq,
		      struct btrfs_work_struct *work);
void btrfs_destroy_workqueue(struct btrfs_workqueue_struct *wq);
void btrfs_workqueue_set_max(struct btrfs_workqueue_struct *wq, int max);
150
void btrfs_set_work_high_priority(struct btrfs_work_struct *work);
151
#endif