3
A brief file description
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_platform.h"
25
#include "ink_unused.h" /* MAGIC_EDITING_TAG */
26
#include "ink_assert.h"
28
#include "SimpleQueue.h"
30
/****************************************************************************
32
* SimpleQueue.cc - a thread safe queue
35
****************************************************************************/
40
// The queue is a doublely linked list. The two operations
41
// permitted are add a new item to the end of the queue
42
// and remove the first item.
44
// An attempt to remove the first item when the queue is empty
45
// will block until an item is available
47
// accessLock protects head, tail and entries in the linked list
48
// waitSema is used to block a thread until an item is available
50
SimpleQueue::SimpleQueue()
52
ink_mutex_init(&accessLock, "SimpleQueue Lock");
57
snprintf(sname,NAME_MAX,"%s%d","SimpleQueueLock",qnum);
58
ink_sem_unlink(sname); // FIXME: remove, semaphore should be properly deleted after usage
59
waitSema = ink_sem_open(sname, O_CREAT | O_EXCL, 0777, 0);
61
ink_sem_init(&waitSema, 0);
70
// Does not attempt to deallocate the entries
71
// bucket data points to.
73
SimpleQueue::~SimpleQueue()
75
SimpleQueueEntry *current;
76
SimpleQueueEntry *next;
77
ink_mutex_acquire(&accessLock);
79
// Delete all the containers in the queue
81
while (current != NULL) {
87
ink_mutex_destroy(&accessLock);
89
ink_sem_close(waitSema);
91
ink_sem_destroy(&waitSema);
96
// SimpleQueue::dequeue()
98
// Waits until there is something on the queue
99
// and then returns the first item
102
SimpleQueue::dequeue()
104
SimpleQueueEntry *headEntry;
107
// Wait for something to be on the queue
109
ink_sem_wait(waitSema);
111
ink_sem_wait(&waitSema);
113
ink_mutex_acquire(&accessLock);
115
ink_assert(head != NULL && tail != NULL);
121
// The queue is now empty
128
ink_mutex_release(&accessLock);
130
data = headEntry->data;
137
// SimpleQueue::pop()
139
// Waits until there is something on the queue
140
// and then returns the last item
145
SimpleQueueEntry *tailEntry;
148
// Wait for something to be on the queue
150
ink_sem_wait(waitSema);
152
ink_sem_wait(&waitSema);
154
ink_mutex_acquire(&accessLock);
156
ink_assert(head != NULL && tail != NULL);
162
// The queue is now empty
169
ink_mutex_release(&accessLock);
171
data = tailEntry->data;
178
// SimpleQueue::enqueue(void* data)
180
// adds new item to the tail of the list
183
SimpleQueue::enqueue(void *data)
185
SimpleQueueEntry *newEntry = new SimpleQueueEntry;
186
newEntry->data = data;
188
ink_mutex_acquire(&accessLock);
192
ink_assert(head == NULL);
194
newEntry->next = NULL;
195
newEntry->prev = NULL;
197
newEntry->prev = tail;
198
newEntry->next = NULL;
199
tail->next = newEntry;
204
ink_mutex_release(&accessLock);
206
ink_sem_post(waitSema);
208
ink_sem_post(&waitSema);
213
// SimpleQueue::push(void* data)
215
// adds new item to the tail of the list
218
SimpleQueue::push(void *data)
223
// SimpleQueue::Print()
225
// Prints out the queue. For DEBUG only, not thread safe
228
SimpleQueue::isEmpty()
230
return (head == NULL);