slow-work.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Worker thread pool for slow items, such as filesystem lookups or mkdirs
  2. *
  3. * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. *
  11. * See Documentation/slow-work.txt
  12. */
  13. #ifndef _LINUX_SLOW_WORK_H
  14. #define _LINUX_SLOW_WORK_H
  15. #ifdef CONFIG_SLOW_WORK
  16. #include <linux/sysctl.h>
  17. #include <linux/timer.h>
  18. struct slow_work;
  19. #ifdef CONFIG_SLOW_WORK_PROC
  20. struct seq_file;
  21. #endif
  22. /*
  23. * The operations used to support slow work items
  24. */
  25. struct slow_work_ops {
  26. /* owner */
  27. struct module *owner;
  28. /* get a ref on a work item
  29. * - return 0 if successful, -ve if not
  30. */
  31. int (*get_ref)(struct slow_work *work);
  32. /* discard a ref to a work item */
  33. void (*put_ref)(struct slow_work *work);
  34. /* execute a work item */
  35. void (*execute)(struct slow_work *work);
  36. #ifdef CONFIG_SLOW_WORK_PROC
  37. /* describe a work item for /proc */
  38. void (*desc)(struct slow_work *work, struct seq_file *m);
  39. #endif
  40. };
  41. /*
  42. * A slow work item
  43. * - A reference is held on the parent object by the thread pool when it is
  44. * queued
  45. */
  46. struct slow_work {
  47. struct module *owner; /* the owning module */
  48. unsigned long flags;
  49. #define SLOW_WORK_PENDING 0 /* item pending (further) execution */
  50. #define SLOW_WORK_EXECUTING 1 /* item currently executing */
  51. #define SLOW_WORK_ENQ_DEFERRED 2 /* item enqueue deferred */
  52. #define SLOW_WORK_VERY_SLOW 3 /* item is very slow */
  53. #define SLOW_WORK_CANCELLING 4 /* item is being cancelled, don't enqueue */
  54. #define SLOW_WORK_DELAYED 5 /* item is struct delayed_slow_work with active timer */
  55. const struct slow_work_ops *ops; /* operations table for this item */
  56. struct list_head link; /* link in queue */
  57. #ifdef CONFIG_SLOW_WORK_PROC
  58. struct timespec mark; /* jiffies at which queued or exec begun */
  59. #endif
  60. };
  61. struct delayed_slow_work {
  62. struct slow_work work;
  63. struct timer_list timer;
  64. };
  65. /**
  66. * slow_work_init - Initialise a slow work item
  67. * @work: The work item to initialise
  68. * @ops: The operations to use to handle the slow work item
  69. *
  70. * Initialise a slow work item.
  71. */
  72. static inline void slow_work_init(struct slow_work *work,
  73. const struct slow_work_ops *ops)
  74. {
  75. work->flags = 0;
  76. work->ops = ops;
  77. INIT_LIST_HEAD(&work->link);
  78. }
  79. /**
  80. * slow_work_init - Initialise a delayed slow work item
  81. * @work: The work item to initialise
  82. * @ops: The operations to use to handle the slow work item
  83. *
  84. * Initialise a delayed slow work item.
  85. */
  86. static inline void delayed_slow_work_init(struct delayed_slow_work *dwork,
  87. const struct slow_work_ops *ops)
  88. {
  89. init_timer(&dwork->timer);
  90. slow_work_init(&dwork->work, ops);
  91. }
  92. /**
  93. * vslow_work_init - Initialise a very slow work item
  94. * @work: The work item to initialise
  95. * @ops: The operations to use to handle the slow work item
  96. *
  97. * Initialise a very slow work item. This item will be restricted such that
  98. * only a certain number of the pool threads will be able to execute items of
  99. * this type.
  100. */
  101. static inline void vslow_work_init(struct slow_work *work,
  102. const struct slow_work_ops *ops)
  103. {
  104. work->flags = 1 << SLOW_WORK_VERY_SLOW;
  105. work->ops = ops;
  106. INIT_LIST_HEAD(&work->link);
  107. }
  108. extern int slow_work_enqueue(struct slow_work *work);
  109. extern void slow_work_cancel(struct slow_work *work);
  110. extern int slow_work_register_user(struct module *owner);
  111. extern void slow_work_unregister_user(struct module *owner);
  112. extern int delayed_slow_work_enqueue(struct delayed_slow_work *dwork,
  113. unsigned long delay);
  114. static inline void delayed_slow_work_cancel(struct delayed_slow_work *dwork)
  115. {
  116. slow_work_cancel(&dwork->work);
  117. }
  118. #ifdef CONFIG_SYSCTL
  119. extern ctl_table slow_work_sysctls[];
  120. #endif
  121. #endif /* CONFIG_SLOW_WORK */
  122. #endif /* _LINUX_SLOW_WORK_H */