-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmpmc_queue.h
More file actions
221 lines (186 loc) · 4.26 KB
/
mpmc_queue.h
File metadata and controls
221 lines (186 loc) · 4.26 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#ifndef __MPMC_QUEUE__
#define __MPMC_QUEUE__
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include "src/core/lib/iomgr/executor/mpmcqueue.h"
class GrpcMpmcQueue
{
public:
GrpcMpmcQueue(int capacity)
{
this->res_max = capacity;
this->res_cnt = 0;
}
void enqueue(int element)
{
void *tmp = malloc(sizeof(int));
memcpy(tmp, &element, sizeof(int));
this->queue.Put(tmp);
//this->queue.Put((void *)((unsigned long long)element));
}
int dequeue()
{
void *tmp = this->queue.Get();
int ret = *(int *)tmp;
free(tmp);
return ret;
// return (int)reinterpret_cast<unsigned long long>(this->queue.Get());
}
int size()
{
return this->queue.count();
//return this->res_cnt;
}
private:
int res_max;
int res_cnt;
grpc_core::InfLenFIFOQueue queue;
};
#include "workflow_list.h"
struct __poller_queue_waiter
{
struct list_head list;
int res;
pthread_cond_t cond;
};
#define WAITER_INITIALIZER (-1)
class MpmcQueue
{
public:
MpmcQueue(int capacity)
{
this->res_max = capacity;
this->res_cnt = 0;
INIT_LIST_HEAD(&this->waiter_list);
}
void enqueue(int element)
{
return this->enqueue_basic(element);
//return this->enqueue_cond_signal_outside(element);
}
int dequeue()
{
return this->dequeue_basic();
//return this->dequeue_cond_signal_outside();
}
int size()
{
return this->res_cnt;
}
// signal outside the mutex; list_add_tail;
void enqueue_basic(int element);
int dequeue_basic();
// signal outside the mutex; cond; list_add_head;
void enqueue_cond_signal_outside(int element);
int dequeue_cond_signal_outside();
private:
int res_max;
int res_cnt;
struct list_head waiter_list;
List<int> res_list;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
};
//////// impl
inline void MpmcQueue::enqueue_basic(int element)
{
struct __poller_queue_waiter *waiter = NULL;
struct list_head *pos;
pthread_mutex_lock(&this->mutex);
while (this->res_cnt > this->res_max - 1)
pthread_cond_wait(&this->cond, &this->mutex);
this->res_list.add_tail(element);
this->res_cnt++;
pos = this->waiter_list.next;
if (pos != &this->waiter_list)
{
waiter = list_entry(pos, struct __poller_queue_waiter, list);
list_del(pos);
pthread_cond_signal(&waiter->cond);
//waiter->cond = NULL;
waiter->list.next = NULL;
}
pthread_mutex_unlock(&this->mutex);
// if (waiter)
// pthread_cond_signal(&waiter->cond);
}
inline int MpmcQueue::dequeue_basic()
{
int ret;
pthread_mutex_lock(&this->mutex);
if (this->res_cnt == 0)
{
struct __poller_queue_waiter waiter = {
.list = { },
.res = WAITER_INITIALIZER,
.cond = PTHREAD_COND_INITIALIZER
};
do
{
list_add_tail(&waiter.list, &this->waiter_list);
do
pthread_cond_wait(&cond, &this->mutex);
while (waiter.list.next); // ...
} while (this->res_cnt == 0); // && !queue->nonblock)
}
if (this->res_cnt != 0)
{
this->res_list.get_head(ret);
this->res_cnt--;
// if (this->res_cnt-- > this->res_max - 1) // this is wrong
pthread_cond_signal(&this->cond);
}
pthread_mutex_unlock(&this->mutex);
return ret;
}
inline void MpmcQueue::enqueue_cond_signal_outside(int element)
{
struct __poller_queue_waiter *waiter;
struct list_head *pos;
pthread_mutex_lock(&this->mutex);
while (this->res_cnt > this->res_max - 1)
pthread_cond_wait(&this->cond, &this->mutex);
pos = this->waiter_list.next;
if (pos != &this->waiter_list)
{
waiter = list_entry(pos, struct __poller_queue_waiter, list);
list_del(pos);
waiter->res = 1412;
// pthread_cond_signal(&waiter->cond);
}
else
{
this->res_list.add_tail(element);
this->res_cnt++;
}
pthread_mutex_unlock(&this->mutex);
pthread_cond_signal(&waiter->cond);
}
inline int MpmcQueue::dequeue_cond_signal_outside()
{
int ret;
pthread_mutex_lock(&this->mutex);
if (this->res_cnt != 0)
{
this->res_list.get_head(ret);
this->res_cnt--;
}
else
{
struct __poller_queue_waiter waiter = {
.list = { },
.res = WAITER_INITIALIZER,
.cond = PTHREAD_COND_INITIALIZER
};
list_add_tail(&waiter.list, &this->waiter_list);
do
pthread_cond_wait(&waiter.cond, &this->mutex);
while (waiter.res == WAITER_INITIALIZER);
}
pthread_mutex_unlock(&this->mutex);
pthread_cond_signal(&this->cond);
return ret;
}
#endif