Skip to content

Commit

Permalink
8294399: (ch) Refactor some methods out of sun.nio.ch.UnixFileDispatc…
Browse files Browse the repository at this point in the history
…herImpl

Reviewed-by: alanb
  • Loading branch information
Brian Burkhalter committed Oct 27, 2022
1 parent 628820f commit d667895
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 73 deletions.
Expand Up @@ -58,6 +58,7 @@ static native long transferFrom0(FileDescriptor src, FileDescriptor dst,
static native void init0();

static {
IOUtil.load();
init0();
}
}
13 changes: 8 additions & 5 deletions src/java.base/unix/classes/sun/nio/ch/DatagramDispatcher.java
@@ -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;
}
18 changes: 12 additions & 6 deletions src/java.base/unix/classes/sun/nio/ch/SocketDispatcher.java
@@ -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
@@ -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();
}
}
19 changes: 0 additions & 19 deletions src/java.base/unix/classes/sun/nio/ch/UnixFileDispatcherImpl.java
Expand Up @@ -39,7 +39,6 @@ class UnixFileDispatcherImpl extends FileDispatcher {

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

private static final JavaIOFileDescriptorAccess fdAccess =
Expand Down Expand Up @@ -108,14 +107,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 @@ -211,14 +202,6 @@ 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 long allocationGranularity0();
Expand All @@ -230,6 +213,4 @@ static native long map0(FileDescriptor fd, int prot, long position,
static native int unmap0(long address, long length);

static native int setDirect0(FileDescriptor fd) throws IOException;

static native void init();
}
11 changes: 10 additions & 1 deletion src/java.base/unix/native/libnio/ch/DatagramDispatcher.c
@@ -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");
}
}
21 changes: 20 additions & 1 deletion src/java.base/unix/native/libnio/ch/SocketDispatcher.c
@@ -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
@@ -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");
}
}
41 changes: 0 additions & 41 deletions src/java.base/unix/native/libnio/ch/UnixFileDispatcherImpl.c
Expand Up @@ -52,21 +52,6 @@
#include "java_lang_Long.h"
#include <assert.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_UnixFileDispatcherImpl_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_UnixFileDispatcherImpl_read0(JNIEnv *env, jclass clazz,
jobject fdo, jlong address, jint len)
Expand Down Expand Up @@ -257,7 +242,6 @@ Java_sun_nio_ch_UnixFileDispatcherImpl_release0(JNIEnv *env, jobject this,
}
}


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

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

JNIEXPORT void JNICALL
Java_sun_nio_ch_UnixFileDispatcherImpl_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_UnixFileDispatcherImpl_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_UnixFileDispatcherImpl_closeIntFD(JNIEnv *env, jclass clazz, jint fd)
{
Expand Down

1 comment on commit d667895

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.