3
Implementation of a simple linked list queue
5
@section license License
7
Licensed to the Apache Software Foundation (ASF) under one
8
or more contributor license agreements. See the NOTICE file
9
distributed with this work for additional information
10
regarding copyright ownership. The ASF licenses this file
11
to you under the Apache License, Version 2.0 (the
12
"License"); you may not use this file except in compliance
13
with the License. You may obtain a copy of the License at
15
http://www.apache.org/licenses/LICENSE-2.0
17
Unless required by applicable law or agreed to in writing, software
18
distributed under the License is distributed on an "AS IS" BASIS,
19
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
See the License for the specific language governing permissions and
21
limitations under the License.
24
#include "ink_config.h"
28
#if !defined(freebsd) && !defined(darwin)
34
#include "ink_unused.h" /* MAGIC_EDITING_TAG */
35
#include "ink_llqueue.h"
38
#define RECORD_CHUNK 1024
40
// These are obviously not used anywhere, I don't know if or how they
41
// were supposed to work, but #ifdef'ing them out of here for now.
49
if (Q->free != NULL) {
51
Q->free = Q->free->next;
56
Q->free = (LLQrec *) xmalloc(RECORD_CHUNK * sizeof(LLQrec));
61
for (i = 0; i < RECORD_CHUNK; i++)
62
Q->free[i].next = &Q->free[i + 1];
64
Q->free[RECORD_CHUNK - 1].next = NULL;
68
Q->free = Q->free->next;
73
// Not used either ...
75
freerec(LLQ * Q, LLQrec * rec)
85
const char *totally_bogus_name = "create_queue";
88
new_val = (LLQ *) xmalloc(sizeof(LLQ));
96
snprintf(sname,NAME_MAX,"%s%d",totally_bogus_name,qnum);
97
ink_sem_unlink(sname); // FIXME: remove, semaphore should be properly deleted after usage
98
new_val->sema = ink_sem_open(sname, O_CREAT | O_EXCL, 0777, 0);
100
ink_sem_init(&(new_val->sema), 0);
102
ink_mutex_init(&(new_val->mux), totally_bogus_name);
104
new_val->head = new_val->tail = new_val->free = NULL;
105
new_val->len = new_val->highwater = 0;
110
// matching delete function, only for empty queue!
112
delete_queue(LLQ * Q)
114
// There seems to have been some ideas of making sure that this queue is
115
// actually empty ...
125
enqueue(LLQ * Q, void *data)
129
ink_mutex_acquire(&(Q->mux));
131
//new_val= newrec(Q);
132
new_val = (LLQrec *) xmalloc(sizeof(LLQrec));
135
ink_mutex_release(&(Q->mux));
139
new_val->data = data;
140
new_val->next = NULL;
143
Q->tail->next = new_val;
150
if (Q->len > Q->highwater)
151
Q->highwater = Q->len;
152
ink_mutex_release(&(Q->mux));
154
ink_sem_post(Q->sema);
156
ink_sem_post(&(Q->sema));
166
/* Do I really need to grab the lock here? */
167
/* ink_mutex_acquire(&(Q->mux)); */
169
/* ink_mutex_release(&(Q->mux)); */
174
queue_highwater(LLQ * Q)
178
/* Do I really need to grab the lock here? */
179
/* ink_mutex_acquire(&(Q->mux)); */
180
highwater = Q->highwater;
181
/* ink_mutex_release(&(Q->mux)); */
188
*---------------------------------------------------------------------------
192
* Is the queue empty?
195
* nonzero if empty, zero else.
201
* Thread Safety: safe.
202
* Mem Management: n/a.
204
*---------------------------------------------------------------------------
207
queue_is_empty(LLQ * Q)
225
ink_sem_wait(Q->sema);
227
ink_sem_wait(&(Q->sema));
229
ink_mutex_acquire(&(Q->mux));
232
if (Q->head == NULL) {
234
ink_mutex_release(&(Q->mux));
241
Q->head = Q->head->next;
250
ink_mutex_release(&(Q->mux));
260
testfun(void *unused)
271
printf("DEQUEUE: %d\n", (int) dequeue(Q));
272
} else if (num == -1) {
273
printf("queue_is_empty: %d\n", queue_is_empty(Q));
275
printf("enqueue: %d\n", num);
276
enqueue(Q, (void *) num);
284
* test harness-- hit Ctrl-C if it blocks or you get tired.
289
assert(thr_create(NULL, 0, testfun, (void *) NULL, THR_NEW_LWP, NULL) == 0);