forked from teawater/libhermit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdequeue.h
More file actions
122 lines (97 loc) · 3.54 KB
/
Copy pathdequeue.h
File metadata and controls
122 lines (97 loc) · 3.54 KB
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
/*
* Copyright (c) 2016, Daniel Krebs, RWTH Aachen University
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @author Daniel Krebs
* @file include/hermit/dequeue.h
* @brief Double-ended queue implementation
*/
#include <hermit/stddef.h>
#include <hermit/spinlock.h>
#include <hermit/errno.h>
#include <string.h>
#ifndef __DEQUEUE_H__
#define __DEQUEUE_H__
#define NOT_NULL(dequeue) do { \
if(BUILTIN_EXPECT(!dequeue, 0)) { \
return -EINVAL; \
} \
} while(0)
typedef struct _dequeue_t {
size_t front; ///< point to first used entry
size_t back; ///< point to first unused entry
spinlock_t lock; ///< make dequeue thread safe
char* buffer; ///< pointer to buffer that holds elements
size_t buffer_length; ///< number of elements buffer can hold
size_t element_size; ///< size of one element in buffer
} dequeue_t;
static inline int
dequeue_init(dequeue_t* dequeue, void* buffer, size_t buffer_length, size_t element_size)
{
NOT_NULL(dequeue);
dequeue->front = 0;
dequeue->back = 0;
dequeue->buffer = buffer;
dequeue->buffer_length = buffer_length;
dequeue->element_size = element_size;
spinlock_init(&dequeue->lock);
return 0;
}
static inline int
dequeue_push(dequeue_t* dequeue, void* v)
{
NOT_NULL(dequeue);
spinlock_lock(&dequeue->lock);
size_t new_back = (dequeue->back + 1) % dequeue->buffer_length;
if(new_back == dequeue->front) {
spinlock_unlock(&dequeue->lock);
return -EOVERFLOW;
}
memcpy(&dequeue->buffer[dequeue->back * dequeue->element_size],
v,
dequeue->element_size);
dequeue->back = new_back;
spinlock_unlock(&dequeue->lock);
return 0;
}
static inline int
dequeue_pop(dequeue_t* dequeue, void* out)
{
NOT_NULL(dequeue);
NOT_NULL(out);
spinlock_lock(&dequeue->lock);
if(dequeue->front == dequeue->back) {
spinlock_unlock(&dequeue->lock);
return -ENOENT;
}
memcpy(out,
&dequeue->buffer[dequeue->front * dequeue->element_size],
dequeue->element_size);
dequeue->front = (dequeue->front + 1) % dequeue->buffer_length;
spinlock_unlock(&dequeue->lock);
return 0;
}
#endif // __DEQUEUE_H__