This repository has been archived by the owner on Jan 18, 2019. It is now read-only.
forked from cksystemsgroup/scal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lockbased_queue.h
184 lines (163 loc) · 4.86 KB
/
lockbased_queue.h
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (c) 2012-2013, the Scal Project Authors. All rights reserved.
// Please see the AUTHORS file for details. Use of this source code is governed
// by a BSD license that can be found in the LICENSE file.
#ifndef SCAL_DATASTRUCTURES_LOCKBASED_QUEUE_
#define SCAL_DATASTRUCTURES_LOCKBASED_QUEUE_
#include <assert.h>
#include <errno.h> // ETIMEDOUT
#include <pthread.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // strerror_r
#include <sys/time.h> // gettimeofday
#include "datastructures/queue.h"
#include "util/malloc.h"
#include "util/platform.h"
#include "util/threadlocals.h"
namespace lb_details {
template<typename T>
struct Node {
T value;
Node<T> *next;
};
} // namespace lb_details
template<typename T>
class LockBasedQueue : Queue<T> {
public:
LockBasedQueue(uint64_t dequeue_mode, uint64_t dequeue_timeout);
bool enqueue(T item);
inline bool dequeue(T *item) {
switch (dequeue_mode_) {
case 0:
return dequeue_default(item);
case 1:
return dequeue_blocking(item);
case 2:
return dequeue_timeout(item, dequeue_timeout_);
default:
return dequeue_default(item);
}
}
private:
typedef lb_details::Node<T> Node;
static const uint8_t kPtrAlignment = scal::kCachePrefetch;
Node *head_;
Node *tail_;
pthread_mutex_t *global_lock_;
pthread_cond_t *enqueue_cond_;
uint64_t dequeue_mode_;
uint64_t dequeue_timeout_;
inline void check_error(const char *std, int rc) {
if (rc != 0) {
char err[256];
char *tmp = strerror_r(rc, err, 256);
fprintf(stderr, "error: %s: %s %s\n", std, err, tmp);
abort();
}
}
bool dequeue_default(T *item);
bool dequeue_blocking(T *item);
bool dequeue_timeout(T *item, uint64_t timeout_ms);
};
template<typename T>
LockBasedQueue<T>::LockBasedQueue(uint64_t dequeue_mode,
uint64_t dequeue_timeout) {
global_lock_ = scal::get<pthread_mutex_t>(kPtrAlignment);
int rc = pthread_mutex_init(global_lock_, NULL);
check_error("pthread_mutex_init", rc);
enqueue_cond_ = scal::get<pthread_cond_t>(kPtrAlignment);
rc = pthread_cond_init(enqueue_cond_, NULL);
check_error("pthread_cond_init", rc);
Node *node = scal::get<Node>(kPtrAlignment);
head_ = node;
tail_ = node;
dequeue_mode_ = dequeue_mode;
dequeue_timeout_ = dequeue_timeout;
}
template<typename T>
bool LockBasedQueue<T>::enqueue(T item) {
Node *node = scal::tlget<Node>(kPtrAlignment);
node->value = item;
int rc = pthread_mutex_lock(global_lock_);
check_error("pthread_mutex_lock", rc);
Node *tail_old = tail_;
tail_old->next = node;
tail_ = node;
rc = pthread_cond_broadcast(enqueue_cond_);
check_error("pthread_cond_broadcast", rc);
rc = pthread_mutex_unlock(global_lock_);
check_error("pthread_mutex_unlock", rc);
return true;
}
template<typename T>
bool LockBasedQueue<T>::dequeue_default(T *item) {
int rc = pthread_mutex_lock(global_lock_);
check_error("pthread_mutex_lock", rc);
if (head_ == tail_) {
pthread_mutex_unlock(global_lock_);
check_error("pthread_mutex_unlock", rc);
return false;
}
*item = head_->next->value;
head_ = head_->next;
rc = pthread_mutex_unlock(global_lock_);
check_error("pthread_mutex_unlock", rc);
return true;
}
template<typename T>
bool LockBasedQueue<T>::dequeue_blocking(T *item) {
int rc;
while (true) {
rc = pthread_mutex_lock(global_lock_);
check_error("pthread_mutex_lock", rc);
while (head_ == tail_) {
pthread_cond_wait(enqueue_cond_, global_lock_);
}
assert(head_ != tail_);
*item = head_->next->value;
head_ = head_->next;
rc = pthread_mutex_unlock(global_lock_);
check_error("pthread_mutex_unlock", rc);
return true;
}
}
template<typename T>
bool LockBasedQueue<T>::dequeue_timeout(T *item, uint64_t timeout_ms) {
int rc;
struct timeval tp;
struct timespec ts;
uint64_t tmp_nsec;
rc = gettimeofday(&tp, NULL);
check_error("gettimeofday", rc);
ts.tv_sec = tp.tv_sec;
ts.tv_nsec = tp.tv_usec * 1000;
tmp_nsec = ts.tv_nsec;
tmp_nsec += timeout_ms * 1000000;
if (tmp_nsec >= 1000000000) {
ts.tv_sec += tmp_nsec / 1000000000;
ts.tv_nsec = tmp_nsec % 1000000000;
} else {
ts.tv_nsec = tmp_nsec;
}
while (true) {
rc =pthread_mutex_lock(global_lock_);
check_error("pthread_mutex_lock", rc);
while (head_ == tail_) {
rc = pthread_cond_timedwait(enqueue_cond_, global_lock_, &ts);
if (rc == ETIMEDOUT) {
rc = pthread_mutex_unlock(global_lock_);
check_error("pthread_mutex_unlock", rc);
return false;
}
check_error("pthread_cond_timedwait", rc);
}
assert(head_ != tail_);
*item = head_->next->value;
head_ = head_->next;
rc = pthread_mutex_unlock(global_lock_);
check_error("pthread_mutex_unlock", rc);
return true;
}
}
#endif // SCAL_DATASTRUCTURES_LOCKBASED_QUEUE_