Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/sun/nio/ch/Iocp.c
32288 views
/*1* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <windows.h>2627#include "jni.h"28#include "jni_util.h"29#include "jlong.h"30#include "nio.h"31#include "nio_util.h"3233#include "sun_nio_ch_Iocp.h"343536static jfieldID completionStatus_error;37static jfieldID completionStatus_bytesTransferred;38static jfieldID completionStatus_completionKey;39static jfieldID completionStatus_overlapped;404142JNIEXPORT void JNICALL43Java_sun_nio_ch_Iocp_initIDs(JNIEnv* env, jclass this)44{45jclass clazz;4647clazz = (*env)->FindClass(env, "sun/nio/ch/Iocp$CompletionStatus");48CHECK_NULL(clazz);49completionStatus_error = (*env)->GetFieldID(env, clazz, "error", "I");50CHECK_NULL(completionStatus_error);51completionStatus_bytesTransferred = (*env)->GetFieldID(env, clazz, "bytesTransferred", "I");52CHECK_NULL(completionStatus_bytesTransferred);53completionStatus_completionKey = (*env)->GetFieldID(env, clazz, "completionKey", "I");54CHECK_NULL(completionStatus_completionKey);55completionStatus_overlapped = (*env)->GetFieldID(env, clazz, "overlapped", "J");56CHECK_NULL(completionStatus_overlapped);57}5859JNIEXPORT jint JNICALL60Java_sun_nio_ch_Iocp_osMajorVersion(JNIEnv* env, jclass this)61{62OSVERSIONINFOEX ver;63ver.dwOSVersionInfoSize = sizeof(ver);64GetVersionEx((OSVERSIONINFO *) &ver);65return (ver.dwPlatformId == VER_PLATFORM_WIN32_NT) ?66(jint)(ver.dwMajorVersion) : (jint)0;67}6869JNIEXPORT jlong JNICALL70Java_sun_nio_ch_Iocp_createIoCompletionPort(JNIEnv* env, jclass this,71jlong handle, jlong existingPort, jint completionKey, jint concurrency)72{73ULONG_PTR ck = completionKey;74HANDLE port = CreateIoCompletionPort((HANDLE)jlong_to_ptr(handle),75(HANDLE)jlong_to_ptr(existingPort),76ck,77(DWORD)concurrency);78if (port == NULL) {79JNU_ThrowIOExceptionWithLastError(env, "CreateIoCompletionPort failed");80}81return ptr_to_jlong(port);82}8384JNIEXPORT void JNICALL85Java_sun_nio_ch_Iocp_close0(JNIEnv* env, jclass this,86jlong handle)87{88HANDLE h = (HANDLE)jlong_to_ptr(handle);89CloseHandle(h);90}919293JNIEXPORT void JNICALL94Java_sun_nio_ch_Iocp_getQueuedCompletionStatus(JNIEnv* env, jclass this,95jlong completionPort, jobject obj)96{97DWORD bytesTransferred;98ULONG_PTR completionKey;99OVERLAPPED *lpOverlapped;100BOOL res;101102res = GetQueuedCompletionStatus((HANDLE)jlong_to_ptr(completionPort),103&bytesTransferred,104&completionKey,105&lpOverlapped,106INFINITE);107if (res == 0 && lpOverlapped == NULL) {108JNU_ThrowIOExceptionWithLastError(env, "GetQueuedCompletionStatus failed");109} else {110DWORD ioResult = (res == 0) ? GetLastError() : 0;111(*env)->SetIntField(env, obj, completionStatus_error, ioResult);112(*env)->SetIntField(env, obj, completionStatus_bytesTransferred,113(jint)bytesTransferred);114(*env)->SetIntField(env, obj, completionStatus_completionKey,115(jint)completionKey);116(*env)->SetLongField(env, obj, completionStatus_overlapped,117ptr_to_jlong(lpOverlapped));118119}120}121122JNIEXPORT void JNICALL123Java_sun_nio_ch_Iocp_postQueuedCompletionStatus(JNIEnv* env, jclass this,124jlong completionPort, jint completionKey)125{126BOOL res;127128res = PostQueuedCompletionStatus((HANDLE)jlong_to_ptr(completionPort),129(DWORD)0,130(DWORD)completionKey,131NULL);132if (res == 0) {133JNU_ThrowIOExceptionWithLastError(env, "PostQueuedCompletionStatus");134}135}136137JNIEXPORT jstring JNICALL138Java_sun_nio_ch_Iocp_getErrorMessage(JNIEnv* env, jclass this, jint errorCode)139{140WCHAR message[255];141142DWORD len = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM,143NULL,144(DWORD)errorCode,1450,146&message[0],147255,148NULL);149150151if (len == 0) {152return NULL;153} else {154return (*env)->NewString(env, (const jchar *)message, (jsize)wcslen(message));155}156}157158159