~ubuntu-branches/ubuntu/precise/corosync/precise-proposed

« back to all changes in this revision

Viewing changes to include/corosync/queue.h

  • Committer: Bazaar Package Importer
  • Author(s): Ante Karamatic
  • Date: 2009-08-21 09:29:56 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090821092956-w9qxxxx3zeoh8dem
Tags: 1.0.0-4ubuntu2
* debian/control:
  - 'Ubuntu Developers' instead of 'Ubuntu Core Developers'
    as maintainer
  - Bump debhelper dependecy to 7

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (c) 2002-2004 MontaVista Software, Inc.
3
 
 *
4
 
 * All rights reserved.
5
 
 *
6
 
 * Author: Steven Dake (sdake@redhat.com)
7
 
 *
8
 
 * This software licensed under BSD license, the text of which follows:
9
 
 * 
10
 
 * Redistribution and use in source and binary forms, with or without
11
 
 * modification, are permitted provided that the following conditions are met:
12
 
 *
13
 
 * - Redistributions of source code must retain the above copyright notice,
14
 
 *   this list of conditions and the following disclaimer.
15
 
 * - Redistributions in binary form must reproduce the above copyright notice,
16
 
 *   this list of conditions and the following disclaimer in the documentation
17
 
 *   and/or other materials provided with the distribution.
18
 
 * - Neither the name of the MontaVista Software, Inc. nor the names of its
19
 
 *   contributors may be used to endorse or promote products derived from this
20
 
 *   software without specific prior written permission.
21
 
 *
22
 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32
 
 * THE POSSIBILITY OF SUCH DAMAGE.
33
 
 */
34
 
#ifndef QUEUE_H_DEFINED
35
 
#define QUEUE_H_DEFINED
36
 
 
37
 
#include <string.h>
38
 
#include <pthread.h>
39
 
#include "assert.h"
40
 
 
41
 
#ifdef COROSYNC_SOLARIS
42
 
/* struct queue is already defined in sys/stream.h on Solaris */
43
 
#define queue _queue
44
 
#endif
45
 
struct queue {
46
 
        int head;
47
 
        int tail;
48
 
        int used;
49
 
        int usedhw;
50
 
        int size;
51
 
        void *items;
52
 
        int size_per_item;
53
 
        int iterator;
54
 
        pthread_mutex_t mutex;
55
 
};
56
 
 
57
 
static inline int queue_init (struct queue *queue, int queue_items, int size_per_item) {
58
 
        queue->head = 0;
59
 
        queue->tail = queue_items - 1;
60
 
        queue->used = 0;
61
 
        queue->usedhw = 0;
62
 
        queue->size = queue_items;
63
 
        queue->size_per_item = size_per_item;
64
 
 
65
 
        queue->items = malloc (queue_items * size_per_item);
66
 
        if (queue->items == 0) {
67
 
                return (-ENOMEM);
68
 
        }
69
 
        memset (queue->items, 0, queue_items * size_per_item);
70
 
        pthread_mutex_init (&queue->mutex, NULL);
71
 
        return (0);
72
 
}
73
 
 
74
 
static inline int queue_reinit (struct queue *queue)
75
 
{
76
 
        pthread_mutex_lock (&queue->mutex);
77
 
        queue->head = 0;
78
 
        queue->tail = queue->size - 1;
79
 
        queue->used = 0;
80
 
        queue->usedhw = 0;
81
 
 
82
 
        memset (queue->items, 0, queue->size * queue->size_per_item);
83
 
        pthread_mutex_unlock (&queue->mutex);
84
 
        return (0);
85
 
}
86
 
 
87
 
static inline void queue_free (struct queue *queue) {
88
 
        pthread_mutex_destroy (&queue->mutex);
89
 
        free (queue->items);
90
 
}
91
 
 
92
 
static inline int queue_is_full (struct queue *queue) {
93
 
        int full;
94
 
 
95
 
        pthread_mutex_lock (&queue->mutex);
96
 
        full = ((queue->size - 1) == queue->used);
97
 
        pthread_mutex_unlock (&queue->mutex);
98
 
        return (full);
99
 
}
100
 
 
101
 
static inline int queue_is_empty (struct queue *queue) {
102
 
        int empty;
103
 
 
104
 
        pthread_mutex_lock (&queue->mutex);
105
 
        empty = (queue->used == 0);
106
 
        pthread_mutex_unlock (&queue->mutex);
107
 
        return (empty);
108
 
}
109
 
 
110
 
static inline void queue_item_add (struct queue *queue, void *item)
111
 
