jdk/src/java.base/unix/native/libnio/ch/NativeThread.c
changeset 25859 3317bb8137f4
parent 22561 5ee70f9edbb7
child 34880 00d8bef75229
equal deleted inserted replaced
25858:836adbf7a2cd 25859:3317bb8137f4
       
     1 /*
       
     2  * Copyright (c) 2002, 2006, 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 #include <sys/types.h>
       
    27 #include <string.h>
       
    28 #include "jni.h"
       
    29 #include "jni_util.h"
       
    30 #include "jvm.h"
       
    31 #include "jlong.h"
       
    32 #include "sun_nio_ch_NativeThread.h"
       
    33 #include "nio_util.h"
       
    34 
       
    35 #ifdef __linux__
       
    36   #include <pthread.h>
       
    37   #include <sys/signal.h>
       
    38   /* Also defined in net/linux_close.c */
       
    39   #define INTERRUPT_SIGNAL (__SIGRTMAX - 2)
       
    40 #elif __solaris__
       
    41   #include <thread.h>
       
    42   #include <signal.h>
       
    43   #define INTERRUPT_SIGNAL (SIGRTMAX - 2)
       
    44 #elif _ALLBSD_SOURCE
       
    45   #include <pthread.h>
       
    46   #include <signal.h>
       
    47   /* Also defined in net/bsd_close.c */
       
    48   #define INTERRUPT_SIGNAL SIGIO
       
    49 #else
       
    50   #error "missing platform-specific definition here"
       
    51 #endif
       
    52 
       
    53 static void
       
    54 nullHandler(int sig)
       
    55 {
       
    56 }
       
    57 
       
    58 JNIEXPORT void JNICALL
       
    59 Java_sun_nio_ch_NativeThread_init(JNIEnv *env, jclass cl)
       
    60 {
       
    61     /* Install the null handler for INTERRUPT_SIGNAL.  This might overwrite the
       
    62      * handler previously installed by java/net/linux_close.c, but that's okay
       
    63      * since neither handler actually does anything.  We install our own
       
    64      * handler here simply out of paranoia; ultimately the two mechanisms
       
    65      * should somehow be unified, perhaps within the VM.
       
    66      */
       
    67 
       
    68     sigset_t ss;
       
    69     struct sigaction sa, osa;
       
    70     sa.sa_handler = nullHandler;
       
    71     sa.sa_flags = 0;
       
    72     sigemptyset(&sa.sa_mask);
       
    73     if (sigaction(INTERRUPT_SIGNAL, &sa, &osa) < 0)
       
    74         JNU_ThrowIOExceptionWithLastError(env, "sigaction");
       
    75 }
       
    76 
       
    77 JNIEXPORT jlong JNICALL
       
    78 Java_sun_nio_ch_NativeThread_current(JNIEnv *env, jclass cl)
       
    79 {
       
    80 #ifdef __solaris__
       
    81     return (jlong)thr_self();
       
    82 #else
       
    83     return (jlong)pthread_self();
       
    84 #endif
       
    85 }
       
    86 
       
    87 JNIEXPORT void JNICALL
       
    88 Java_sun_nio_ch_NativeThread_signal(JNIEnv *env, jclass cl, jlong thread)
       
    89 {
       
    90     int ret;
       
    91 #ifdef __solaris__
       
    92     ret = thr_kill((thread_t)thread, INTERRUPT_SIGNAL);
       
    93 #else
       
    94     ret = pthread_kill((pthread_t)thread, INTERRUPT_SIGNAL);
       
    95 #endif
       
    96     if (ret != 0)
       
    97         JNU_ThrowIOExceptionWithLastError(env, "Thread signal failed");
       
    98 }