src/hotspot/os/bsd/semaphore_bsd.cpp
author jiangli
Mon, 21 May 2018 15:15:58 -0400
changeset 50199 83d8b3a25f25
parent 48635 612dfa1d8aad
child 53461 08d6edeb3145
permissions -rw-r--r--
8199807: AppCDS performs overly restrictive path matching check. 8203377: Cleanup the usage of os::file_name_strcmp() in SharedPathsMiscInfo::check(). Summary: Relax CDS/AppCDS path check for modules image. Reviewed-by: iklam, ccheung
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/precompiled.hpp"
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    26
#include "semaphore_bsd.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 <semaphore.h>
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    30
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    31
#ifdef __APPLE__
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    32
// 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
    33
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    34
static const char* sem_init_strerror(kern_return_t value) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    35
  switch (value) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    36
    case KERN_INVALID_ARGUMENT:  return "Invalid argument";
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    37
    case KERN_RESOURCE_SHORTAGE: return "Resource shortage";
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    38
    default:                     return "Unknown";
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    39
  }
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
OSXSemaphore::OSXSemaphore(uint value) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    43
  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
    44
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    45
  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
    46
}
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
OSXSemaphore::~OSXSemaphore() {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    49
  semaphore_destroy(mach_task_self(), _semaphore);
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    50
}
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
void OSXSemaphore::signal(uint count) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    53
  for (uint i = 0; i < count; i++) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    54
    kern_return_t ret = semaphore_signal(_semaphore);
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
    assert(ret == KERN_SUCCESS, "Failed to signal semaphore");
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    57
  }
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
void OSXSemaphore::wait() {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    61
  kern_return_t ret;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    62
  while ((ret = semaphore_wait(_semaphore)) == KERN_ABORTED) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    63
    // Semaphore was interrupted. Retry.
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    64
  }
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    65
  assert(ret == KERN_SUCCESS, "Failed to wait on semaphore");
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    66
}
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
int64_t OSXSemaphore::currenttime() {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    69
  struct timeval tv;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    70
  gettimeofday(&tv, NULL);
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    71
  return (tv.tv_sec * NANOSECS_PER_SEC) + (tv.tv_usec * 1000);
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    72
}
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    73
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    74
bool OSXSemaphore::trywait() {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    75
  return timedwait(0, 0);
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    76
}
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    77
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    78
bool OSXSemaphore::timedwait(unsigned int sec, int nsec) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    79
  kern_return_t kr = KERN_ABORTED;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    80
  mach_timespec_t waitspec;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    81
  waitspec.tv_sec = sec;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    82
  waitspec.tv_nsec = nsec;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    83
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    84
  int64_t starttime = currenttime();
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    85
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    86
  kr = semaphore_timedwait(_semaphore, waitspec);
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    87
  while (kr == KERN_ABORTED) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    88
    int64_t totalwait = (sec * NANOSECS_PER_SEC) + nsec;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    89
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    90
    int64_t current = currenttime();
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    91
    int64_t passedtime = current - starttime;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    92
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    93
    if (passedtime >= totalwait) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    94
      waitspec.tv_sec = 0;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    95
      waitspec.tv_nsec = 0;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    96
    } else {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    97
      int64_t waittime = totalwait - (current - starttime);
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    98
      waitspec.tv_sec = waittime / NANOSECS_PER_SEC;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    99
      waitspec.tv_nsec = waittime % NANOSECS_PER_SEC;
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
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
   102
    kr = semaphore_timedwait(_semaphore, waitspec);
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
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
   105
  return kr == KERN_SUCCESS;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
   106
}
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
   107
#endif // __APPLE__