-
Notifications
You must be signed in to change notification settings - Fork 0
/
opque.c
277 lines (209 loc) · 7.39 KB
/
opque.c
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
/*
Advanced Computing Center for Research and Education Proprietary License
Version 1.0 (April 2006)
Copyright (c) 2006, Advanced Computing Center for Research and Education,
Vanderbilt University, All rights reserved.
This Work is the sole and exclusive property of the Advanced Computing Center
for Research and Education department at Vanderbilt University. No right to
disclose or otherwise disseminate any of the information contained herein is
granted by virtue of your possession of this software except in accordance with
the terms and conditions of a separate License Agreement entered into with
Vanderbilt University.
THE AUTHOR OR COPYRIGHT HOLDERS PROVIDES THE "WORK" ON AN "AS IS" BASIS,
WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, TITLE, FITNESS FOR A PARTICULAR
PURPOSE, AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Vanderbilt University
Advanced Computing Center for Research and Education
230 Appleton Place
Nashville, TN 37203
http://www.accre.vanderbilt.edu
*/
#include <assert.h>
#include <apr_thread_mutex.h>
#include <apr_thread_cond.h>
#include <stdlib.h>
#include <string.h>
#include "log.h"
#include "opque.h"
//*************************************************************
// _opque_cb - Global callback for all opque's
//*************************************************************
void _opque_cb(void *v)
{
int n;
opque_an_t *qan = (opque_an_t *)v;
lock_opque(qan->q);
push(qan->q->finished, qan->iol); //** It always goes o nthe finished list
n = oplist_nfailed(qan->iol);
if (n != 0) push(qan->q->failed, qan->iol); //** Push it on the failed list if needed
qan->q->nleft--;
if (qan->q->nleft <= 0) { //** we're finished
apr_thread_cond_broadcast(qan->q->cond);
}
unlock_opque(qan->q);
}
//*************************************************************
// init_opque - Initializes a que list container
//*************************************************************
void init_opque(opque_t *que, oplist_app_notify_t *an)
{
apr_thread_mutex_lock(_oplist_lock);
que->id = _oplist_counter;
_oplist_counter++;
apr_thread_mutex_unlock(_oplist_lock);
apr_thread_mutex_create(&(que->lock), APR_THREAD_MUTEX_DEFAULT,_oplist_pool);
apr_thread_cond_create(&(que->cond), _oplist_pool);
que->list = new_stack();
que->finished = new_stack();
que->failed = new_stack();
que->oplist_an = new_stack();
que->count_id = 0;
que->nleft = 0;
que->an = NULL; app_notify_append(que->an, an);
}
//*************************************************************
// new_opque - Generates a new que container
//*************************************************************
opque_t *new_opque(oplist_app_notify_t *an)
{
opque_t *q = (opque_t *)malloc(sizeof(opque_t));
if (q == NULL) {
log_printf(0, "new_opque: malloc failed!\n");
return(NULL);
}
init_opque(q, an);
return(q);
}
//*************************************************************
// teardown_opque - tears down the que container
//*************************************************************
void teardown_opque(opque_t *q)
{
log_printf(15, "teardown_opque: opque=%d size(list)=%d size(finished)=%d size(failed)=%d\n",
q->id, stack_size(q->list), stack_size(q->finished), stack_size(q->failed));
free_stack(q->list, 0);
free_stack(q->finished, 0);
free_stack(q->failed, 0);
free_stack(q->oplist_an, 0);
apr_thread_mutex_destroy(q->lock);
apr_thread_cond_destroy(q->cond);
}
//*************************************************************
// free_opque - Frees the que container
//*************************************************************
void free_opque(opque_t *q)
{
teardown_opque(q);
free(q);
}
//*************************************************************
// finalize_opque - Destroys the que container
// but does NOT free the que memory
//*************************************************************
void finalize_opque(opque_t *q)
{
teardown_opque(q);
}
//*************************************************************
// add_opque - Adds a task list to the que
//*************************************************************
int add_opque(opque_t *q, oplist_t *iol)
{
opque_an_t *qan;
//** Create the oplist callback **
assert((qan = (opque_an_t *)malloc(sizeof(opque_an_t))) != NULL);
app_notify_set(&(qan->an), _opque_cb, (void *)qan); //** Set the global callback for the list
lock_opque(q);
log_printf(15, "add_opque: q=%d oplist=%d\n", q->id, iol->id);
//** Add the CB to the oplist
qan->iol = iol;
qan->q = q;
push(q->oplist_an, (void *)qan);
q->nleft++;
move_to_bottom(q->list);
insert_below(q->list, (void *)iol);
unlock_opque(q);
oplist_notify_append(iol, &(qan->an)); //** Lastly append the callback
return(0);
}
//*************************************************************
// opque_get_failed - returns a failed task list from the provided que
// or NULL if none exist.
//*************************************************************
oplist_t *opque_get_failed(opque_t *q)
{
void *iol;
lock_opque(q);
iol = pop(q->failed);
unlock_opque(q);
return(iol);
}
//*************************************************************
// opque_nfailed- Returns the # of errors left in the
// failed que
//*************************************************************
int opque_nfailed(opque_t *q)
{
int nf;
lock_opque(q);
nf = stack_size(q->failed);
unlock_opque(q);
return(nf);
}
//*************************************************************
// opque_tasks_left - Returns the number of tasks remaining
//*************************************************************
int opque_tasks_left(opque_t *q)
{
int n;
lock_opque(q);
n = q->nleft;
unlock_opque(q);
return(n);
}
//*************************************************************
// opque_notify_append - Adds a callback to the opque
//*************************************************************
void opque_notify_append(opque_t *q, oplist_app_notify_t *an)
{
app_notify_append(q->an, an);
}
//*************************************************************
// opque_waitany - waits until any given task completes and
// returns the operation.
//*************************************************************
oplist_t *opque_waitany(opque_t *q)
{
void *opl;
lock_opque(q);
// opl = (oplist_t *)pop(q->finished);
// if ((q->nleft == 0) && (opl == NULL)) { //** Nothing left to do so exit
// unlock_opque(q);
// return(NULL);
// }
while (((opl = (oplist_t *)pop(q->finished)) == NULL) && (q->nleft > 0)) {
apr_thread_cond_wait(q->cond, q->lock); //** Sleep until something completes
}
unlock_opque(q);
return(opl);
}
//*************************************************************
// opque_waitall - waits until all the tasks are completed
// It returns op_status if all the tasks completed without problems or
// with the last error otherwise.
//*************************************************************
int opque_waitall(opque_t *q)
{
oplist_t *opl;
lock_opque(q);
while (q->nleft > 0) {
unlock_opque(q);
opl = opque_waitany(q);
lock_opque(q);
}
return(stack_size(q->failed));
}