{
112
 
        char *queue_item;
113
 
        int queue_position;
114
 
 
115
 
        pthread_mutex_lock (&queue->mutex);
116
 
        queue_position = queue->head;
117
 
        queue_item = queue->items;
118
 
        queue_item += queue_position * queue->size_per_item;
119
 
        memcpy (queue_item, item, queue->size_per_item);
120
 
 
121
 
        assert (queue->tail != queue->head);
122
 
 
123
 
        queue->head = (queue->head + 1) % queue->size;
124
 
        queue->used++;
125
 
        if (queue->used > queue->usedhw) {
126
 
                queue->usedhw = queue->used;
127
 
        }
128
 
        pthread_mutex_unlock (&queue->mutex);
129
 
}
130
 
 
131
 
static inline void *queue_item_get (struct queue *queue)
132
 
{
133
 
        char *queue_item;
134
 
        int queue_position;
135
 
 
136
 
        pthread_mutex_lock (&queue->mutex);
137
 
        queue_position = (queue->tail + 1) % queue->size;
138
 
        queue_item = queue->items;
139
 
        queue_item += queue_position * queue->size_per_item;
140
 
        pthread_mutex_unlock (&queue->mutex);
141
 
        return ((void *)queue_item);
142
 
}
143
 
 
144
 
static inline void queue_item_remove (struct queue *queue) {
145
 
        pthread_mutex_lock (&queue->mutex);
146
 
        queue->tail = (queue->tail + 1) % queue->size;
147
 
        
148
 
        assert (queue->tail != queue->head);
149
 
 
150
 
        queue->used--;
151
 
        assert (queue->used >= 0);
152
 
        pthread_mutex_unlock (&queue->mutex);
153
 
}
154
 
 
155
 
static inline void queue_items_remove (struct queue *queue, int rel_count)
156
 
{
157
 
        pthread_mutex_lock (&queue->mutex);
158
 
        queue->tail = (queue->tail + rel_count) % queue->size;
159
 
        
160
 
        assert (queue->tail != queue->head);
161
 
 
162
 
        queue->used -= rel_count;
163
 
        pthread_mutex_unlock (&queue->mutex);
164
 
}
165
 
 
166
 
 
167
 
static inline void queue_item_iterator_init (struct queue *queue)
168
 
{
169
 
        pthread_mutex_lock (&queue->mutex);
170
 
        queue->iterator = (queue->tail + 1) % queue->size;
171
 
        pthread_mutex_unlock (&queue->mutex);
172
 
}
173
 
 
174
 
static inline void *queue_item_iterator_get (struct queue *queue)
175
 
{
176
 
        char *queue_item;
177
 
        int queue_position;
178
 
 
179
 
        pthread_mutex_lock (&queue->mutex);
180
 
        queue_position = (queue->iterator) % queue->size;
181
 
        if (queue->iterator == queue->head) {
182
 
                pthread_mutex_unlock (&queue->mutex);
183
 
                return (0);
184
 
        }
185
 
        queue_item = queue->items;
186
 
        queue_item += queue_position * queue->size_per_item;
187
 
        pthread_mutex_unlock (&queue->mutex);
188
 
        return ((void *)queue_item);
189
 
}
190
 
 
191
 
static inline int queue_item_iterator_next (struct queue *queue)
192
 
{
193
 
        int next_res;
194
 
 
195
 
        pthread_mutex_lock (&queue->mutex);
196
 
        queue->iterator = (queue->iterator + 1) % queue->size;
197
 
 
198
 
        next_res = queue->iterator == queue->head;
199
 
        pthread_mutex_unlock (&queue->mutex);
200
 
        return (next_res);
201
 
}
202
 
 
203
 
static inline void queue_avail (struct queue *queue, int *avail)
204
 
{
205
 
        pthread_mutex_lock (&queue->mutex);
206
 
        *avail = queue->size - queue->used - 2;
207
 
        assert (*avail >= 0);
208
 
        pthread_mutex_unlock (&queue->mutex);
209
 
}
210
 
 
211
 
static inline int queue_used (struct queue *queue) {
212
 
        int used;
213
 
 
214
 
        pthread_mutex_lock (&queue->mutex);
215
 
        used = queue->used;
216
 
        pthread_mutex_unlock (&queue->mutex);
217
 
 
218
 
        return (used);
219
 
}
220
 
 
221
 
static inline int queue_usedhw (struct queue *queue) {
222
 
        int usedhw;
223
 
 
224
 
        pthread_mutex_lock (&queue->mutex);
225
 
        usedhw = queue->usedhw;
226
 
        pthread_mutex_unlock (&queue->mutex);
227
 
 
228
 
        return (usedhw);
229
 
}
230
 
 
231
 
#endif /* QUEUE_H_DEFINED */