1 /*
2 * Copyright 2024 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.channel.unix.Errors;
19
20 import java.io.UncheckedIOException;
21
22 final class RingBuffer {
23 private final SubmissionQueue ioUringSubmissionQueue;
24 private final CompletionQueue ioUringCompletionQueue;
25 private final int features;
26 private boolean closed;
27 private boolean enabled;
28
29 RingBuffer(SubmissionQueue ioUringSubmissionQueue,
30 CompletionQueue ioUringCompletionQueue, int features) {
31 this.ioUringSubmissionQueue = ioUringSubmissionQueue;
32 this.ioUringCompletionQueue = ioUringCompletionQueue;
33 this.features = features;
34 }
35
36 /**
37 * Enable ring. This method must be called from the same method that will call {@link SubmissionQueue#submit()} and
38 * {@link SubmissionQueue#submitAndWait()}.
39 */
40 void enable() {
41 // We create our ring in disabled mode and so need to enable it first. This method might be called more than
42 // once (e.g. when an EventLoop is resumed), but the ring can only be enabled once as enabling an already
43 // enabled ring fails.
44 if (!enabled) {
45 int res = Native.ioUringRegisterEnableRings(fd());
46 if (res < 0) {
47 // The ring stays disabled in this case and so every io_uring_enter(...) call would fail later on.
48 throw new UncheckedIOException(Errors.newIOException("io_uring_register", res));
49 }
50 enabled = true;
51 }
52 // Now also register the ring filedescriptor itself. This needs to happen in the same thread
53 // that will also call the io_uring_enter(...)
54 ioUringSubmissionQueue.tryRegisterRingFd();
55 }
56
57 int fd() {
58 return ioUringCompletionQueue.ringFd;
59 }
60
61 int features() {
62 return features;
63 }
64
65 SubmissionQueue ioUringSubmissionQueue() {
66 return this.ioUringSubmissionQueue;
67 }
68
69 CompletionQueue ioUringCompletionQueue() {
70 return this.ioUringCompletionQueue;
71 }
72
73 void close() {
74 if (closed) {
75 return;
76 }
77 closed = true;
78 ioUringSubmissionQueue.close();
79 ioUringCompletionQueue.close();
80 Native.ioUringExit(
81 ioUringSubmissionQueue.submissionQueueArrayAddress(),
82 ioUringSubmissionQueue.ringEntries,
83 ioUringSubmissionQueue.ringAddress,
84 ioUringSubmissionQueue.ringSize,
85 ioUringCompletionQueue.ringAddress,
86 ioUringCompletionQueue.ringSize,
87 ioUringSubmissionQueue.ringFd,
88 ioUringSubmissionQueue.enterRingFd);
89 }
90 }