Path: blob/master/src/jdk.jdi/windows/native/libdt_shmem/shmem_md.c
41152 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/*33* These functions are not completely universal. For now, they are used34* exclusively for Jbug's shared memory transport mechanism. They have35* been implemented on Win32 only so far, so the abstractions may not be correct36* yet.37*/3839static HANDLE memHandle = NULL;4041#ifdef DEBUG42#define sysAssert(expression) { \43if (!(expression)) { \44exitTransportWithError \45("\"%s\", line %d: assertion failure\n", \46__FILE__, __DATE__, __LINE__); \47} \48}49#else50#define sysAssert(expression) ((void) 0)51#endif5253int54sysSharedMemCreate(const char *name, int length,55sys_shmem_t *mem, void **buffer)56{57void *mappedMemory;58HANDLE memHandle;5960sysAssert(buffer);61sysAssert(name);62sysAssert(length > 0);6364memHandle =65CreateFileMapping(INVALID_HANDLE_VALUE, /* backed by page file */66NULL, /* no inheritance */67PAGE_READWRITE,680, length, /* hi, lo order of length */69name);70if (memHandle == NULL) {71return SYS_ERR;72} else if (GetLastError() == ERROR_ALREADY_EXISTS) {73/* If the call above didn't create it, consider it an error */74CloseHandle(memHandle);75memHandle = NULL;76return SYS_INUSE;77}7879mappedMemory =80MapViewOfFile(memHandle,81FILE_MAP_WRITE, /* read/write */820, 0, 0); /* map entire "file" */8384if (mappedMemory == NULL) {85CloseHandle(memHandle);86memHandle = NULL;87return SYS_ERR;88}8990*mem = memHandle;91*buffer = mappedMemory;92return SYS_OK;93}9495int96sysSharedMemOpen(const char *name, sys_shmem_t *mem, void **buffer)97{98void *mappedMemory;99HANDLE memHandle;100101sysAssert(name);102sysAssert(buffer);103104memHandle =105OpenFileMapping(FILE_MAP_WRITE, /* read/write */106FALSE, /* no inheritance */107name);108if (memHandle == NULL) {109return SYS_ERR;110}111112mappedMemory =113MapViewOfFile(memHandle,114FILE_MAP_WRITE, /* read/write */1150, 0, 0); /* map entire "file" */116117if (mappedMemory == NULL) {118CloseHandle(memHandle);119memHandle = NULL;120return SYS_ERR;121}122123*mem = memHandle;124*buffer = mappedMemory;125return SYS_OK;126}127128int129sysSharedMemClose(sys_shmem_t mem, void *buffer)130{131if (buffer != NULL) {132if (!UnmapViewOfFile(buffer)) {133return SYS_ERR;134}135}136137if (!CloseHandle(mem)) {138return SYS_ERR;139}140141return SYS_OK;142}143144int145sysIPMutexCreate(const char *name, sys_ipmutex_t *mutexPtr)146{147HANDLE mutex;148149sysAssert(mutexPtr);150sysAssert(name);151152mutex = CreateMutex(NULL, /* no inheritance */153FALSE, /* no initial owner */154name);155if (mutex == NULL) {156return SYS_ERR;157} else if (GetLastError() == ERROR_ALREADY_EXISTS) {158/* If the call above didn't create it, consider it an error */159CloseHandle(mutex);160return SYS_INUSE;161}162163*mutexPtr = mutex;164return SYS_OK;165}166167int168sysIPMutexOpen(const char *name, sys_ipmutex_t *mutexPtr)169{170HANDLE mutex;171172sysAssert(mutexPtr);173sysAssert(name);174175mutex = OpenMutex(SYNCHRONIZE, /* able to wait/release */176FALSE, /* no inheritance */177name);178if (mutex == NULL) {179return SYS_ERR;180}181182*mutexPtr = mutex;183return SYS_OK;184}185186int187sysIPMutexEnter(sys_ipmutex_t mutex, sys_event_t event)188{189HANDLE handles[2] = { mutex, event };190int count = event == NULL ? 1 : 2;191DWORD rc;192193sysAssert(mutex);194rc = WaitForMultipleObjects(count, handles,195FALSE, /* wait for either, not both */196INFINITE); /* infinite timeout */197return (rc == WAIT_OBJECT_0) ? SYS_OK : SYS_ERR;198}199200int201sysIPMutexExit(sys_ipmutex_t mutex)202{203sysAssert(mutex);204return ReleaseMutex(mutex) ? SYS_OK : SYS_ERR;205}206207int208sysIPMutexClose(sys_ipmutex_t mutex)209{210return CloseHandle(mutex) ? SYS_OK : SYS_ERR;211}212213int214sysEventCreate(const char *name, sys_event_t *eventPtr, jboolean manualReset)215{216HANDLE event;217BOOL reset = (manualReset == JNI_TRUE) ? TRUE : FALSE;218219sysAssert(eventPtr);220221event = CreateEvent(NULL, /* no inheritance */222reset, /* manual reset */223FALSE, /* initially, not signalled */224name);225if (event == NULL) {226return SYS_ERR;227} else if (GetLastError() == ERROR_ALREADY_EXISTS) {228/* If the call above didn't create it, consider it an error */229CloseHandle(event);230return SYS_INUSE;231}232233*eventPtr = event;234return SYS_OK;235}236237int238sysEventOpen(const char *name, sys_event_t *eventPtr)239{240HANDLE event;241242sysAssert(eventPtr);243sysAssert(name);244245event = OpenEvent(SYNCHRONIZE | EVENT_MODIFY_STATE,246/* able to wait/signal */247FALSE, /* no inheritance */248name);249if (event == NULL) {250return SYS_ERR;251}252253*eventPtr = event;254return SYS_OK;255}256257int258sysEventWait(sys_process_t otherProcess, sys_event_t event, long timeout)259{260HANDLE handles[2]; /* process, event */261DWORD rc;262int count;263DWORD dwTimeout = (timeout == 0) ? INFINITE : (DWORD)timeout;264265/*266* If the signalling process is specified, and it dies while we wait,267* detect it and return an error.268*/269sysAssert(event);270271handles[0] = event;272handles[1] = otherProcess;273274count = (otherProcess == NULL) ? 1 : 2;275276rc = WaitForMultipleObjects(count, handles,277FALSE, /* wait for either, not both */278dwTimeout);279if (rc == WAIT_OBJECT_0) {280/* Signalled, return success */281return SYS_OK;282} else if (rc == WAIT_OBJECT_0 + 1) {283/* Other process died, return error */284return SYS_DIED;285} else if (rc == WAIT_TIMEOUT) {286/* timeout */287return SYS_TIMEOUT;288}289return SYS_ERR;290}291292int293sysEventSignal(sys_event_t event)294{295sysAssert(event);296return SetEvent(event) ? SYS_OK : SYS_ERR;297}298299int300sysEventClose(sys_event_t event)301{302return CloseHandle(event) ? SYS_OK : SYS_ERR;303}304305jlong306sysProcessGetID()307{308return GetCurrentProcessId();309}310311int312sysProcessOpen(jlong processID, sys_process_t *processPtr)313{314HANDLE process;315316sysAssert(processPtr);317318process = OpenProcess(SYNCHRONIZE, /* able to wait on death */319FALSE, /* no inheritance */320(DWORD)processID);321if (process == NULL) {322return SYS_ERR;323}324325*processPtr = process;326return SYS_OK;327}328329int330sysProcessClose(sys_process_t *process)331{332return CloseHandle(process) ? SYS_OK : SYS_ERR;333}334335int336sysGetLastError(char *buf, int len)337{338long errval = GetLastError();339if (errval != 0) {340int n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,341NULL, errval,3420, buf, len, NULL);343if (n > 3) {344/* Drop final '.', CR, LF */345if (buf[n - 1] == '\n') n--;346if (buf[n - 1] == '\r') n--;347if (buf[n - 1] == '.') n--;348buf[n] = '\0';349}350return SYS_OK;351}352buf[0] = '\0';353return 0;354}355356int357sysTlsAlloc() {358return TlsAlloc();359}360361void362sysTlsFree(int index) {363TlsFree(index);364}365366void367sysTlsPut(int index, void *value) {368TlsSetValue(index, value);369}370371void *372sysTlsGet(int index) {373return TlsGetValue(index);374}375376void377sysSleep(long duration) {378Sleep((DWORD)duration);379}380381382