~posulliv/drizzle/optimizer-style-cleanup

« back to all changes in this revision

Viewing changes to plugin/pbxt/src/linklist_xt.cc

  • Committer: Padraig O'Sullivan
  • Date: 2010-04-17 01:38:47 UTC
  • mfrom: (1237.9.238 bad-staging)
  • Revision ID: osullivan.padraig@gmail.com-20100417013847-ibjioqsfbmf5yg4g
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2005 PrimeBase Technologies GmbH
 
2
 *
 
3
 * PrimeBase XT
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 *
 
19
 * 2005-02-03   Paul McCullagh
 
20
 *
 
21
 * H&G2JCtL
 
22
 */
 
23
 
 
24
#include "xt_config.h"
 
25
 
 
26
#include "pthread_xt.h"
 
27
#include "linklist_xt.h"
 
28
#include "thread_xt.h"
 
29
#include "memory_xt.h"
 
30
 
 
31
xtPublic XTLinkedListPtr xt_new_linkedlist(struct XTThread *self, void *thunk, XTFreeFunc free_func, xtBool with_lock)
 
32
{
 
33
        XTLinkedListPtr ll;
 
34
 
 
35
        ll = (XTLinkedListPtr) xt_calloc(self, sizeof(XTLinkedListRec));
 
36
        try_(a) {
 
37
                if (with_lock) {
 
38
                        ll->ll_lock = (xt_mutex_type *) xt_calloc(self, sizeof(xt_mutex_type));
 
39
                        try_(b) {
 
40
                                xt_init_mutex_with_autoname(self, ll->ll_lock);
 
41
                        }
 
42
                        catch_(b) {
 
43
                                xt_free(self, ll->ll_lock);
 
44
                                ll->ll_lock = NULL;
 
45
                                throw_();
 
46
                        }
 
47
                        cont_(b);
 
48
                        ll->ll_cond = (xt_cond_type *) xt_calloc(self, sizeof(xt_cond_type));
 
49
                        try_(c) {
 
50
                                xt_init_cond(self, ll->ll_cond);
 
51
                        }
 
52
                        catch_(c) {
 
53
                                xt_free(self, ll->ll_cond);
 
54
                                ll->ll_cond = NULL;
 
55
                                throw_();
 
56
                        }
 
57
                        cont_(c);
 
58
                }
 
59
                ll->ll_thunk = thunk;
 
60
                ll->ll_free_func = free_func;
 
61
        }
 
62
        catch_(a) {
 
63
                xt_free_linkedlist(self, ll);
 
64
                throw_();
 
65
        }
 
66
        cont_(a);
 
67
        return ll;
 
68
}
 
69
 
 
70
xtPublic void xt_free_linkedlist(XTThreadPtr self, XTLinkedListPtr ll)
 
71
{
 
72
        if (ll->ll_lock)
 
73
                xt_lock_mutex(self, ll->ll_lock);
 
74
        while (ll->ll_items)
 
75
                xt_ll_remove(self, ll, ll->ll_items, FALSE);
 
76
        if (ll->ll_lock)
 
77
                xt_unlock_mutex(self, ll->ll_lock);
 
78
        if (ll->ll_lock) {
 
79
                xt_free_mutex(ll->ll_lock);
 
80
                xt_free(self, ll->ll_lock);
 
81
        }
 
82
        if (ll->ll_cond) {
 
83
                xt_free_cond(ll->ll_cond);
 
84
                xt_free(self, ll->ll_cond);
 
85
        }
 
86
        xt_free(self, ll);
 
87
}
 
88
 
 
89
xtPublic void xt_ll_add(XTThreadPtr self, XTLinkedListPtr ll, XTLinkedItemPtr li, xtBool lock)
 
90
{
 
91
        if (lock && ll->ll_lock)
 
92
                xt_lock_mutex(self, ll->ll_lock);
 
93
        li->li_next = ll->ll_items;
 
94
        li->li_prev = NULL;
 
95
        if (ll->ll_items)
 
96
                ll->ll_items->li_prev = li;
 
97
        ll->ll_items = li;
 
98
        ll->ll_item_count++;
 
99
        if (lock && ll->ll_lock)
 
100
                xt_unlock_mutex(self, ll->ll_lock);
 
101
}
 
102
 
 
103
xtPublic XTLinkedItemPtr xt_ll_first_item(XTThreadPtr XT_UNUSED(self), XTLinkedListPtr ll)
 
104
{
 
105
        return ll ? ll->ll_items : NULL;
 
106
}
 
107
 
 
108
xtPublic XTLinkedItemPtr xt_ll_next_item(XTThreadPtr XT_UNUSED(self), XTLinkedItemPtr item)
 
109
{
 
110
        return item->li_next;
 
111
}
 
112
 
 
113
xtPublic xtBool xt_ll_exists(XTThreadPtr self, XTLinkedListPtr ll, XTLinkedItemPtr li, xtBool lock)
 
