BThreadWork.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @file BThreadWork.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * System for performing computations (possibly) in parallel with the event loop
  25. * in a different thread.
  26. */
  27. #ifndef BADVPN_BTHREADWORK_BTHREADWORK_H
  28. #define BADVPN_BTHREADWORK_BTHREADWORK_H
  29. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  30. #include <pthread.h>
  31. #include <semaphore.h>
  32. #endif
  33. #include <misc/debug.h>
  34. #include <structure/LinkedList2.h>
  35. #include <system/DebugObject.h>
  36. #include <system/BReactor.h>
  37. #define BTHREADWORK_STATE_PENDING 1
  38. #define BTHREADWORK_STATE_RUNNING 2
  39. #define BTHREADWORK_STATE_FINISHED 3
  40. #define BTHREADWORK_STATE_FORGOTTEN 4
  41. #define BTHREADWORK_MAX_THREADS 8
  42. struct BThreadWork_s;
  43. struct BThreadWorkDispatcher_s;
  44. /**
  45. * Function called to do the work for a {@link BThreadWork}.
  46. * The function may be called in another thread, in parallel with the event loop.
  47. *
  48. * @param user as work_func_user in {@link BThreadWork_Init}
  49. */
  50. typedef void (*BThreadWork_work_func) (void *user);
  51. /**
  52. * Handler called when a {@link BThreadWork} work is done.
  53. *
  54. * @param user as in {@link BThreadWork_Init}
  55. */
  56. typedef void (*BThreadWork_handler_done) (void *user);
  57. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  58. struct BThreadWorkDispatcher_thread {
  59. struct BThreadWorkDispatcher_s *d;
  60. struct BThreadWork_s *running_work;
  61. pthread_cond_t new_cond;
  62. pthread_t thread;
  63. };
  64. #endif
  65. typedef struct BThreadWorkDispatcher_s {
  66. BReactor *reactor;
  67. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  68. LinkedList2 pending_list;
  69. LinkedList2 finished_list;
  70. pthread_mutex_t mutex;
  71. int pipe[2];
  72. BFileDescriptor bfd;
  73. BPending more_job;
  74. int cancel;
  75. int num_threads;
  76. struct BThreadWorkDispatcher_thread threads[BTHREADWORK_MAX_THREADS];
  77. #endif
  78. DebugObject d_obj;
  79. DebugCounter d_ctr;
  80. } BThreadWorkDispatcher;
  81. typedef struct BThreadWork_s {
  82. BThreadWorkDispatcher *d;
  83. BThreadWork_handler_done handler_done;
  84. void *user;
  85. BThreadWork_work_func work_func;
  86. void *work_func_user;
  87. union {
  88. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  89. struct {
  90. LinkedList2Node list_node;
  91. int state;
  92. sem_t finished_sem;
  93. };
  94. #endif
  95. struct {
  96. BPending job;
  97. };
  98. };
  99. DebugObject d_obj;
  100. } BThreadWork;
  101. /**
  102. * Initializes the work dispatcher.
  103. * Works may be started using {@link BThreadWork_Init}.
  104. *
  105. * @param o the object
  106. * @param reactor reactor we live in
  107. * @param num_threads_hint hint for the number of threads to use:
  108. * <0 - A choice will be made automatically, probably based on the number of CPUs.
  109. * 0 - No additional threads will be used, and computations will be performed directly
  110. * in the event loop in job handlers.
  111. * @return 1 on success, 0 on failure
  112. */
  113. int BThreadWorkDispatcher_Init (BThreadWorkDispatcher *o, BReactor *reactor, int num_threads_hint) WARN_UNUSED;
  114. /**
  115. * Frees the work dispatcher.
  116. * There must be no {@link BThreadWork}'s with this dispatcher.
  117. *
  118. * @param o the object
  119. */
  120. void BThreadWorkDispatcher_Free (BThreadWorkDispatcher *o);
  121. /**
  122. * Determines whether threads are being used for computations, or computations
  123. * are done in the event loop.
  124. *
  125. * @return 1 if threads are being used, 0 if not
  126. */
  127. int BThreadWorkDispatcher_UsingThreads (BThreadWorkDispatcher *o);
  128. /**
  129. * Initializes the work.
  130. *
  131. * @param o the object
  132. * @param d work dispatcher
  133. * @param handler_done handler to call when the work is done
  134. * @param user argument to handler
  135. * @param work_func function that will do the work, possibly from another thread
  136. * @param work_func_user argument to work_func
  137. */
  138. void BThreadWork_Init (BThreadWork *o, BThreadWorkDispatcher *d, BThreadWork_handler_done handler_done, void *user, BThreadWork_work_func work_func, void *work_func_user);
  139. /**
  140. * Frees the work.
  141. * After this function returns, the work function will either have fully executed,
  142. * or not called at all, and never will be.
  143. *
  144. * @param o the object
  145. */
  146. void BThreadWork_Free (BThreadWork *o);
  147. #endif