/*1* Copyright (C) 2007 Oracle. All rights reserved.2*3* This program is free software; you can redistribute it and/or4* modify it under the terms of the GNU General Public5* License v2 as published by the Free Software Foundation.6*7* This program is distributed in the hope that it will be useful,8* but WITHOUT ANY WARRANTY; without even the implied warranty of9* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU10* General Public License for more details.11*12* You should have received a copy of the GNU General Public13* License along with this program; if not, write to the14* Free Software Foundation, Inc., 59 Temple Place - Suite 330,15* Boston, MA 021110-1307, USA.16*/1718#ifndef __BTRFS_ASYNC_THREAD_19#define __BTRFS_ASYNC_THREAD_2021struct btrfs_worker_thread;2223/*24* This is similar to a workqueue, but it is meant to spread the operations25* across all available cpus instead of just the CPU that was used to26* queue the work. There is also some batching introduced to try and27* cut down on context switches.28*29* By default threads are added on demand up to 2 * the number of cpus.30* Changing struct btrfs_workers->max_workers is one way to prevent31* demand creation of kthreads.32*33* the basic model of these worker threads is to embed a btrfs_work34* structure in your own data struct, and use container_of in a35* work function to get back to your data struct.36*/37struct btrfs_work {38/*39* func should be set to the function you want called40* your work struct is passed as the only arg41*42* ordered_func must be set for work sent to an ordered work queue,43* and it is called to complete a given work item in the same44* order they were sent to the queue.45*/46void (*func)(struct btrfs_work *work);47void (*ordered_func)(struct btrfs_work *work);48void (*ordered_free)(struct btrfs_work *work);4950/*51* flags should be set to zero. It is used to make sure the52* struct is only inserted once into the list.53*/54unsigned long flags;5556/* don't touch these */57struct btrfs_worker_thread *worker;58struct list_head list;59struct list_head order_list;60};6162struct btrfs_workers {63/* current number of running workers */64int num_workers;6566int num_workers_starting;6768/* max number of workers allowed. changed by btrfs_start_workers */69int max_workers;7071/* once a worker has this many requests or fewer, it is idle */72int idle_thresh;7374/* force completions in the order they were queued */75int ordered;7677/* more workers required, but in an interrupt handler */78int atomic_start_pending;7980/*81* are we allowed to sleep while starting workers or are we required82* to start them at a later time? If we can't sleep, this indicates83* which queue we need to use to schedule thread creation.84*/85struct btrfs_workers *atomic_worker_start;8687/* list with all the work threads. The workers on the idle thread88* may be actively servicing jobs, but they haven't yet hit the89* idle thresh limit above.90*/91struct list_head worker_list;92struct list_head idle_list;9394/*95* when operating in ordered mode, this maintains the list96* of work items waiting for completion97*/98struct list_head order_list;99struct list_head prio_order_list;100101/* lock for finding the next worker thread to queue on */102spinlock_t lock;103104/* lock for the ordered lists */105spinlock_t order_lock;106107/* extra name for this worker, used for current->name */108char *name;109};110111int btrfs_queue_worker(struct btrfs_workers *workers, struct btrfs_work *work);112int btrfs_start_workers(struct btrfs_workers *workers, int num_workers);113int btrfs_stop_workers(struct btrfs_workers *workers);114void btrfs_init_workers(struct btrfs_workers *workers, char *name, int max,115struct btrfs_workers *async_starter);116int btrfs_requeue_work(struct btrfs_work *work);117void btrfs_set_work_high_prio(struct btrfs_work *work);118#endif119120121