~ps10gel/ubuntu/xenial/trafficserver/6.2.0

« back to all changes in this revision

Viewing changes to proxy/mgmt2/utils/SimpleQueue.cc

  • Committer: Bazaar Package Importer
  • Author(s): Arno Toell
  • Date: 2011-01-13 11:49:18 UTC
  • Revision ID: james.westby@ubuntu.com-20110113114918-vu422h8dknrgkj15
Tags: upstream-2.1.5-unstable
ImportĀ upstreamĀ versionĀ 2.1.5-unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @file
 
2
 
 
3
  A brief file description
 
4
 
 
5
  @section license License
 
6
 
 
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
 
14
 
 
15
      http://www.apache.org/licenses/LICENSE-2.0
 
16
 
 
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.
 
22
 */
 
23
 
 
24
#include "ink_platform.h"
 
25
#include "ink_unused.h"    /* MAGIC_EDITING_TAG */
 
26
#include "ink_assert.h"
 
27
 
 
28
#include "SimpleQueue.h"
 
29
 
 
30
/****************************************************************************
 
31
 *
 
32
 *  SimpleQueue.cc - a thread safe queue
 
33
 *
 
34
 *
 
35
 ****************************************************************************/
 
36
 
 
37
//
 
38
// class SimpleQueue
 
39
//
 
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.
 
43
//
 
44
//   An attempt to remove the first item  when the queue is empty
 
45
//     will block until an item is available
 
46
//
 
47
//   accessLock protects head, tail and entries in the linked list
 
48
//   waitSema is used to block a thread until an item is available
 
49
 
 
50
SimpleQueue::SimpleQueue()
 
51
{
 
52
  ink_mutex_init(&accessLock, "SimpleQueue Lock");
 
53
#if defined(darwin)
 
54
  static int qnum = 0;
 
55
  char sname[NAME_MAX];
 
56
  qnum++;
 
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);
 
60
#else /* !darwin */
 
61
  ink_sem_init(&waitSema, 0);
 
62
#endif /* !darwin */
 
63
  head = NULL;
 
64
  tail = NULL;
 
65
}
 
66
 
 
67
//
 
68
// destory the queue
 
69
//
 
70
//    Does not attempt to deallocate the entries
 
71
//      bucket data points to.
 
72
//
 
73
SimpleQueue::~SimpleQueue()
 
74
{
 
75
  SimpleQueueEntry *current;
 
76
  SimpleQueueEntry *next;
 
77
  ink_mutex_acquire(&accessLock);
 
78
 
 
79
  // Delete all the containers in the queue
 
80
  current = head;
 
81
  while (current != NULL) {
 
82
    next = current->next;
 
83
    delete current;
 
84
    current = next;
 
85
  }
 
86
 
 
87
  ink_mutex_destroy(&accessLock);
 
88
#if defined(darwin)
 
89
  ink_sem_close(waitSema);
 
90
#else
 
91
  ink_sem_destroy(&waitSema);
 
92
#endif
 
93
}
 
94
 
 
95
//
 
96
// SimpleQueue::dequeue()
 
97
//
 
98
//   Waits until there is something on the queue
 
99
//     and then returns the first item
 
100
//
 
101
void *
 
102
SimpleQueue::dequeue()
 
103
{
 
104
  SimpleQueueEntry *headEntry;
 
105
  void *data;
 
106
 
 
107
  // Wait for something to be on the queue
 
108
#if defined(darwin)
 
109
  ink_sem_wait(waitSema);
 
110
#else
 
111
  ink_sem_wait(&waitSema);
 
112
#endif
 
113
  ink_mutex_acquire(&accessLock);
 
114
 
 
115
  ink_assert(head != NULL && tail != NULL);
 
116
 
 
117
  headEntry = head;
 
118
  head = head->next;
 
119
 
 
120
  if (head == NULL) {
 
121
    // The queue is now empty
 
122
    tail = NULL;
 
123
  } else {
 
124
    // New head element
 
125
    head->prev = NULL;
 
126
  }
 
127
 
 
128
  ink_mutex_release(&accessLock);
 
129
 
 
130
  data = headEntry->data;
 
131
  delete headEntry;
 
132
 
 
133
  return data;
 
134
}
 
135
 
 
136
//
 
137
// SimpleQueue::pop()
 
138
//
 
139
//   Waits until there is something on the queue
 
140
//     and then returns the last item
 
141
//
 
142
void *
 
143
SimpleQueue::pop()
 
144
{
 
145
  SimpleQueueEntry *tailEntry;
 
146
  void *data;
 
147
 
 
148
  // Wait for something to be on the queue
 
149
#if defined(darwin)
 
150
  ink_sem_wait(waitSema);
 
151
#else
 
152
  ink_sem_wait(&waitSema);
 
153
#endif
 
154
  ink_mutex_acquire(&accessLock);
 
155
 
 
156
  ink_assert(head != NULL && tail != NULL);
 
157
 
 
158
  tailEntry = tail;
 
159
  tail = tail->prev;
 
160
 
 
161
  if (tail == NULL) {
 
162
    // The queue is now empty
 
163
    head = NULL;
 
164
  } else {
 
165
    // New trail element
 
166
    tail->next = NULL;
 
167
  }
 
168
 
 
169
  ink_mutex_release(&accessLock);
 
170
 
 
171
  data = tailEntry->data;
 
172
  delete tailEntry;
 
173
 
 
174
  return data;
 
175
}
 
176
 
 
177
//
 
178
//  SimpleQueue::enqueue(void* data)
 
179
//
 
180
//     adds new item to the tail of the list
 
181
//
 
182
void
 
183
SimpleQueue::enqueue(void *data)
 
184
{
 
185
  SimpleQueueEntry *newEntry = new SimpleQueueEntry;
 
186
  newEntry->data = data;
 
187
 
 
188
  ink_mutex_acquire(&accessLock);
 
189
 
 
190
  if (tail == NULL) {
 
191
    tail = newEntry;
 
192
    ink_assert(head == NULL);
 
193
    head = newEntry;
 
194
    newEntry->next = NULL;
 
195
    newEntry->prev = NULL;
 
196
  } else {
 
197
    newEntry->prev = tail;
 
198
    newEntry->next = NULL;
 
199
    tail->next = newEntry;
 
200
    tail = newEntry;
 
201
 
 
202
  }
 
203
 
 
204
  ink_mutex_release(&accessLock);
 
205
#if defined(darwin)
 
206
  ink_sem_post(waitSema);
 
207
#else
 
208
  ink_sem_post(&waitSema);
 
209
#endif
 
210
}
 
211
 
 
212
//
 
213
//  SimpleQueue::push(void* data)
 
214
//
 
215
//     adds new item to the tail of the list
 
216
//
 
217
void
 
218
SimpleQueue::push(void *data)
 
219
{
 
220
  enqueue(data);
 
221
}
 
222
 
 
223
// SimpleQueue::Print()
 
224
//
 
225
//   Prints out the queue.  For DEBUG only, not thread safe
 
226
//
 
227
bool
 
228
SimpleQueue::isEmpty()
 
229
{
 
230
  return (head == NULL);
 
231
}
 
232