BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 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  
35 #ifndef BADVPN_BTHREADWORK_BTHREADWORK_H
36 #define BADVPN_BTHREADWORK_BTHREADWORK_H
37  
38 #ifdef BADVPN_THREADWORK_USE_PTHREAD
39 #include <pthread.h>
40 #include <semaphore.h>
41 #endif
42  
43 #include <misc/debug.h>
44 #include <structure/LinkedList1.h>
45 #include <base/DebugObject.h>
46 #include <system/BReactor.h>
47  
48 #define BTHREADWORK_STATE_PENDING 1
49 #define BTHREADWORK_STATE_RUNNING 2
50 #define BTHREADWORK_STATE_FINISHED 3
51 #define BTHREADWORK_STATE_FORGOTTEN 4
52  
53 #define BTHREADWORK_MAX_THREADS 8
54  
55 struct BThreadWork_s;
56 struct BThreadWorkDispatcher_s;
57  
58 /**
59 * Function called to do the work for a {@link BThreadWork}.
60 * The function may be called in another thread, in parallel with the event loop.
61 *
62 * @param user as work_func_user in {@link BThreadWork_Init}
63 */
64 typedef void (*BThreadWork_work_func) (void *user);
65  
66 /**
67 * Handler called when a {@link BThreadWork} work is done.
68 *
69 * @param user as in {@link BThreadWork_Init}
70 */
71 typedef void (*BThreadWork_handler_done) (void *user);
72  
73 #ifdef BADVPN_THREADWORK_USE_PTHREAD
74 struct BThreadWorkDispatcher_thread {
75 struct BThreadWorkDispatcher_s *d;
76 struct BThreadWork_s *running_work;
77 pthread_cond_t new_cond;
78 pthread_t thread;
79 };
80 #endif
81  
82 typedef struct BThreadWorkDispatcher_s {
83 BReactor *reactor;
84 #ifdef BADVPN_THREADWORK_USE_PTHREAD
85 LinkedList1 pending_list;
86 LinkedList1 finished_list;
87 pthread_mutex_t mutex;
88 int pipe[2];
89 BFileDescriptor bfd;
90 BPending more_job;
91 int cancel;
92 int num_threads;
93 struct BThreadWorkDispatcher_thread threads[BTHREADWORK_MAX_THREADS];
94 #endif
95 DebugObject d_obj;
96 DebugCounter d_ctr;
97 } BThreadWorkDispatcher;
98  
99 typedef struct BThreadWork_s {
100 BThreadWorkDispatcher *d;
101 BThreadWork_handler_done handler_done;
102 void *user;
103 BThreadWork_work_func work_func;
104 void *work_func_user;
105 union {
106 #ifdef BADVPN_THREADWORK_USE_PTHREAD
107 struct {
108 LinkedList1Node list_node;
109 int state;
110 sem_t finished_sem;
111 };
112 #endif
113 struct {
114 BPending job;
115 };
116 };
117 DebugObject d_obj;
118 } BThreadWork;
119  
120 /**
121 * Initializes the work dispatcher.
122 * Works may be started using {@link BThreadWork_Init}.
123 *
124 * @param o the object
125 * @param reactor reactor we live in
126 * @param num_threads_hint hint for the number of threads to use:
127 * <0 - A choice will be made automatically, probably based on the number of CPUs.
128 * 0 - No additional threads will be used, and computations will be performed directly
129 * in the event loop in job handlers.
130 * @return 1 on success, 0 on failure
131 */
132 int BThreadWorkDispatcher_Init (BThreadWorkDispatcher *o, BReactor *reactor, int num_threads_hint) WARN_UNUSED;
133  
134 /**
135 * Frees the work dispatcher.
136 * There must be no {@link BThreadWork}'s with this dispatcher.
137 *
138 * @param o the object
139 */
140 void BThreadWorkDispatcher_Free (BThreadWorkDispatcher *o);
141  
142 /**
143 * Determines whether threads are being used for computations, or computations
144 * are done in the event loop.
145 *
146 * @return 1 if threads are being used, 0 if not
147 */
148 int BThreadWorkDispatcher_UsingThreads (BThreadWorkDispatcher *o);
149  
150 /**
151 * Initializes the work.
152 *
153 * @param o the object
154 * @param d work dispatcher
155 * @param handler_done handler to call when the work is done
156 * @param user argument to handler
157 * @param work_func function that will do the work, possibly from another thread
158 * @param work_func_user argument to work_func
159 */
160 void BThreadWork_Init (BThreadWork *o, BThreadWorkDispatcher *d, BThreadWork_handler_done handler_done, void *user, BThreadWork_work_func work_func, void *work_func_user);
161  
162 /**
163 * Frees the work.
164 * After this function returns, the work function will either have fully executed,
165 * or not called at all, and never will be.
166 *
167 * @param o the object
168 */
169 void BThreadWork_Free (BThreadWork *o);
170  
171 #endif