BThreadWork.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @file BThreadWork.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * System for performing computations (possibly) in parallel with the event loop
  32. * in a different thread.
  33. */
  34. #ifndef BADVPN_BTHREADWORK_BTHREADWORK_H
  35. #define BADVPN_BTHREADWORK_BTHREADWORK_H
  36. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  37. #include <pthread.h>
  38. #include <semaphore.h>
  39. #endif
  40. #include <misc/debug.h>
  41. #include <structure/LinkedList1.h>
  42. #include <base/DebugObject.h>
  43. #include <system/BReactor.h>
  44. #define BTHREADWORK_STATE_PENDING 1
  45. #define BTHREADWORK_STATE_RUNNING 2
  46. #define BTHREADWORK_STATE_FINISHED 3
  47. #define BTHREADWORK_STATE_FORGOTTEN 4
  48. #define BTHREADWORK_MAX_THREADS 8
  49. struct BThreadWork_s;
  50. struct BThreadWorkDispatcher_s;
  51. /**
  52. * Function called to do the work for a {@link BThreadWork}.
  53. * The function may be called in another thread, in parallel with the event loop.
  54. *
  55. * @param user as work_func_user in {@link BThreadWork_Init}
  56. */
  57. typedef void (*BThreadWork_work_func) (void *user);
  58. /**
  59. * Handler called when a {@link BThreadWork} work is done.
  60. *
  61. * @param user as in {@link BThreadWork_Init}
  62. */
  63. typedef void (*BThreadWork_handler_done) (void *user);
  64. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  65. struct BThreadWorkDispatcher_thread {
  66. struct BThreadWorkDispatcher_s *d;
  67. struct BThreadWork_s *running_work;
  68. pthread_cond_t new_cond;
  69. pthread_t thread;
  70. };
  71. #endif
  72. typedef struct BThreadWorkDispatcher_s {
  73. BReactor *reactor;
  74. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  75. LinkedList1 pending_list;
  76. LinkedList1 finished_list;
  77. pthread_mutex_t mutex;
  78. int pipe[2];
  79. BFileDescriptor bfd;
  80. BPending more_job;
  81. int cancel;
  82. int num_threads;
  83. struct BThreadWorkDispatcher_thread threads[BTHREADWORK_MAX_THREADS];
  84. #endif
  85. DebugObject d_obj;
  86. DebugCounter d_ctr;
  87. } BThreadWorkDispatcher;
  88. typedef struct BThreadWork_s {
  89. BThreadWorkDispatcher *d;
  90. BThreadWork_handler_done handler_done;
  91. void *user;
  92. BThreadWork_work_func work_func;
  93. void *work_func_user;
  94. union {
  95. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  96. struct {
  97. LinkedList1Node list_node;
  98. int state;
  99. sem_t finished_sem;
  100. };
  101. #endif
  102. struct {
  103. BPending job;
  104. };
  105. };
  106. DebugObject d_obj;
  107. } BThreadWork;
  108. /**
  109. * Initializes the work dispatcher.
  110. * Works may be started using {@link BThreadWork_Init}.
  111. *
  112. * @param o the object
  113. * @param reactor reactor we live in
  114. * @param num_threads_hint hint for the number of threads to use:
  115. * <0 - A choice will be made automatically, probably based on the number of CPUs.
  116. * 0 - No additional threads will be used, and computations will be performed directly
  117. * in the event loop in job handlers.
  118. * @return 1 on success, 0 on failure
  119. */
  120. int BThreadWorkDispatcher_Init (BThreadWorkDispatcher *o, BReactor *reactor, int num_threads_hint) WARN_UNUSED;
  121. /**
  122. * Frees the work dispatcher.
  123. * There must be no {@link BThreadWork}'s with this dispatcher.
  124. *
  125. * @param o the object
  126. */
  127. void BThreadWorkDispatcher_Free (BThreadWorkDispatcher *o);
  128. /**
  129. * Determines whether threads are being used for computations, or computations
  130. * are done in the event loop.
  131. *
  132. * @return 1 if threads are being used, 0 if not
  133. */
  134. int BThreadWorkDispatcher_UsingThreads (BThreadWorkDispatcher *o);
  135. /**
  136. * Initializes the work.
  137. *
  138. * @param o the object
  139. * @param d work dispatcher
  140. * @param handler_done handler to call when the work is done
  141. * @param user argument to handler
  142. * @param work_func function that will do the work, possibly from another thread
  143. * @param work_func_user argument to work_func
  144. */
  145. void BThreadWork_Init (BThreadWork *o, BThreadWorkDispatcher *d, BThreadWork_handler_done handler_done, void *user, BThreadWork_work_func work_func, void *work_func_user);
  146. /**
  147. * Frees the work.
  148. * After this function returns, the work function will either have fully executed,
  149. * or not called at all, and never will be.
  150. *
  151. * @param o the object
  152. */
  153. void BThreadWork_Free (BThreadWork *o);
  154. #endif