BThreadWork.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. struct BThreadWork_s;
  42. /**
  43. * Function called to do the work for a {@link BThreadWork}.
  44. * The function may be called in another thread, in parallel with the event loop.
  45. *
  46. * @param user as work_func_user in {@link BThreadWork_Init}
  47. */
  48. typedef void (*BThreadWork_work_func) (void *user);
  49. /**
  50. * Handler called when a {@link BThreadWork} work is done.
  51. *
  52. * @param user as in {@link BThreadWork_Init}
  53. */
  54. typedef void (*BThreadWork_handler_done) (void *user);
  55. typedef struct {
  56. BReactor *reactor;
  57. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  58. int num_threads;
  59. LinkedList2 pending_list;
  60. struct BThreadWork_s *running_work;
  61. LinkedList2 finished_list;
  62. pthread_mutex_t mutex;
  63. pthread_cond_t new_cond;
  64. int pipe[2];
  65. BFileDescriptor bfd;
  66. BPending more_job;
  67. int cancel;
  68. pthread_t thread;
  69. #endif
  70. DebugObject d_obj;
  71. DebugCounter d_ctr;
  72. } BThreadWorkDispatcher;
  73. typedef struct BThreadWork_s {
  74. BThreadWorkDispatcher *d;
  75. BThreadWork_handler_done handler_done;
  76. void *user;
  77. BThreadWork_work_func work_func;
  78. void *work_func_user;
  79. union {
  80. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  81. struct {
  82. LinkedList2Node list_node;
  83. int state;
  84. sem_t finished_sem;
  85. };
  86. #endif
  87. struct {
  88. BPending job;
  89. };
  90. };
  91. DebugObject d_obj;
  92. } BThreadWork;
  93. /**
  94. * Initializes the work dispatcher.
  95. * Works may be started using {@link BThreadWork_Init}.
  96. *
  97. * @param o the object
  98. * @param reactor reactor we live in
  99. * @param num_threads_hint hint for the number of threads to use:
  100. * <0 - A choice will be made automatically, probably based on the number of CPUs.
  101. * 0 - No additional threads will be used, and computations will be performed directly
  102. * in the event loop in job handlers.
  103. * @return 1 on success, 0 on failure
  104. */
  105. int BThreadWorkDispatcher_Init (BThreadWorkDispatcher *o, BReactor *reactor, int num_threads_hint) WARN_UNUSED;
  106. /**
  107. * Frees the work dispatcher.
  108. * There must be no {@link BThreadWork}'s with this dispatcher.
  109. *
  110. * @param o the object
  111. */
  112. void BThreadWorkDispatcher_Free (BThreadWorkDispatcher *o);
  113. /**
  114. * Determines whether threads are being used for computations, or computations
  115. * are done in the event loop.
  116. *
  117. * @return 1 if threads are being used, 0 if not
  118. */
  119. int BThreadWorkDispatcher_UsingThreads (BThreadWorkDispatcher *o);
  120. /**
  121. * Initializes the work.
  122. *
  123. * @param o the object
  124. * @param d work dispatcher
  125. * @param handler_done handler to call when the work is done
  126. * @param user argument to handler
  127. * @param work_func function that will do the work, possibly from another thread
  128. * @param work_func_user argument to work_func
  129. */
  130. void BThreadWork_Init (BThreadWork *o, BThreadWorkDispatcher *d, BThreadWork_handler_done handler_done, void *user, BThreadWork_work_func work_func, void *work_func_user);
  131. /**
  132. * Frees the work.
  133. * After this function returns, the work function will either have fully executed,
  134. * or not called at all, and never will be.
  135. *
  136. * @param o the object
  137. */
  138. void BThreadWork_Free (BThreadWork *o);
  139. #endif