jdk/src/windows/transport/shmem/shmem_md.c
author ohair
Tue, 11 Sep 2012 13:40:59 -0700
changeset 13678 5c8001201f98
parent 5506 202f599c92aa
child 14342 8435a30053c1
permissions -rw-r--r--
7197771: Adjust jdk sources to avoid use of implementation defined value of __FILE__ 7180608: Sort the order of object files when building shared libraries Reviewed-by: ohrstrom, erikj, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "shmem_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "sysShmem.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "shmemBase.h"  /* for exitTransportWithError */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
13678
5c8001201f98 7197771: Adjust jdk sources to avoid use of implementation defined value of __FILE__
ohair
parents: 5506
diff changeset
    33
/* Use THIS_FILE when it is available. */
5c8001201f98 7197771: Adjust jdk sources to avoid use of implementation defined value of __FILE__
ohair
parents: 5506
diff changeset
    34
#ifndef THIS_FILE
5c8001201f98 7197771: Adjust jdk sources to avoid use of implementation defined value of __FILE__
ohair
parents: 5506
diff changeset
    35
    #define THIS_FILE __FILE__
5c8001201f98 7197771: Adjust jdk sources to avoid use of implementation defined value of __FILE__
ohair
parents: 5506
diff changeset
    36
#endif
5c8001201f98 7197771: Adjust jdk sources to avoid use of implementation defined value of __FILE__
ohair
parents: 5506
diff changeset
    37
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * These functions are not completely universal. For now, they are used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * exclusively for Jbug's shared memory transport mechanism. They have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * been implemented on Win32 only so far, so the abstractions may not be correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
static HANDLE memHandle = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#define sysAssert(expression) {         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    if (!(expression)) {                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            exitTransportWithError \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            ("\"%s\", line %d: assertion failure\n", \
13678
5c8001201f98 7197771: Adjust jdk sources to avoid use of implementation defined value of __FILE__
ohair
parents: 5506
diff changeset
    52
             THIS_FILE, __DATE__, __LINE__); \
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
#define sysAssert(expression) ((void) 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
sysSharedMemCreate(const char *name, int length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                   sys_shmem_t *mem, void **buffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    void *mappedMemory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    HANDLE memHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    sysAssert(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    sysAssert(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    sysAssert(length > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    memHandle  =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        CreateFileMapping(INVALID_HANDLE_VALUE, /* backed by page file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                          NULL,               /* no inheritance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                          PAGE_READWRITE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                          0, length,          /* hi, lo order of length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                          name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    if (memHandle == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return SYS_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    } else if (GetLastError() == ERROR_ALREADY_EXISTS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        /* If the call above didn't create it, consider it an error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        CloseHandle(memHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        memHandle = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return SYS_INUSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    mappedMemory =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        MapViewOfFile(memHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                      FILE_MAP_WRITE,       /* read/write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                      0, 0, 0);             /* map entire "file" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    if (mappedMemory == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        CloseHandle(memHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        memHandle = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return SYS_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    *mem = memHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    *buffer = mappedMemory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    return SYS_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
sysSharedMemOpen(const char *name, sys_shmem_t *mem, void **buffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    void *mappedMemory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    HANDLE memHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    sysAssert(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    sysAssert(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    memHandle =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        OpenFileMapping(FILE_MAP_WRITE,     /* read/write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        FALSE,              /* no inheritance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                        name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    if (memHandle == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return SYS_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    mappedMemory =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        MapViewOfFile(memHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                      FILE_MAP_WRITE,       /* read/write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                      0, 0, 0);             /* map entire "file" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    if (mappedMemory == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        CloseHandle(memHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        memHandle = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return SYS_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    *mem = memHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    *buffer = mappedMemory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    return SYS_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
sysSharedMemClose(sys_shmem_t mem, void *buffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    if (buffer != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (!UnmapViewOfFile(buffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return SYS_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    if (!CloseHandle(mem)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return SYS_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    return SYS_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
sysIPMutexCreate(const char *name, sys_ipmutex_t *mutexPtr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    HANDLE mutex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    sysAssert(mutexPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    sysAssert(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    mutex = CreateMutex(NULL,            /* no inheritance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        FALSE,           /* no initial owner */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    if (mutex == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return SYS_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    } else if (GetLastError() == ERROR_ALREADY_EXISTS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        /* If the call above didn't create it, consider it an error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        CloseHandle(mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return SYS_INUSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    *mutexPtr = mutex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    return SYS_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
sysIPMutexOpen(const char *name, sys_ipmutex_t *mutexPtr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    HANDLE mutex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    sysAssert(mutexPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    sysAssert(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    mutex = OpenMutex(SYNCHRONIZE,      /* able to wait/release */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                      FALSE,            /* no inheritance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                      name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    if (mutex == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return SYS_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    *mutexPtr = mutex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    return SYS_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
sysIPMutexEnter(sys_ipmutex_t mutex, sys_event_t event)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    HANDLE handles[2] = { mutex, event };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    int count = event == NULL ? 1 : 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    DWORD rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    sysAssert(mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    rc = WaitForMultipleObjects(count, handles,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                FALSE,              /* wait for either, not both */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                INFINITE);          /* infinite timeout */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    return (rc == WAIT_OBJECT_0) ? SYS_OK : SYS_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
sysIPMutexExit(sys_ipmutex_t mutex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    sysAssert(mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    return ReleaseMutex(mutex) ? SYS_OK : SYS_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
sysIPMutexClose(sys_ipmutex_t mutex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    return CloseHandle(mutex) ? SYS_OK : SYS_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
sysEventCreate(const char *name, sys_event_t *eventPtr, jboolean manualReset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    HANDLE event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    BOOL reset = (manualReset == JNI_TRUE) ? TRUE : FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    sysAssert(eventPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    event = CreateEvent(NULL,            /* no inheritance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                        reset,           /* manual reset */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                        FALSE,           /* initially, not signalled */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                        name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    if (event == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return SYS_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    } else if (GetLastError() == ERROR_ALREADY_EXISTS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        /* If the call above didn't create it, consider it an error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        CloseHandle(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return SYS_INUSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    *eventPtr = event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    return SYS_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
sysEventOpen(const char *name, sys_event_t *eventPtr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    HANDLE event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    sysAssert(eventPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    sysAssert(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    event = OpenEvent(SYNCHRONIZE | EVENT_MODIFY_STATE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                        /* able to wait/signal */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                      FALSE,            /* no inheritance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                      name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    if (event == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return SYS_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    *eventPtr = event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    return SYS_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
sysEventWait(sys_process_t otherProcess, sys_event_t event, long timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    HANDLE handles[2];        /* process, event */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    DWORD rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    DWORD dwTimeout = (timeout == 0) ? INFINITE : (DWORD)timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * If the signalling process is specified, and it dies while we wait,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * detect it and return an error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    sysAssert(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    handles[0] = event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    handles[1] = otherProcess;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    count = (otherProcess == NULL) ? 1 : 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    rc = WaitForMultipleObjects(count, handles,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                                FALSE,        /* wait for either, not both */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                                dwTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    if (rc == WAIT_OBJECT_0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        /* Signalled, return success */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return SYS_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    } else if (rc == WAIT_OBJECT_0 + 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        /* Other process died, return error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return SYS_DIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    } else if (rc == WAIT_TIMEOUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        /* timeout */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        return SYS_TIMEOUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    return SYS_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
sysEventSignal(sys_event_t event)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    sysAssert(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    return SetEvent(event) ? SYS_OK : SYS_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
sysEventClose(sys_event_t event)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    return CloseHandle(event) ? SYS_OK : SYS_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
sysProcessGetID()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    return GetCurrentProcessId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
sysProcessOpen(jlong processID, sys_process_t *processPtr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    HANDLE process;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    sysAssert(processPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    process = OpenProcess(SYNCHRONIZE,    /* able to wait on death */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                          FALSE,          /* no inheritance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                          (DWORD)processID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    if (process == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return SYS_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    *processPtr = process;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    return SYS_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
sysProcessClose(sys_process_t *process)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    return CloseHandle(process) ? SYS_OK : SYS_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
sysGetLastError(char *buf, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    long errval = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    if (errval != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        int n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                              NULL, errval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                              0, buf, len, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if (n > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            /* Drop final '.', CR, LF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            if (buf[n - 1] == '\n') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            if (buf[n - 1] == '\r') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            if (buf[n - 1] == '.') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            buf[n] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        return SYS_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    buf[0] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
sysTlsAlloc() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    return TlsAlloc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
sysTlsFree(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    TlsFree(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
sysTlsPut(int index, void *value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    TlsSetValue(index, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
sysTlsGet(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    return TlsGetValue(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
sysSleep(long duration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    Sleep((DWORD)duration);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
}