src/hotspot/os/posix/semaphore_posix.cpp
author coleenp
Thu, 11 Jan 2018 18:42:36 -0500
changeset 48635 612dfa1d8aad
child 53461 08d6edeb3145
permissions -rw-r--r--
8130039: Move the platform-specific [OS]Semaphore code 8130038: Unify the semaphore usage in os_xxx.cpp 8194763: os::signal_lookup is unused Reviewed-by: dholmes, kbarrett
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
#ifndef __APPLE__
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    27
#include "runtime/os.hpp"
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    28
// POSIX unamed semaphores are not supported on OS X.
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    29
#include "semaphore_posix.hpp"
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
#define check_with_errno(check_type, cond, msg)                             \
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    33
  do {                                                                      \
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    34
    int err = errno;                                                        \
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    35
    check_type(cond, "%s; error='%s' (errno=%s)", msg, os::strerror(err),   \
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    36
               os::errno_name(err));                                        \
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    37
} while (false)
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    38
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    39
#define assert_with_errno(cond, msg)    check_with_errno(assert, cond, msg)
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    40
#define guarantee_with_errno(cond, msg) check_with_errno(guarantee, cond, msg)
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
PosixSemaphore::PosixSemaphore(uint value) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    43
  int ret = sem_init(&_semaphore, 0, 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_with_errno(ret == 0, "Failed to initialize semaphore");
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
PosixSemaphore::~PosixSemaphore() {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    49
  sem_destroy(&_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 PosixSemaphore::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
    int ret = sem_post(&_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_with_errno(ret == 0, "sem_post failed");
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 PosixSemaphore::wait() {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    61
  int ret;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    62
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    63
  do {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    64
    ret = sem_wait(&_semaphore);
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    65
  } while (ret != 0 && errno == EINTR);
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
  assert_with_errno(ret == 0, "sem_wait failed");
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    68
}
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    69
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    70
bool PosixSemaphore::trywait() {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    71
  int ret;
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
  do {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    74
    ret = sem_trywait(&_semaphore);
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    75
  } while (ret != 0 && errno == EINTR);
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
  assert_with_errno(ret == 0 || errno == EAGAIN, "trywait failed");
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    78
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    79
  return ret == 0;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    80
}
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    81
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    82
bool PosixSemaphore::timedwait(struct timespec ts) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    83
  while (true) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    84
    int result = sem_timedwait(&_semaphore, &ts);
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    85
    if (result == 0) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    86
      return true;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    87
    } else if (errno == EINTR) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    88
      continue;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    89
    } else if (errno == ETIMEDOUT) {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    90
      return false;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    91
    } else {
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    92
      assert_with_errno(false, "timedwait failed");
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    93
      return false;
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    94
    }
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    95
  }
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    96
}
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    97
#endif // __APPLE__
612dfa1d8aad 8130039: Move the platform-specific [OS]Semaphore code
coleenp
parents:
diff changeset
    98