1 /*
2 * Copyright 2025 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16 package io.netty.channel.uring;
17
18 import io.netty.util.internal.MathUtil;
19
20 import java.nio.ByteBuffer;
21
22 /**
23 * A buffer for completion events.
24 */
25 final class CompletionBuffer {
26 private final CompletionCallback callback = this::add;
27 // long[(tail + 1) % capacity] holds res and flags (packed as long) and long[(tail + 2) % capacity] the udata.
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 // Package-private for testing
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 // Pack res and flag into the first slot.
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 * Drain completions from the given {@link CompletionQueue}.
69 *
70 * @param queue the queue to drain from.
71 * @return {@code true} if the whole queue was drained, {@code false} otherwise.
72 */
73 boolean drain(CompletionQueue queue) {
74 if (size == capacity) {
75 // The buffer is already full.
76 return false;
77 }
78 queue.process(callback);
79 return !queue.hasCompletions();
80 }
81
82 /**
83 * Process buffered completions via the given {@link CompletionCallback}.
84 *
85 * @param callback the callback to use.
86 * @return the number of processed completions.
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 // Skipping over tombstones
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 // We basically just scan over the whole array (in reverse order as it is most likely that the completion
112 // that belongs to the udata was submitted last), if this turns out to be a performance problem
113 // (we actually don't expect too many outstanding completions) it's possible to be a bit smarter.
114 //
115 // We could make the udata generation shared across channels and always increase it. Then we could use
116 // a binarySearch to find the right completion to handle. This only downside would be that this will not
117 // work once we overflow so we would need to handle this somehow.
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 }