Skip to content

8294399: (ch) Refactor some methods out of sun.nio.ch.UnixFileDispatcherImpl #3631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/java.base/unix/classes/sun/nio/ch/DatagramDispatcher.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -33,7 +33,7 @@
* for read and write operations.
*/

class DatagramDispatcher extends NativeDispatcher {
class DatagramDispatcher extends UnixDispatcher {

static {
IOUtil.load();
Expand All @@ -56,15 +56,15 @@ long writev(FileDescriptor fd, long address, int len) throws IOException {
}

void close(FileDescriptor fd) throws IOException {
FileDispatcherImpl.close0(fd);
close0(fd);
}

void preClose(FileDescriptor fd) throws IOException {
FileDispatcherImpl.preClose0(fd);
preClose0(fd);
}

void dup(FileDescriptor fd1, FileDescriptor fd2) throws IOException {
FileDispatcherImpl.dup0(fd1, fd2);
dup0(fd1, fd2);
}

static native int read0(FileDescriptor fd, long address, int len)
Expand All @@ -78,4 +78,7 @@ static native int write0(FileDescriptor fd, long address, int len)

static native long writev0(FileDescriptor fd, long address, int len)
throws IOException;

static native void dup0(FileDescriptor fd1, FileDescriptor fd2)
throws IOException;
}
20 changes: 0 additions & 20 deletions src/java.base/unix/classes/sun/nio/ch/FileDispatcherImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class FileDispatcherImpl extends FileDispatcher {

static {
IOUtil.load();
init();
}

private static final JavaIOFileDescriptorAccess fdAccess =
Expand Down Expand Up @@ -104,14 +103,6 @@ void close(FileDescriptor fd) throws IOException {
fdAccess.close(fd);
}

void preClose(FileDescriptor fd) throws IOException {
preClose0(fd);
}

void dup(FileDescriptor fd1, FileDescriptor fd2) throws IOException {
dup0(fd1, fd2);
}

FileDescriptor duplicateForMapping(FileDescriptor fd) {
// file descriptor not required for mapping operations; okay
// to return invalid file descriptor.
Expand Down Expand Up @@ -178,20 +169,9 @@ static native int lock0(FileDescriptor fd, boolean blocking, long pos,
static native void release0(FileDescriptor fd, long pos, long size)
throws IOException;

// Shared with SocketDispatcher and DatagramDispatcher but
// NOT used by FileDispatcherImpl
static native void close0(FileDescriptor fd) throws IOException;

static native void preClose0(FileDescriptor fd) throws IOException;

static native void dup0(FileDescriptor fd1, FileDescriptor fd2) throws IOException;

static native void closeIntFD(int fd) throws IOException;

static native boolean canTransferToFromOverlappedMap0();

static native int setDirect0(FileDescriptor fd) throws IOException;

static native void init();

}
18 changes: 12 additions & 6 deletions src/java.base/unix/classes/sun/nio/ch/SocketDispatcher.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -33,7 +33,7 @@
* for read and write operations.
*/

class SocketDispatcher extends NativeDispatcher {
class SocketDispatcher extends UnixDispatcher {
SocketDispatcher() { }

/**
Expand All @@ -59,19 +59,19 @@ long readv(FileDescriptor fd, long address, int len) throws IOException {
}

int write(FileDescriptor fd, long address, int len) throws IOException {
return FileDispatcherImpl.write0(fd, address, len);
return write0(fd, address, len);
}

long writev(FileDescriptor fd, long address, int len) throws IOException {
return FileDispatcherImpl.writev0(fd, address, len);
return writev0(fd, address, len);
}

void close(FileDescriptor fd) throws IOException {
FileDispatcherImpl.close0(fd);
close0(fd);
}

void preClose(FileDescriptor fd) throws IOException {
FileDispatcherImpl.preClose0(fd);
preClose0(fd);
}

// -- Native methods --
Expand All @@ -82,6 +82,12 @@ private static native int read0(FileDescriptor fd, long address, int len)
private static native long readv0(FileDescriptor fd, long address, int len)
throws IOException;

static native int write0(FileDescriptor fd, long address, int len)
throws IOException;

static native long writev0(FileDescriptor fd, long address, int len)
throws IOException;

static {
IOUtil.load();
}
Expand Down
51 changes: 51 additions & 0 deletions src/java.base/unix/classes/sun/nio/ch/UnixDispatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package sun.nio.ch;

import java.io.FileDescriptor;
import java.io.IOException;

abstract class UnixDispatcher extends NativeDispatcher {

void close(FileDescriptor fd) throws IOException {
close0(fd);
}

void preClose(FileDescriptor fd) throws IOException {
preClose0(fd);
}

static native void close0(FileDescriptor fd) throws IOException;

static native void preClose0(FileDescriptor fd) throws IOException;

static native void init();

static {
IOUtil.load();
init();
}
}
11 changes: 10 additions & 1 deletion src/java.base/unix/native/libnio/ch/DatagramDispatcher.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -115,3 +115,12 @@ Java_sun_nio_ch_DatagramDispatcher_writev0(JNIEnv *env, jclass clazz,
}
return convertLongReturnVal(env, (jlong)result, JNI_FALSE);
}

JNIEXPORT void JNICALL
Java_sun_nio_ch_DatagramDispatcher_dup0(JNIEnv* env, jclass clazz,
jobject fdo1, jobject fdo2)
{
if (dup2(fdval(env, fdo1), fdval(env, fdo2)) < 0) {
JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
}
}
42 changes: 0 additions & 42 deletions src/java.base/unix/native/libnio/ch/FileDispatcherImpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,6 @@
#include "sun_nio_ch_FileDispatcherImpl.h"
#include "java_lang_Long.h"

static int preCloseFD = -1; /* File descriptor to which we dup other fd's
before closing them for real */


JNIEXPORT void JNICALL
Java_sun_nio_ch_FileDispatcherImpl_init(JNIEnv *env, jclass cl)
{
int sp[2];
if (socketpair(PF_UNIX, SOCK_STREAM, 0, sp) < 0) {
JNU_ThrowIOExceptionWithLastError(env, "socketpair failed");
return;
}
preCloseFD = sp[0];
close(sp[1]);
}

JNIEXPORT jint JNICALL
Java_sun_nio_ch_FileDispatcherImpl_read0(JNIEnv *env, jclass clazz,
jobject fdo, jlong address, jint len)
Expand Down Expand Up @@ -298,7 +282,6 @@ Java_sun_nio_ch_FileDispatcherImpl_release0(JNIEnv *env, jobject this,
}
}


static void closeFileDescriptor(JNIEnv *env, int fd) {
if (fd != -1) {
int result = close(fd);
Expand All @@ -307,31 +290,6 @@ static void closeFileDescriptor(JNIEnv *env, int fd) {
}
}

JNIEXPORT void JNICALL
Java_sun_nio_ch_FileDispatcherImpl_close0(JNIEnv *env, jclass clazz, jobject fdo)
{
jint fd = fdval(env, fdo);
closeFileDescriptor(env, fd);
}

JNIEXPORT void JNICALL
Java_sun_nio_ch_FileDispatcherImpl_preClose0(JNIEnv *env, jclass clazz, jobject fdo)
{
jint fd = fdval(env, fdo);
if (preCloseFD >= 0) {
if (dup2(preCloseFD, fd) < 0)
JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
}
}

JNIEXPORT void JNICALL
Java_sun_nio_ch_FileDispatcherImpl_dup0(JNIEnv *env, jobject this, jobject fdo1, jobject fdo2)
{
if (dup2(fdval(env, fdo1), fdval(env, fdo2)) < 0) {
JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
}
}

JNIEXPORT void JNICALL
Java_sun_nio_ch_FileDispatcherImpl_closeIntFD(JNIEnv *env, jclass clazz, jint fd)
{
Expand Down
21 changes: 20 additions & 1 deletion src/java.base/unix/native/libnio/ch/SocketDispatcher.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -63,3 +63,22 @@
return convertLongReturnVal(env, n, JNI_TRUE);
}
}

JNIEXPORT jint JNICALL
Java_sun_nio_ch_SocketDispatcher_write0(JNIEnv *env, jclass clazz,
jobject fdo, jlong address, jint len)
{
jint fd = fdval(env, fdo);
void *buf = (void *)jlong_to_ptr(address);

return convertReturnVal(env, write(fd, buf, len), JNI_FALSE);
}

JNIEXPORT jlong JNICALL
Java_sun_nio_ch_SocketDispatcher_writev0(JNIEnv *env, jclass clazz,
jobject fdo, jlong address, jint len)
{
jint fd = fdval(env, fdo);
struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
return convertLongReturnVal(env, writev(fd, iov, len), JNI_FALSE);
}
69 changes: 69 additions & 0 deletions src/java.base/unix/native/libnio/ch/UnixDispatcher.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

#include "nio.h"
#include "nio_util.h"

#include "sun_nio_ch_UnixDispatcher.h"

static int preCloseFD = -1; /* File descriptor to which we dup other fd's
before closing them for real */

static void closeFileDescriptor(JNIEnv *env, int fd) {
if (fd != -1) {
int result = close(fd);
if (result < 0)
JNU_ThrowIOExceptionWithLastError(env, "Close failed");
}
}

JNIEXPORT void JNICALL
Java_sun_nio_ch_UnixDispatcher_init(JNIEnv *env, jclass clazz)
{
int sp[2];
if (socketpair(PF_UNIX, SOCK_STREAM, 0, sp) < 0) {
JNU_ThrowIOExceptionWithLastError(env, "socketpair failed");
return;
}
preCloseFD = sp[0];
close(sp[1]);
}

JNIEXPORT void JNICALL
Java_sun_nio_ch_UnixDispatcher_close0(JNIEnv *env, jclass clazz, jobject fdo)
{
jint fd = fdval(env, fdo);
closeFileDescriptor(env, fd);
}

JNIEXPORT void JNICALL
Java_sun_nio_ch_UnixDispatcher_preClose0(JNIEnv *env, jclass clazz, jobject fdo)
{
jint fd = fdval(env, fdo);
if (preCloseFD >= 0) {
if (dup2(preCloseFD, fd) < 0)
JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
}
}