Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/windows/classes/sun/nio/ch/WEPoll.java
41139 views
1
/*
2
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.nio.ch;
27
28
import java.io.IOException;
29
import jdk.internal.misc.Unsafe;
30
31
/**
32
* Provides access to wepoll.
33
*/
34
class WEPoll {
35
private static final Unsafe UNSAFE = Unsafe.getUnsafe();
36
private static final int ADDRESS_SIZE = UNSAFE.addressSize();
37
38
private WEPoll() { }
39
40
/**
41
* typedef union epoll_data {
42
* void *ptr;
43
* int fd;
44
* uint32_t u32;
45
* uint64_t u64;
46
* SOCKET sock; // Windows specific
47
* HANDLE hnd; // Windows specific
48
* } epoll_data_t;
49
*
50
* struct epoll_event {
51
* uint32_t events;
52
* epoll_data_t data;
53
* }
54
*/
55
private static final int SIZEOF_EPOLLEVENT = eventSize();
56
private static final int OFFSETOF_EVENTS = eventsOffset();
57
private static final int OFFSETOF_SOCK = dataOffset();
58
59
// opcodes
60
static final int EPOLL_CTL_ADD = 1;
61
static final int EPOLL_CTL_MOD = 2;
62
static final int EPOLL_CTL_DEL = 3;
63
64
// events
65
static final int EPOLLIN = (1 << 0);
66
static final int EPOLLPRI = (1 << 1);
67
static final int EPOLLOUT = (1 << 2);
68
static final int EPOLLERR = (1 << 3);
69
static final int EPOLLHUP = (1 << 4);
70
71
// flags
72
static final int EPOLLONESHOT = (1 << 31);
73
74
/**
75
* Allocates a poll array to handle up to {@code count} events.
76
*/
77
static long allocatePollArray(int count) {
78
long size = (long) count * SIZEOF_EPOLLEVENT;
79
long base = UNSAFE.allocateMemory(size);
80
UNSAFE.setMemory(base, size, (byte) 0);
81
return base;
82
}
83
84
/**
85
* Free a poll array
86
*/
87
static void freePollArray(long address) {
88
UNSAFE.freeMemory(address);
89
}
90
91
/**
92
* Returns event[i];
93
*/
94
static long getEvent(long address, int i) {
95
return address + (SIZEOF_EPOLLEVENT*i);
96
}
97
98
/**
99
* Returns event->data.socket
100
*/
101
static long getSocket(long eventAddress) {
102
if (ADDRESS_SIZE == 8) {
103
return UNSAFE.getLong(eventAddress + OFFSETOF_SOCK);
104
} else {
105
return UNSAFE.getInt(eventAddress + OFFSETOF_SOCK);
106
}
107
}
108
109
/**
110
* Return event->data.socket as an int file descriptor
111
*/
112
static int getDescriptor(long eventAddress) {
113
long s = getSocket(eventAddress);
114
int fd = (int) s;
115
assert ((long) fd) == s;
116
return fd;
117
}
118
119
/**
120
* Returns event->events
121
*/
122
static int getEvents(long eventAddress) {
123
return UNSAFE.getInt(eventAddress + OFFSETOF_EVENTS);
124
}
125
126
// -- Native methods --
127
128
private static native int eventSize();
129
130
private static native int eventsOffset();
131
132
private static native int dataOffset();
133
134
static native long create() throws IOException;
135
136
static native int ctl(long h, int opcode, long s, int events);
137
138
static native int wait(long h, long pollAddress, int numfds, int timeout)
139
throws IOException;
140
141
static native void close(long h);
142
143
static {
144
IOUtil.load();
145
}
146
}
147
148