async-thread.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. * Copyright (C) 2014 Fujitsu. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public
  7. * License v2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public
  15. * License along with this program; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 021110-1307, USA.
  18. */
  19. #ifndef __BTRFS_ASYNC_THREAD_
  20. #define __BTRFS_ASYNC_THREAD_
  21. struct btrfs_worker_thread;
  22. /*
  23. * This is similar to a workqueue, but it is meant to spread the operations
  24. * across all available cpus instead of just the CPU that was used to
  25. * queue the work. There is also some batching introduced to try and
  26. * cut down on context switches.
  27. *
  28. * By default threads are added on demand up to 2 * the number of cpus.
  29. * Changing struct btrfs_workers->max_workers is one way to prevent
  30. * demand creation of kthreads.
  31. *
  32. * the basic model of these worker threads is to embed a btrfs_work
  33. * structure in your own data struct, and use container_of in a
  34. * work function to get back to your data struct.
  35. */
  36. struct btrfs_work {
  37. /*
  38. * func should be set to the function you want called
  39. * your work struct is passed as the only arg
  40. *
  41. * ordered_func must be set for work sent to an ordered work queue,
  42. * and it is called to complete a given work item in the same
  43. * order they were sent to the queue.
  44. */
  45. void (*func)(struct btrfs_work *work);
  46. void (*ordered_func)(struct btrfs_work *work);
  47. void (*ordered_free)(struct btrfs_work *work);
  48. /*
  49. * flags should be set to zero. It is used to make sure the
  50. * struct is only inserted once into the list.
  51. */
  52. unsigned long flags;
  53. /* don't touch these */
  54. struct btrfs_worker_thread *worker;
  55. struct list_head list;
  56. struct list_head order_list;
  57. };
  58. struct btrfs_workers {
  59. /* current number of running workers */
  60. int num_workers;
  61. int num_workers_starting;
  62. /* max number of workers allowed. changed by btrfs_start_workers */
  63. int max_workers;
  64. /* once a worker has this many requests or fewer, it is idle */
  65. int idle_thresh;
  66. /* force completions in the order they were queued */
  67. int ordered;
  68. /* more workers required, but in an interrupt handler */
  69. int atomic_start_pending;
  70. /*
  71. * are we allowed to sleep while starting workers or are we required
  72. * to start them at a later time? If we can't sleep, this indicates
  73. * which queue we need to use to schedule thread creation.
  74. */
  75. struct btrfs_workers *atomic_worker_start;
  76. /* list with all the work threads. The workers on the idle thread
  77. * may be actively servicing jobs, but they haven't yet hit the
  78. * idle thresh limit above.
  79. */
  80. struct list_head worker_list;
  81. struct list_head idle_list;
  82. /*
  83. * when operating in ordered mode, this maintains the list
  84. * of work items waiting for completion
  85. */
  86. struct list_head order_list;
  87. struct list_head prio_order_list;
  88. /* lock for finding the next worker thread to queue on */
  89. spinlock_t lock;
  90. /* lock for the ordered lists */
  91. spinlock_t order_lock;
  92. /* extra name for this worker, used for current->name */
  93. char *name;
  94. int stopping;
  95. };
  96. void btrfs_queue_worker(struct btrfs_workers *workers, struct btrfs_work *work);
  97. int btrfs_start_workers(struct btrfs_workers *workers);
  98. void btrfs_stop_workers(struct btrfs_workers *workers);
  99. void btrfs_init_workers(struct btrfs_workers *workers, char *name, int max,
  100. struct btrfs_workers *async_starter);
  101. void btrfs_requeue_work(struct btrfs_work *work);
  102. void btrfs_set_work_high_prio(struct btrfs_work *work);
  103. struct btrfs_workqueue_struct;
  104. struct btrfs_work_struct {
  105. void (*func)(struct btrfs_work_struct *arg);
  106. void (*ordered_func)(struct btrfs_work_struct *arg);
  107. void (*ordered_free)(struct btrfs_work_struct *arg);
  108. /* Don't touch things below */
  109. struct work_struct normal_work;
  110. struct list_head ordered_list;
  111. struct btrfs_workqueue_struct *wq;
  112. unsigned long flags;
  113. };
  114. struct btrfs_workqueue_struct *btrfs_alloc_workqueue(char *name,
  115. int flags,
  116. int max_active);
  117. void btrfs_init_work(struct btrfs_work_struct *work,
  118. void (*func)(struct btrfs_work_struct *),
  119. void (*ordered_func)(struct btrfs_work_struct *),
  120. void (*ordered_free)(struct btrfs_work_struct *));
  121. void btrfs_queue_work(struct btrfs_workqueue_struct *wq,
  122. struct btrfs_work_struct *work);
  123. void btrfs_destroy_workqueue(struct btrfs_workqueue_struct *wq);
  124. void btrfs_workqueue_set_max(struct btrfs_workqueue_struct *wq, int max);
  125. #endif