src/hotspot/os/bsd/semaphore_bsd.cpp
author alanb
Thu, 21 Mar 2019 08:41:10 +0000
branchniosocketimpl-branch
changeset 57275 222fa5ed1c91
parent 53461 08d6edeb3145
permissions -rw-r--r--
Add @systemProperty ref to system property to configure old impl
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
/*
53461
08d6edeb3145 8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents: 48635
diff changeset
     2
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
48635
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/precompiled.hpp"
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    26
#include "semaphore_bsd.hpp"
53461
08d6edeb3145 8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents: 48635
diff changeset
    27
#include "runtime/os.hpp"
48635
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    28
#include "utilities/debug.hpp"
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    29
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    30
#include <semaphore.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
#ifdef __APPLE__
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    33
// OS X doesn't support unamed POSIX semaphores, so the implementation in os_posix.cpp can't be used.
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
static const char* sem_init_strerror(kern_return_t value) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    36
  switch (value) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    37
    case KERN_INVALID_ARGUMENT:  return "Invalid argument";
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    38
    case KERN_RESOURCE_SHORTAGE: return "Resource shortage";
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    39
    default:                     return "Unknown";
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
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    43
OSXSemaphore::OSXSemaphore(uint value) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    44
  kern_return_t ret = semaphore_create(mach_task_self(), &_semaphore, SYNC_POLICY_FIFO, value);
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
  guarantee(ret == KERN_SUCCESS, "Failed to create semaphore: %s", sem_init_strerror(ret));
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
OSXSemaphore::~OSXSemaphore() {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    50
  semaphore_destroy(mach_task_self(), _semaphore);
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    51
}
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    52
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    53
void OSXSemaphore::signal(uint count) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    54
  for (uint i = 0; i < count; i++) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    55
    kern_return_t ret = semaphore_signal(_semaphore);
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    56
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    57
    assert(ret == KERN_SUCCESS, "Failed to signal semaphore");
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    58
  }
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    59
}
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    60
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    61
void OSXSemaphore::wait() {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    62
  kern_return_t ret;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    63
  while ((ret = semaphore_wait(_semaphore)) == KERN_ABORTED) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    64
    // Semaphore was interrupted. Retry.
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    65
  }
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    66
  assert(ret == KERN_SUCCESS, "Failed to wait on semaphore");
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    67
}
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    68
53461
08d6edeb3145 8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents: 48635
diff changeset
    69
bool OSXSemaphore::trywait() {
08d6edeb3145 8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents: 48635
diff changeset
    70
  return timedwait(0);
48635
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    71
}
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    72
53461
08d6edeb3145 8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents: 48635
diff changeset
    73
bool OSXSemaphore::timedwait(int64_t millis) {
48635
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    74
  kern_return_t kr = KERN_ABORTED;
53461
08d6edeb3145 8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents: 48635
diff changeset
    75
08d6edeb3145 8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents: 48635
diff changeset
    76
  // kernel semaphores take a relative timeout
48635
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    77
  mach_timespec_t waitspec;
53461
08d6edeb3145 8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents: 48635
diff changeset
    78
  int secs = millis / MILLIUNITS;
08d6edeb3145 8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents: 48635
diff changeset
    79
  int nsecs = (millis % MILLIUNITS) * NANOSECS_PER_MILLISEC;
08d6edeb3145 8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents: 48635
diff changeset
    80
  waitspec.tv_sec = secs;
08d6edeb3145 8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents: 48635
diff changeset
    81
  waitspec.tv_nsec = nsecs;
48635
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    82
53461
08d6edeb3145 8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents: 48635
diff changeset
    83
  int64_t starttime = os::javaTimeMillis() * NANOSECS_PER_MILLISEC;
48635
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    84
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    85
  kr = semaphore_timedwait(_semaphore, waitspec);
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    86
  while (kr == KERN_ABORTED) {
53461
08d6edeb3145 8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents: 48635
diff changeset
    87
    // reduce the timout and try again
08d6edeb3145 8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents: 48635
diff changeset
    88
    int64_t totalwait = millis * NANOSECS_PER_MILLISEC;
08d6edeb3145 8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents: 48635
diff changeset
    89
    int64_t current = os::javaTimeMillis() * NANOSECS_PER_MILLISEC;
48635
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    90
    int64_t passedtime = current - starttime;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    91
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    92
    if (passedtime >= totalwait) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    93
      waitspec.tv_sec = 0;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    94
      waitspec.tv_nsec = 0;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    95
    } else {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    96
      int64_t waittime = totalwait - (current - starttime);
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    97
      waitspec.tv_sec = waittime / NANOSECS_PER_SEC;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    98
      waitspec.tv_nsec = waittime % NANOSECS_PER_SEC;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    99
    }
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
   100
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
   101
    kr = semaphore_timedwait(_semaphore, waitspec);
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
   102
  }
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
   103
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
   104
  return kr == KERN_SUCCESS;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
   105
}
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
   106
#endif // __APPLE__