View Javadoc
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  /**
21   * A buffer for completion events.
22   */
23  final class CompletionBuffer {
24      private final CompletionCallback callback = this::add;
25      // long[(tail + 1) % capacity] holds res and flags (packed as long) and long[(tail + 2) % capacity] the udata.
26      private final long[] array;
27      private final int capacity;
28      private final int mask;
29      private final long tombstone;
30      private int size;
31      private int head;
32      private int tail = -1;
33  
34      CompletionBuffer(int numCompletions, long tombstone) {
35          capacity = MathUtil.findNextPositivePowerOfTwo(numCompletions);
36          array = new long[capacity];
37          mask = capacity - 1;
38          for (int i = 0; i < capacity; i += 2) {
39              array[i] = tombstone;
40          }
41          this.tombstone = tombstone;
42      }
43  
44      // Package-private for testing
45      boolean add(int res, int flags, long udata) {
46          if (udata == tombstone) {
47              throw new IllegalStateException("udata can't be the same as the tombstone");
48          }
49          // Pack res and flag into the first slot.
50          array[combinedIdx(tail + 1)] = (((long) res) << 32) | (flags & 0xffffffffL);
51          array[udataIdx(tail + 1)] = udata;
52  
53          tail += 2;
54          size += 2;
55          return size < capacity;
56      }
57  
58      /**
59       * Drain completions from the given {@link CompletionQueue}.
60       *
61       * @param queue the queue to drain from.
62       * @return      {@code true} if the whole queue was drained, {@code false} otherwise.
63       */
64      boolean drain(CompletionQueue queue) {
65          if (size == capacity) {
66              // The buffer is already full.
67              return false;
68          }
69          queue.process(callback);
70          return !queue.hasCompletions();
71      }
72  
73      /**
74       * Process buffered completions via the given {@link CompletionCallback}.
75       *
76       * @param callback  the callback to use.
77       * @return          the number of processed completions.
78       */
79      int processNow(CompletionCallback callback) {
80          int i = 0;
81  
82          boolean processing = true;
83          do {
84              if (size == 0) {
85                  break;
86              }
87              long combined = array[combinedIdx(head)];
88              long udata = array[udataIdx(head)];
89  
90              head += 2;
91              size -= 2;
92              // Skipping over tombstones
93              if (udata != tombstone) {
94                  processing = handle(callback, combined, udata);
95                  i++;
96              }
97          } while (processing);
98          return i;
99      }
100 
101     boolean processOneNow(CompletionCallback callback, long udata) {
102         // We basically just scan over the whole array (in reverse order as it is most likely that the completion
103         // that belongs to the udata was submitted last), if this turns out to be a performance problem
104         // (we actually don't expect too many outstanding completions) it's possible to be a bit smarter.
105         //
106         // We could make the udata generation shared across channels and always increase it. Then we could use
107         // a binarySearch to find the right completion to handle. This only downside would be that this will not
108         // work once we overflow so we would need to handle this somehow.
109         int idx = tail - 1;
110 
111         for (int i = 0; i < size; i += 2, idx -= 2) {
112             int udataIdx = udataIdx(idx);
113             long data = array[udataIdx];
114             if (udata != data) {
115                 continue;
116             }
117             long combined = array[combinedIdx(idx)];
118             array[udataIdx] = tombstone;
119             return handle(callback, combined, udata);
120         }
121         return false;
122     }
123 
124     private int combinedIdx(int idx) {
125         return idx & mask;
126     }
127 
128     private int udataIdx(int idx) {
129         return (idx + 1) & mask;
130     }
131 
132     private static boolean handle(CompletionCallback callback, long combined, long udata) {
133         int res = (int) (combined >> 32);
134         int flags = (int) combined;
135         return callback.handle(res, flags, udata);
136     }
137 }