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
222
use std::cell::UnsafeCell;
use std::collections::VecDeque;
use std::ptr;
use std::sync::atomic::{AtomicIsize, Ordering};
use std::sync::Arc;
use std::thread::{self, ThreadId};
use crate::error::{Error, Result};
use crate::grpc_sys::{self, gpr_clock_type, grpc_completion_queue};
use crate::task::UnfinishedWork;
pub use crate::grpc_sys::grpc_completion_type as EventType;
pub use crate::grpc_sys::grpc_event as Event;
pub struct CompletionQueueHandle {
cq: *mut grpc_completion_queue,
ref_cnt: AtomicIsize,
}
unsafe impl Sync for CompletionQueueHandle {}
unsafe impl Send for CompletionQueueHandle {}
impl CompletionQueueHandle {
pub fn new() -> CompletionQueueHandle {
CompletionQueueHandle {
cq: unsafe { grpc_sys::grpc_completion_queue_create_for_next(ptr::null_mut()) },
ref_cnt: AtomicIsize::new(1),
}
}
fn add_ref(&self) -> Result<()> {
let mut cnt = self.ref_cnt.load(Ordering::SeqCst);
loop {
if cnt <= 0 {
return Err(Error::QueueShutdown);
}
let new_cnt = cnt + 1;
match self.ref_cnt.compare_exchange_weak(
cnt,
new_cnt,
Ordering::SeqCst,
Ordering::SeqCst,
) {
Ok(_) => return Ok(()),
Err(c) => cnt = c,
}
}
}
fn unref(&self) {
let mut cnt = self.ref_cnt.load(Ordering::SeqCst);
let shutdown = loop {
let new_cnt = cnt - cnt.signum();
match self.ref_cnt.compare_exchange_weak(
cnt,
new_cnt,
Ordering::SeqCst,
Ordering::SeqCst,
) {
Ok(_) => break new_cnt == 0,
Err(c) => cnt = c,
}
};
if shutdown {
unsafe {
grpc_sys::grpc_completion_queue_shutdown(self.cq);
}
}
}
fn shutdown(&self) {
let mut cnt = self.ref_cnt.load(Ordering::SeqCst);
let shutdown = loop {
if cnt <= 0 {
return;
}
let new_cnt = -cnt + 1;
match self.ref_cnt.compare_exchange_weak(
cnt,
new_cnt,
Ordering::SeqCst,
Ordering::SeqCst,
) {
Ok(_) => break new_cnt == 0,
Err(c) => cnt = c,
}
};
if shutdown {
unsafe {
grpc_sys::grpc_completion_queue_shutdown(self.cq);
}
}
}
}
impl Drop for CompletionQueueHandle {
fn drop(&mut self) {
unsafe { grpc_sys::grpc_completion_queue_destroy(self.cq) }
}
}
pub struct CompletionQueueRef<'a> {
queue: &'a CompletionQueue,
}
impl<'a> CompletionQueueRef<'a> {
pub fn as_ptr(&self) -> *mut grpc_completion_queue {
self.queue.handle.cq
}
}
impl<'a> Drop for CompletionQueueRef<'a> {
fn drop(&mut self) {
self.queue.handle.unref();
}
}
pub struct WorkQueue {
id: ThreadId,
pending_work: UnsafeCell<VecDeque<UnfinishedWork>>,
}
unsafe impl Sync for WorkQueue {}
unsafe impl Send for WorkQueue {}
const QUEUE_CAPACITY: usize = 4096;
impl WorkQueue {
pub fn new() -> WorkQueue {
WorkQueue {
id: std::thread::current().id(),
pending_work: UnsafeCell::new(VecDeque::with_capacity(QUEUE_CAPACITY)),
}
}
pub fn push_work(&self, work: UnfinishedWork) -> Option<UnfinishedWork> {
if self.id == thread::current().id() {
unsafe { &mut *self.pending_work.get() }.push_back(work);
None
} else {
Some(work)
}
}
pub unsafe fn pop_work(&self) -> Option<UnfinishedWork> {
let queue = &mut *self.pending_work.get();
if queue.capacity() > QUEUE_CAPACITY && queue.len() < queue.capacity() / 2 {
queue.shrink_to_fit();
}
{ &mut *self.pending_work.get() }.pop_back()
}
}
#[derive(Clone)]
pub struct CompletionQueue {
handle: Arc<CompletionQueueHandle>,
pub(crate) worker: Arc<WorkQueue>,
}
impl CompletionQueue {
pub fn new(handle: Arc<CompletionQueueHandle>, worker: Arc<WorkQueue>) -> CompletionQueue {
CompletionQueue { handle, worker }
}
pub fn next(&self) -> Event {
unsafe {
let inf = grpc_sys::gpr_inf_future(gpr_clock_type::GPR_CLOCK_REALTIME);
grpc_sys::grpc_completion_queue_next(self.handle.cq, inf, ptr::null_mut())
}
}
pub fn borrow(&self) -> Result<CompletionQueueRef<'_>> {
self.handle.add_ref()?;
Ok(CompletionQueueRef { queue: self })
}
pub fn shutdown(&self) {
self.handle.shutdown()
}
pub fn worker_id(&self) -> ThreadId {
self.worker.id
}
}