File: opq.c

package info (click to toggle)
openipmi 2.0.37-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,100 kB
  • sloc: ansic: 140,279; python: 8,801; sh: 5,723; perl: 5,394; makefile: 490
file content (348 lines) | stat: -rw-r--r-- 7,780 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * opq.c
 *
 * Code for handling an operation queue.
 *
 * Author: MontaVista Software, Inc.
 *         Corey Minyard <minyard@mvista.com>
 *         source@mvista.com
 *
 * Copyright 2002,2003 MontaVista Software Inc.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *
 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdlib.h>
#include <string.h>

#include <OpenIPMI/os_handler.h>

#include <OpenIPMI/internal/ipmi_int.h>
#include <OpenIPMI/internal/ilist.h>
#include <OpenIPMI/internal/opq.h>

struct opq_elem_s
{
    int               block;
    opq_handler_cb    handler;
    void              *handler_data;
    opq_done_cb       done;
    void              *done_data;
    struct opq_elem_s *next;
    ilist_item_t      ilist_item;
};

struct opq_s
{
    ilist_t        *ops;
    os_hnd_lock_t  *lock;
    int            in_handler;
    os_handler_t   *os_hnd;
    opq_done_cb    done_handler;
    void           *done_data;
    int            blocked;
    int            in_destroy;
};

static void
opq_lock(opq_t *opq)
{
    if (opq->lock)
	opq->os_hnd->lock(opq->os_hnd, opq->lock);
}

static void
opq_unlock(opq_t *opq)
{
    if (opq->lock)
	opq->os_hnd->unlock(opq->os_hnd, opq->lock);
}

opq_t *
opq_alloc(os_handler_t *os_hnd)
{
    int   rv;
    opq_t *opq;

    opq = ipmi_mem_alloc(sizeof(*opq));
    if (!opq)
	return NULL;
    memset(opq, 0, sizeof(*opq));

    opq->os_hnd = os_hnd;
    opq->in_handler = 0;
    opq->ops = alloc_ilist();
    if (!(opq->ops)) {
	ipmi_mem_free(opq);
	return NULL;
    }

    if (os_hnd->create_lock) {
	rv = os_hnd->create_lock(opq->os_hnd, &(opq->lock));
	if (rv) {
	    free_ilist(opq->ops);
	    ipmi_mem_free(opq);
	    return NULL;
	}
    } else {
	opq->lock = NULL;
    }

    return opq;
}

static void
opq_destroy_item(ilist_iter_t *iter, void *item, void *cb_data)
{
    opq_elem_t *elem = (opq_elem_t *) item;

    elem->handler(elem->handler_data, 1);
    /* Memory for this is in elem, so we must delete it before we free
       the elem. */
    ilist_delete(iter);
    opq_free_elem(elem);
}

void
opq_destroy(opq_t *opq)
{
    /* Only allow this to be done once.  Callbacks might call this
       again. */
    opq_lock(opq);
    if (opq->in_destroy) {
	opq_unlock(opq);
	return;
    }
    opq->in_destroy = 1;
    opq_unlock(opq);

    ilist_iter(opq->ops, opq_destroy_item, NULL);
    free_ilist(opq->ops);
    if (opq->lock)
	opq->os_hnd->destroy_lock(opq->os_hnd, opq->lock);
    ipmi_mem_free(opq);
}

static void
start_next_op(opq_t *opq)
{
    ilist_iter_t iter;
    opq_elem_t   *elem;
    int          success;

    ilist_init_iter(&iter, opq->ops);
    ilist_first(&iter);
    elem = ilist_get(&iter);
    while (elem) {
	ilist_delete(&iter);
	opq->done_handler = elem->done;
	opq->done_data = elem->done_data;
	opq_unlock(opq);
	success = elem->handler(elem->handler_data, 0);
	opq_free_elem(elem);
	opq_lock(opq);
	if (success == OPQ_HANDLER_STARTED)
	    break;
	ilist_first(&iter);
	elem = ilist_get(&iter);
    }
    if (!elem)
	opq->in_handler = 0;
}

opq_elem_t *
opq_alloc_elem(void)
{
    opq_elem_t *elem;
    elem = ipmi_mem_alloc(sizeof(opq_elem_t));
    return elem;
}

void
opq_free_elem(opq_elem_t *elem)
{
    ipmi_mem_free(elem);
}

