1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.uring;
17
18 import io.netty.util.internal.MathUtil;
19
20 import java.nio.ByteBuffer;
21
22
23
24
25 final class CompletionBuffer {
26 private final CompletionCallback callback = this::add;
27
28 private final long[] array;
29 private final int capacity;
30 private final int mask;
31 private final long tombstone;
32 private int size;
33 private int head;
34 private int tail = -1;
35
36 CompletionBuffer(int numCompletions, long tombstone) {
37 capacity = MathUtil.findNextPositivePowerOfTwo(numCompletions);
38 array = new long[capacity];
39 mask = capacity - 1;
40 for (int i = 0; i < capacity; i += 2) {
41 array[i] = tombstone;
42 }
43 this.tombstone = tombstone;
44 }
45
46
47 boolean add(int res, int flags, long udata) {
48 return add(res, flags, udata, null);
49 }
50
51 private boolean add(int res, int flags, long udata, ByteBuffer extraCqeData) {
52 if (udata == tombstone) {
53 throw new IllegalStateException("udata can't be the same as the tombstone");
54 }
55 if (extraCqeData != null) {
56 throw new IllegalArgumentException("extraCqeData not supported");
57 }
58
59 array[combinedIdx(tail + 1)] = (((long) res) << 32) | (flags & 0xffffffffL);
60 array[udataIdx(tail + 1)] = udata;
61
62 tail += 2;
63 size += 2;
64 return size < capacity;
65 }
66
67
68
69
70
71
72
73 boolean drain(CompletionQueue queue) {
74 if (size == capacity) {
75
76 return false;
77 }
78 queue.process(callback);
79 return !queue.hasCompletions();
80 }
81
82
83
84
85
86
87
88 int processNow(CompletionCallback callback) {
89 int i = 0;
90
91 boolean processing = true;
92 do {
93 if (size == 0) {
94 break;
95 }
96 long combined = array[combinedIdx(head)];
97 long udata = array[udataIdx(head)];
98
99 head += 2;
100 size -= 2;
101
102 if (udata != tombstone) {
103 processing = handle(callback, combined, udata);
104 i++;
105 }
106 } while (processing);
107 return i;
108 }
109
110 boolean processOneNow(CompletionCallback callback, long udata) {
111
112
113
114
115
116
117
118 int idx = tail - 1;
119
120 for (int i = 0; i < size; i += 2, idx -= 2) {
121 int udataIdx = udataIdx(idx);
122 long data = array[udataIdx];
123 if (udata != data) {
124 continue;
125 }
126 long combined = array[combinedIdx(idx)];
127 array[udataIdx] = tombstone;
128 return handle(callback, combined, udata);
129 }
130 return false;
131 }
132
133 private int combinedIdx(int idx) {
134 return idx & mask;
135 }
136
137 private int udataIdx(int idx) {
138 return (idx + 1) & mask;
139 }
140
141 private static boolean handle(CompletionCallback callback, long combined, long udata) {
142 int res = (int) (combined >> 32);
143 int flags = (int) combined;
144 return callback.handle(res, flags, udata, null);
145 }
146 }