BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file BEventLock.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/offset.h>
31  
32 #include "BEventLock.h"
33  
34 static void exec_job_handler (BEventLock *o)
35 {
36 ASSERT(!LinkedList1_IsEmpty(&o->jobs))
37 DebugObject_Access(&o->d_obj);
38  
39 // get job
40 BEventLockJob *j = UPPER_OBJECT(LinkedList1_GetFirst(&o->jobs), BEventLockJob, pending_node);
41 ASSERT(j->pending)
42  
43 // call handler
44 j->handler(j->user);
45 return;
46 }
47  
48 void BEventLock_Init (BEventLock *o, BPendingGroup *pg)
49 {
50 // init jobs list
51 LinkedList1_Init(&o->jobs);
52  
53 // init exec job
54 BPending_Init(&o->exec_job, pg, (BPending_handler)exec_job_handler, o);
55  
56 DebugObject_Init(&o->d_obj);
57 DebugCounter_Init(&o->pending_ctr);
58 }
59  
60 void BEventLock_Free (BEventLock *o)
61 {
62 ASSERT(LinkedList1_IsEmpty(&o->jobs))
63 DebugCounter_Free(&o->pending_ctr);
64 DebugObject_Free(&o->d_obj);
65  
66 // free exec jobs
67 BPending_Free(&o->exec_job);
68 }
69  
70 void BEventLockJob_Init (BEventLockJob *o, BEventLock *l, BEventLock_handler handler, void *user)
71 {
72 // init arguments
73 o->l = l;
74 o->handler = handler;
75 o->user = user;
76  
77 // set not pending
78 o->pending = 0;
79  
80 DebugObject_Init(&o->d_obj);
81 DebugCounter_Increment(&l->pending_ctr);
82 }
83  
84 void BEventLockJob_Free (BEventLockJob *o)
85 {
86 BEventLock *l = o->l;
87  
88 DebugCounter_Decrement(&l->pending_ctr);
89 DebugObject_Free(&o->d_obj);
90  
91 if (o->pending) {
92 int was_first = (&o->pending_node == LinkedList1_GetFirst(&l->jobs));
93  
94 // remove from jobs list
95 LinkedList1_Remove(&l->jobs, &o->pending_node);
96  
97 // schedule/unschedule job
98 if (was_first) {
99 if (LinkedList1_IsEmpty(&l->jobs)) {
100 BPending_Unset(&l->exec_job);
101 } else {
102 BPending_Set(&l->exec_job);
103 }
104 }
105 }
106 }
107  
108 void BEventLockJob_Wait (BEventLockJob *o)
109 {
110 BEventLock *l = o->l;
111 ASSERT(!o->pending)
112  
113 // append to jobs
114 LinkedList1_Append(&l->jobs, &o->pending_node);
115  
116 // set pending
117 o->pending = 1;
118  
119 // schedule next job if needed
120 if (&o->pending_node == LinkedList1_GetFirst(&l->jobs)) {
121 BPending_Set(&l->exec_job);
122 }
123 }
124  
125 void BEventLockJob_Release (BEventLockJob *o)
126 {
127 BEventLock *l = o->l;
128 ASSERT(o->pending)
129  
130 int was_first = (&o->pending_node == LinkedList1_GetFirst(&l->jobs));
131  
132 // remove from jobs list
133 LinkedList1_Remove(&l->jobs, &o->pending_node);
134  
135 // set not pending
136 o->pending = 0;
137  
138 // schedule/unschedule job
139 if (was_first) {
140 if (LinkedList1_IsEmpty(&l->jobs)) {
141 BPending_Unset(&l->exec_job);
142 } else {
143 BPending_Set(&l->exec_job);
144 }
145 }
146 }