Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/transport/shmem/shmem_md.c
48482 views
/*1* Copyright (c) 1999, 2012, 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>26#include <errno.h>2728#include "shmem_md.h"29#include "sysShmem.h"30#include "shmemBase.h" /* for exitTransportWithError */3132/* Use THIS_FILE when it is available. */33#ifndef THIS_FILE34#define THIS_FILE __FILE__35#endif3637/*38* These functions are not completely universal. For now, they are used39* exclusively for Jbug's shared memory transport mechanism. They have40* been implemented on Win32 only so far, so the abstractions may not be correct41* yet.42*/4344static HANDLE memHandle = NULL;4546#ifdef DEBUG47#define sysAssert(expression) { \48if (!(expression)) { \49exitTransportWithError \50("\"%s\", line %d: assertion failure\n", \51THIS_FILE, __DATE__, __LINE__); \52} \53}54#else55#define sysAssert(expression) ((void) 0)56#endif5758int59sysSharedMemCreate(const char *name, int length,60sys_shmem_t *mem, void **buffer)61{62void *mappedMemory;63HANDLE memHandle;6465sysAssert(buffer);66sysAssert(name);67sysAssert(length > 0);6869memHandle =70CreateFileMapping(INVALID_HANDLE_VALUE, /* backed by page file */71NULL, /* no inheritance */72PAGE_READWRITE,730, length, /* hi, lo order of length */74name);75if (memHandle == NULL) {76return SYS_ERR;77} else if (GetLastError() == ERROR_ALREADY_EXISTS) {78/* If the call above didn't create it, consider it an error */79CloseHandle(memHandle);80memHandle = NULL;81return SYS_INUSE;82}8384mappedMemory =85MapViewOfFile(memHandle,86FILE_MAP_WRITE, /* read/write */870, 0, 0); /* map entire "file" */8889if (mappedMemory == NULL) {90CloseHandle(memHandle);91memHandle = NULL;92return SYS_ERR;93}9495*mem = memHandle;96*buffer = mappedMemory;97return SYS_OK;98}99100int101sysSharedMemOpen(const char *name, sys_shmem_t *mem, void **buffer)102{103void *mappedMemory;104HANDLE memHandle;105106sysAssert(name);107sysAssert(buffer);108109memHandle =110OpenFileMapping(FILE_MAP_WRITE, /* read/write */111FALSE, /* no inheritance */112name);113if (memHandle == NULL) {114return SYS_ERR;115}116117mappedMemory =118MapViewOfFile(memHandle,119FILE_MAP_WRITE, /* read/write */1200, 0, 0); /* map entire "file" */121122if (mappedMemory == NULL) {123CloseHandle(memHandle);124memHandle = NULL;125return SYS_ERR;126}127128*mem = memHandle;129*buffer = mappedMemory;130return SYS_OK;131}132133int134sysSharedMemClose(sys_shmem_t mem, void *buffer)135{136if (buffer != NULL) {137if (!UnmapViewOfFile(buffer)) {138return SYS_ERR;139}140}141142if (!CloseHandle(mem)) {143return SYS_ERR;144}145146return SYS_OK;147}148149int150sysIPMutexCreate(const char *name, sys_ipmutex_t *mutexPtr)151{152HANDLE mutex;153154sysAssert(mutexPtr);155sysAssert(name);156157mutex = CreateMutex(NULL, /* no inheritance */158FALSE, /* no initial owner */159name);160if (mutex == NULL) {161return SYS_ERR;162} else if (GetLastError() == ERROR_ALREADY_EXISTS) {163/* If the call above didn't create it, consider it an error */164CloseHandle(mutex);165return SYS_INUSE;166}167168*mutexPtr = mutex;169return SYS_OK;170}171172int173sysIPMutexOpen(const char *name, sys_ipmutex_t *mutexPtr)174{175HANDLE mutex;176177sysAssert(mutexPtr);178sysAssert(name);179180mutex = OpenMutex(SYNCHRONIZE, /* able to wait/release */181FALSE, /* no inheritance */182name);183if (mutex == NULL) {184return SYS_ERR;185}186187*mutexPtr = mutex;188return SYS_OK;189}190191int192sysIPMutexEnter(sys_ipmutex_t mutex, sys_event_t event)193{194HANDLE handles[2] = { mutex, event };195int count = event == NULL ? 1 : 2;196DWORD rc;197198sysAssert(mutex);199rc = WaitForMultipleObjects(count, handles,200FALSE, /* wait for either, not both */201INFINITE); /* infinite timeout */202return (rc == WAIT_OBJECT_0) ? SYS_OK : SYS_ERR;203}204205int206sysIPMutexExit(sys_ipmutex_t mutex)207{208sysAssert(mutex);209return ReleaseMutex(mutex) ? SYS_OK : SYS_ERR;210}211212int213sysIPMutexClose(sys_ipmutex_t mutex)214{215return CloseHandle(mutex) ? SYS_OK : SYS_ERR;216}217218int219sysEventCreate(const char *name, sys_event_t *eventPtr, jboolean manualReset)220{221HANDLE event;222BOOL reset = (manualReset == JNI_TRUE) ? TRUE : FALSE;223224sysAssert(eventPtr);225226event = CreateEvent(NULL, /* no inheritance */227reset, /* manual reset */228FALSE, /* initially, not signalled */229name);230if (event == NULL) {231return SYS_ERR;232} else if (GetLastError() == ERROR_ALREADY_EXISTS) {233/* If the call above didn't create it, consider it an error */234CloseHandle(event);235return SYS_INUSE;236}237238*eventPtr = event;239return SYS_OK;240}241242int243sysEventOpen(const char *name, sys_event_t *eventPtr)244{245HANDLE event;246247sysAssert(eventPtr);248sysAssert(name);249250event = OpenEvent(SYNCHRONIZE | EVENT_MODIFY_STATE,251/* able to wait/signal */252FALSE, /* no inheritance */253name);254if (event == NULL) {255return SYS_ERR;256}257258*eventPtr = event;259return SYS_OK;260}261262int263sysEventWait(sys_process_t otherProcess, sys_event_t event, long timeout)264{265HANDLE handles[2]; /* process, event */266DWORD rc;267int count;268DWORD dwTimeout = (timeout == 0) ? INFINITE : (DWORD)timeout;269270/*271* If the signalling process is specified, and it dies while we wait,272* detect it and return an error.273*/274sysAssert(event);275276handles[0] = event;277handles[1] = otherProcess;278279count = (otherProcess == NULL) ? 1 : 2;280281rc = WaitForMultipleObjects(count, handles,282FALSE, /* wait for either, not both */283dwTimeout);284if (rc == WAIT_OBJECT_0) {285/* Signalled, return success */286return SYS_OK;287} else if (rc == WAIT_OBJECT_0 + 1) {288/* Other process died, return error */289return SYS_DIED;290} else if (rc == WAIT_TIMEOUT) {291/* timeout */292return SYS_TIMEOUT;293}294return SYS_ERR;295}296297int298sysEventSignal(sys_event_t event)299{300sysAssert(event);301return SetEvent(event) ? SYS_OK : SYS_ERR;302}303304int305sysEventClose(sys_event_t event)306{307return CloseHandle(event) ? SYS_OK : SYS_ERR;308}309310jlong311sysProcessGetID()312{313return GetCurrentProcessId();314}315316int317sysProcessOpen(jlong processID, sys_process_t *processPtr)318{319HANDLE process;320321sysAssert(processPtr);322323process = OpenProcess(SYNCHRONIZE, /* able to wait on death */324FALSE, /* no inheritance */325(DWORD)processID);326if (process == NULL) {327return SYS_ERR;328}329330*processPtr = process;331return SYS_OK;332}333334int335sysProcessClose(sys_process_t *process)336{337return CloseHandle(process) ? SYS_OK : SYS_ERR;338}339340int341sysGetLastError(char *buf, int len)342{343long errval = GetLastError();344if (errval != 0) {345int n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,346NULL, errval,3470, buf, len, NULL);348if (n > 3) {349/* Drop final '.', CR, LF */350if (buf[n - 1] == '\n') n--;351if (buf[n - 1] == '\r') n--;352if (buf[n - 1] == '.') n--;353buf[n] = '\0';354}355return SYS_OK;356}357buf[0] = '\0';358return 0;359}360361int362sysTlsAlloc() {363return TlsAlloc();364}365366void367sysTlsFree(int index) {368TlsFree(index);369}370371void372sysTlsPut(int index, void *value) {373TlsSetValue(index, value);374}375376void *377sysTlsGet(int index) {378return TlsGetValue(index);379}380381void382sysSleep(long duration) {383Sleep((DWORD)duration);384}385386387