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 * 2);
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      private boolean add(int res, int flags, long udata) {
45          if (udata == tombstone) {
46              throw new IllegalStateException("udata can't be the same as the tombstone");
47          }
48          // Pack res and flag into the first slot.
49          array[combinedIdx(tail + 1)] = (((long) res) << 32) | (flags & 0xffffffffL);
50          array[udataIdx(tail + 1)] = udata;
51  
52          tail += 2;
53          size += 2;
54          return size < capacity;
55      }
56  
57      /**
58       * Drain completions from the given {@link CompletionQueue}.
59       *
60       * @param queue the queue to drain from.
61       * @return      {@code true} if the whole queue was drained, {@code false} otherwise.
62       */
63      boolean drain(CompletionQueue queue) {
64          if (size == capacity) {
65              // The buffer is already full.
66              return false;
67          }
68          queue.process(callback);
69          return !queue.hasCompletions();
70      }
71  
72      /**
73       * Process buffered completions via the given {@link CompletionCallback}.
74       *
75       * @param callback  the callback to use.
76       * @return          the number of processed completions.
77       */
78      int processNow(CompletionCallback callback) {
79          int i = 0;
80  
81          boolean processing = true;
82          do {
83              if (size == 0) {
84                  break;
85              }
86              long combined = array[combinedIdx(head)];
87              long udata = array[udataIdx(head)];
88  
89              head += 2;
90              size -= 2;
91              // Skipping over tombstones
92              if (udata != tombstone) {
93                  processing = handle(callback, combined, udata);
94                  i++;
95              }
96          } while (processing);
97          return i;
98      }
99  
100     private int combinedIdx(int idx) {
101         return idx & mask;
102     }
103 
104     private int udataIdx(int idx) {
105         return (idx + 1) & mask;
106     }
107 
108     private static boolean handle(CompletionCallback callback, long combined, long udata) {
109         int res = (int) (combined >> 32);
110         int flags = (int) combined;
111         return callback.handle(res, flags, udata);
112     }
113 }