BThreadWork.h 4.2 KB

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