BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file BPending.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 <stddef.h>
31  
32 #include <misc/debug.h>
33 #include <misc/offset.h>
34  
35 #include "BPending.h"
36  
37 #include "BPending_list.h"
38 #include <structure/SLinkedList_impl.h>
39  
40 void BPendingGroup_Init (BPendingGroup *g)
41 {
42 // init jobs list
43 BPending__List_Init(&g->jobs);
44  
45 // init pending counter
46 DebugCounter_Init(&g->pending_ctr);
47  
48 // init debug object
49 DebugObject_Init(&g->d_obj);
50 }
51  
52 void BPendingGroup_Free (BPendingGroup *g)
53 {
54 DebugCounter_Free(&g->pending_ctr);
55 ASSERT(BPending__List_IsEmpty(&g->jobs))
56 DebugObject_Free(&g->d_obj);
57 }
58  
59 int BPendingGroup_HasJobs (BPendingGroup *g)
60 {
61 DebugObject_Access(&g->d_obj);
62  
63 return !BPending__List_IsEmpty(&g->jobs);
64 }
65  
66 void BPendingGroup_ExecuteJob (BPendingGroup *g)
67 {
68 ASSERT(!BPending__List_IsEmpty(&g->jobs))
69 DebugObject_Access(&g->d_obj);
70  
71 // get a job
72 BSmallPending *p = BPending__List_First(&g->jobs);
73 ASSERT(!BPending__ListIsRemoved(p))
74 ASSERT(p->pending)
75  
76 // remove from jobs list
77 BPending__List_RemoveFirst(&g->jobs);
78  
79 // set not pending
80 BPending__ListMarkRemoved(p);
81 #ifndef NDEBUG
82 p->pending = 0;
83 #endif
84  
85 // execute job
86 p->handler(p->user);
87 return;
88 }
89  
90 BSmallPending * BPendingGroup_PeekJob (BPendingGroup *g)
91 {
92 DebugObject_Access(&g->d_obj);
93  
94 return BPending__List_First(&g->jobs);
95 }
96  
97 void BSmallPending_Init (BSmallPending *o, BPendingGroup *g, BSmallPending_handler handler, void *user)
98 {
99 // init arguments
100 o->handler = handler;
101 o->user = user;
102  
103 // set not pending
104 BPending__ListMarkRemoved(o);
105 #ifndef NDEBUG
106 o->pending = 0;
107 #endif
108  
109 // increment pending counter
110 DebugCounter_Increment(&g->pending_ctr);
111  
112 // init debug object
113 DebugObject_Init(&o->d_obj);
114 }
115  
116 void BSmallPending_Free (BSmallPending *o, BPendingGroup *g)
117 {
118 DebugCounter_Decrement(&g->pending_ctr);
119 DebugObject_Free(&o->d_obj);
120 ASSERT(o->pending == !BPending__ListIsRemoved(o))
121  
122 // remove from jobs list
123 if (!BPending__ListIsRemoved(o)) {
124 BPending__List_Remove(&g->jobs, o);
125 }
126 }
127  
128 void BSmallPending_SetHandler (BSmallPending *o, BSmallPending_handler handler, void *user)
129 {
130 DebugObject_Access(&o->d_obj);
131  
132 // set handler
133 o->handler = handler;
134 o->user = user;
135 }
136  
137 void BSmallPending_Set (BSmallPending *o, BPendingGroup *g)
138 {
139 DebugObject_Access(&o->d_obj);
140 ASSERT(o->pending == !BPending__ListIsRemoved(o))
141  
142 // remove from jobs list
143 if (!BPending__ListIsRemoved(o)) {
144 BPending__List_Remove(&g->jobs, o);
145 }
146  
147 // insert to jobs list
148 BPending__List_Prepend(&g->jobs, o);
149  
150 // set pending
151 #ifndef NDEBUG
152 o->pending = 1;
153 #endif
154 }
155  
156 void BSmallPending_Unset (BSmallPending *o, BPendingGroup *g)
157 {
158 DebugObject_Access(&o->d_obj);
159 ASSERT(o->pending == !BPending__ListIsRemoved(o))
160  
161 if (!BPending__ListIsRemoved(o)) {
162 // remove from jobs list
163 BPending__List_Remove(&g->jobs, o);
164  
165 // set not pending
166 BPending__ListMarkRemoved(o);
167 #ifndef NDEBUG
168 o->pending = 0;
169 #endif
170 }
171 }
172  
173 int BSmallPending_IsSet (BSmallPending *o)
174 {
175 DebugObject_Access(&o->d_obj);
176 ASSERT(o->pending == !BPending__ListIsRemoved(o))
177  
178 return !BPending__ListIsRemoved(o);
179 }
180  
181 void BPending_Init (BPending *o, BPendingGroup *g, BPending_handler handler, void *user)
182 {
183 BSmallPending_Init(&o->base, g, handler, user);
184 o->g = g;
185 }
186  
187 void BPending_Free (BPending *o)
188 {
189 BSmallPending_Free(&o->base, o->g);
190 }
191  
192 void BPending_Set (BPending *o)
193 {
194 BSmallPending_Set(&o->base, o->g);
195 }
196  
197 void BPending_Unset (BPending *o)
198 {
199 BSmallPending_Unset(&o->base, o->g);
200 }
201  
202 int BPending_IsSet (BPending *o)
203 {
204 return BSmallPending_IsSet(&o->base);
205 }