BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file BLockReactor.c |
||
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 | |||
30 | #include <misc/debug.h> |
||
31 | #include <misc/offset.h> |
||
32 | #include <base/BLog.h> |
||
33 | |||
34 | #include "BLockReactor.h" |
||
35 | |||
36 | #include <generated/blog_channel_BLockReactor.h> |
||
37 | |||
38 | static void thread_signal_handler (BThreadSignal *thread_signal) |
||
39 | { |
||
40 | BLockReactor *o = UPPER_OBJECT(thread_signal, BLockReactor, thread_signal); |
||
41 | DebugObject_Access(&o->d_obj); |
||
42 | |||
43 | ASSERT_FORCE(sem_post(&o->sem1) == 0) |
||
44 | ASSERT_FORCE(sem_wait(&o->sem2) == 0) |
||
45 | } |
||
46 | |||
47 | int BLockReactor_Init (BLockReactor *o, BReactor *reactor) |
||
48 | { |
||
49 | o->reactor = reactor; |
||
50 | |||
51 | if (!BThreadSignal_Init(&o->thread_signal, reactor, thread_signal_handler)) { |
||
52 | BLog(BLOG_ERROR, "BThreadSignal_Init failed"); |
||
53 | goto fail0; |
||
54 | } |
||
55 | |||
56 | if (sem_init(&o->sem1, 0, 0) < 0) { |
||
57 | BLog(BLOG_ERROR, "sem_init failed"); |
||
58 | goto fail1; |
||
59 | } |
||
60 | |||
61 | if (sem_init(&o->sem2, 0, 0) < 0) { |
||
62 | BLog(BLOG_ERROR, "sem_init failed"); |
||
63 | goto fail2; |
||
64 | } |
||
65 | |||
66 | #ifndef NDEBUG |
||
67 | o->locked = 0; |
||
68 | #endif |
||
69 | |||
70 | DebugObject_Init(&o->d_obj); |
||
71 | return 1; |
||
72 | |||
73 | fail2: |
||
74 | if (sem_close(&o->sem1) < 0) { |
||
75 | BLog(BLOG_ERROR, "sem_close failed"); |
||
76 | } |
||
77 | fail1: |
||
78 | BThreadSignal_Free(&o->thread_signal); |
||
79 | fail0: |
||
80 | return 0; |
||
81 | } |
||
82 | |||
83 | void BLockReactor_Free (BLockReactor *o) |
||
84 | { |
||
85 | DebugObject_Free(&o->d_obj); |
||
86 | #ifndef NDEBUG |
||
87 | ASSERT(!o->locked) |
||
88 | #endif |
||
89 | |||
90 | if (sem_destroy(&o->sem2) < 0) { |
||
91 | BLog(BLOG_ERROR, "sem_close failed"); |
||
92 | } |
||
93 | if (sem_destroy(&o->sem1) < 0) { |
||
94 | BLog(BLOG_ERROR, "sem_close failed"); |
||
95 | } |
||
96 | BThreadSignal_Free(&o->thread_signal); |
||
97 | } |
||
98 | |||
99 | int BLockReactor_Thread_Lock (BLockReactor *o) |
||
100 | { |
||
101 | DebugObject_Access(&o->d_obj); |
||
102 | #ifndef NDEBUG |
||
103 | ASSERT(!o->locked) |
||
104 | #endif |
||
105 | |||
106 | if (!BThreadSignal_Thread_Signal(&o->thread_signal)) { |
||
107 | return 0; |
||
108 | } |
||
109 | |||
110 | ASSERT_FORCE(sem_wait(&o->sem1) == 0) |
||
111 | |||
112 | #ifndef NDEBUG |
||
113 | o->locked = 1; |
||
114 | #endif |
||
115 | |||
116 | return 1; |
||
117 | } |
||
118 | |||
119 | void BLockReactor_Thread_Unlock (BLockReactor *o) |
||
120 | { |
||
121 | DebugObject_Access(&o->d_obj); |
||
122 | #ifndef NDEBUG |
||
123 | ASSERT(o->locked) |
||
124 | #endif |
||
125 | |||
126 | #ifndef NDEBUG |
||
127 | o->locked = 0; |
||
128 | #endif |
||
129 | |||
130 | ASSERT_FORCE(sem_post(&o->sem2) == 0) |
||
131 | } |