int
opq_new_op_prio(opq_t *opq, opq_handler_cb handler, void *cb_data,
		int nowait, int prio, opq_elem_t *elem)
{
    int        success;

    opq_lock(opq);
    if (opq->in_handler) {
	if (nowait) {
	    opq_unlock(opq);
	    return -1;
	}
	if (!elem) {
	    elem = opq_alloc_elem();
	    if (!elem)
		goto out_err;
	}
	elem->handler = handler;
	elem->done = NULL;
	elem->handler_data = cb_data;
	elem->block = 1;
	if (prio)
	    ilist_add_head(opq->ops, elem, &elem->ilist_item);
	else
	    ilist_add_tail(opq->ops, elem, &elem->ilist_item);
	opq->blocked = 0;
	opq_unlock(opq);
    } else {
	if (elem)
	    opq_free_elem(elem);
	opq->blocked = 0;
	opq->in_handler = 1;
	opq->done_handler = NULL;
	opq_unlock(opq);
	success = handler(cb_data, 0);
	if (success == OPQ_HANDLER_ABORTED) {
	    /* In case any were added while I was unlocked. */
	    opq_lock(opq);
	    start_next_op(opq);
	    opq_unlock(opq);
	}
    }

    return 1;

 out_err:
    opq_unlock(opq);
    return 0;
}

int
opq_new_op(opq_t *opq, opq_handler_cb handler, void *cb_data, int nowait)
{
    return opq_new_op_prio(opq, handler, cb_data, nowait, OPQ_ADD_TAIL, NULL);
}

int
opq_new_op_with_done(opq_t          *opq,
		     opq_handler_cb handler,
		     void           *handler_data,
		     opq_done_cb    done,
		     void           *done_data)
{
    opq_elem_t *elem;
    int        success;

    opq_lock(opq);
    if (opq->in_handler) {
	elem = ipmi_mem_alloc(sizeof(*elem));
	if (!elem)
	    goto out_err;
	elem->handler = handler;
	elem->handler_data = handler_data;
	elem->done = done;
	elem->done_data = done_data;
	elem->block = opq->blocked;
	ilist_add_tail(opq->ops, elem, &elem->ilist_item);
	opq->blocked = 0;
	opq_unlock(opq);
    } else {
	opq->blocked = 0;
	opq->in_handler = 1;
	opq->done_handler = done;
	opq->done_data = done_data;
	opq_unlock(opq);
	success = handler(handler_data, 0);
	if (success == OPQ_HANDLER_ABORTED) {
	    /* In case any were added while I was unlocked. */
	    opq_lock(opq);
	    start_next_op(opq);
	    opq_unlock(opq);
	}
    }

    return 1;

 out_err:
    opq_unlock(opq);
    return 0;
}

void
opq_add_block(opq_t *opq)
{
    opq_lock(opq);
    opq->blocked = 1;
    opq_unlock(opq);
}

void
opq_op_done(opq_t *opq)
{
    ilist_iter_t   iter;
    opq_elem_t     *elem;
    opq_elem_t     *list = NULL;
    opq_elem_t     *next;
    opq_elem_t     **list_end = &list;
    opq_done_cb    done_handler;
    void           *done_data;

    /* First check for done handlers. */
    opq_lock(opq);
    ilist_init_iter(&iter, opq->ops);
    ilist_first(&iter);
    elem = ilist_get(&iter);
    while (elem && (!elem->block)) {
	ilist_delete(&iter);
	elem->next = NULL;
	*list_end = elem;
	list_end = &(elem->next);
	elem = ilist_get(&iter);
    }
    done_handler = opq->done_handler;
    done_data = opq->done_data;
    opq->done_handler = NULL;
    if (done_handler || list) {
	/* There are done handlers to call, unlock and call them. */
	opq_unlock(opq);

	if (done_handler)
	    done_handler(done_data, 0);
	while (list) {
	    next = list->next;
	    list->done(list->done_data, 0);
	    opq_free_elem(list);
	    list = next;
	}

	opq_lock(opq);
	/* During the time we were unlocked, handlers may have been
           added. */
	ilist_first(&iter);
	elem = ilist_get(&iter);
    }
    start_next_op(opq);
    opq_unlock(opq);
}

int
opq_stuff_in_progress(opq_t *opq)
{
    return opq->in_handler;
}