src/hotspot/os/windows/semaphore_windows.cpp
author serb
Sun, 24 Jun 2018 16:35:21 -0700
changeset 50834 9cf279436b9d
parent 48635 612dfa1d8aad
permissions -rw-r--r--
8205153: Delete "standard instructions" machinery in the open automated tests Reviewed-by: prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48635
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
     1
/*
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
     4
 *
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
     7
 * published by the Free Software Foundation.
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
     8
 *
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    13
 * accompanied this code).
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    14
 *
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    18
 *
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    21
 * questions.
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    22
 *
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    23
 */
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    24
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    25
#include "precompiled.hpp"
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    26
#include "semaphore_windows.hpp"
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    27
#include "utilities/debug.hpp"
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    28
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    29
#include <windows.h>
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    30
#include <errno.h>
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    31
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    32
WindowsSemaphore::WindowsSemaphore(uint value) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    33
  _semaphore = ::CreateSemaphore(NULL, value, LONG_MAX, NULL);
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    34
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    35
  guarantee(_semaphore != NULL, "CreateSemaphore failed with error code: %lu", GetLastError());
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    36
}
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    37
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    38
WindowsSemaphore::~WindowsSemaphore() {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    39
  ::CloseHandle(_semaphore);
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    40
}
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    41
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    42
void WindowsSemaphore::signal(uint count) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    43
  if (count > 0) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    44
    BOOL ret = ::ReleaseSemaphore(_semaphore, count, NULL);
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    45
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    46
    assert(ret != 0, "ReleaseSemaphore failed with error code: %lu", GetLastError());
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    47
  }
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    48
}
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    49
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    50
void WindowsSemaphore::wait() {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    51
  DWORD ret = ::WaitForSingleObject(_semaphore, INFINITE);
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    52
  assert(ret != WAIT_FAILED,   "WaitForSingleObject failed with error code: %lu", GetLastError());
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    53
  assert(ret == WAIT_OBJECT_0, "WaitForSingleObject failed with return value: %lu", ret);
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    54
}
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    55
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    56
bool WindowsSemaphore::trywait() {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    57
  DWORD ret = ::WaitForSingleObject(_semaphore, 0);
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    58
  assert(ret != WAIT_FAILED,   "WaitForSingleObject failed with error code: %lu", GetLastError());
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    59
  return ret == WAIT_OBJECT_0;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    60
}