114
{
 
115
        XTLinkedItemPtr ptr;
 
116
 
 
117
        if (lock && ll->ll_lock)
 
118
                xt_lock_mutex(self, ll->ll_lock);
 
119
 
 
120
        ptr = ll->ll_items;
 
121
        
 
122
        for (ptr = ll->ll_items; ptr && (ptr != li); ptr = ptr->li_next){}
 
123
        
 
124
        if (lock && ll->ll_lock)
 
125
                xt_unlock_mutex(self, ll->ll_lock);
 
126
                
 
127
        return (ptr == li);
 
128
}
 
129
 
 
130
xtPublic void xt_ll_remove(XTThreadPtr self, XTLinkedListPtr ll, XTLinkedItemPtr li, xtBool lock)
 
131
{
 
132
        if (lock && ll->ll_lock)
 
133
                xt_lock_mutex(self, ll->ll_lock);
 
134
 
 
135
        /* Move front pointer: */
 
136
        if (ll->ll_items == li)
 
137
                ll->ll_items = li->li_next;
 
138
 
 
139
        /* Remove from list: */
 
140
        if (li->li_prev)
 
141
                li->li_prev->li_next = li->li_next;
 
142
        if (li->li_next)
 
143
                li->li_next->li_prev = li->li_prev;
 
144
 
 
145
        ll->ll_item_count--;
 
146
        if (ll->ll_free_func)
 
147
                (*ll->ll_free_func)(self, ll->ll_thunk, li);
 
148
 
 
149
        /* Signal one less: */
 
150
        if (ll->ll_cond)
 
151
                xt_signal_cond(self, ll->ll_cond);
 
152
 
 
153
        if (lock && ll->ll_lock)
 
154
                xt_unlock_mutex(self, ll->ll_lock);
 
155
}
 
156
 
 
157
xtPublic void xt_ll_lock(XTThreadPtr self, XTLinkedListPtr ll)
 
158
{
 
159
        if (ll->ll_lock)
 
160
                xt_lock_mutex(self, ll->ll_lock);
 
161
}
 
162
 
 
163
xtPublic void xt_ll_unlock(XTThreadPtr self, XTLinkedListPtr ll)
 
164
{
 
165
        if (ll->ll_lock)
 
166
                xt_unlock_mutex(self, ll->ll_lock);
 
167
}
 
168
 
 
169
xtPublic void xt_ll_wait_till_empty(XTThreadPtr self, XTLinkedListPtr ll)
 
170
{
 
171
        xt_lock_mutex(self, ll->ll_lock);
 
172
        pushr_(xt_unlock_mutex, ll->ll_lock);
 
173
        for (;;) {
 
174
                if (ll->ll_item_count == 0)
 
175
                        break;
 
176
                xt_wait_cond(self, ll->ll_cond, ll->ll_lock);
 
177
        }
 
178
        freer_(); // xt_unlock_mutex(ll->ll_lock)
 
179
}
 
180
 
 
181
xtPublic u_int xt_ll_get_size(XTLinkedListPtr ll)
 
182
{
 
183
        return ll->ll_item_count;
 
184
}
 
185
 
 
186
xtPublic void xt_init_linkedqueue(XTThreadPtr XT_UNUSED(self), XTLinkedQueuePtr lq)
 
187
{
 
188
        lq->lq_count = 0;
 
189
        lq->lq_front = NULL;
 
190
        lq->lq_back = NULL;
 
191
}
 
192
 
 
193
xtPublic void xt_exit_linkedqueue(XTThreadPtr XT_UNUSED(self), XTLinkedQueuePtr lq)
 
194
{
 
195
        lq->lq_count = 0;
 
196
        lq->lq_front = NULL;
 
197
        lq->lq_back = NULL;
 
198
}
 
199
 
 
200
xtPublic void xt_lq_add(XTThreadPtr XT_UNUSED(self), XTLinkedQueuePtr lq, XTLinkedQItemPtr qi)
 
201
{
 
202
        lq->lq_count++;
 
203
        qi->qi_next = NULL;
 
204
        if (!lq->lq_front)
 
205
                lq->lq_front = qi;
 
206
        if (lq->lq_back)
 
207
                lq->lq_back->qi_next = qi;
 
208
        lq->lq_back = qi;
 
209
}
 
210
 
 
211
xtPublic XTLinkedQItemPtr xt_lq_remove(XTThreadPtr XT_UNUSED(self), XTLinkedQueuePtr lq)
 
212
{
 
213
        XTLinkedQItemPtr qi = NULL;
 
214
 
 
215
        if (!lq->lq_front) {
 
216
                qi = lq->lq_front;
 
217
                lq->lq_front = qi->qi_next;
 
218
                if (!lq->lq_front)
 
219
                        lq->lq_back = NULL;
 
220
                qi->qi_next = NULL;
 
221
        }
 
222
        return qi;
 
223
}
 
224