50 #include "runtime/javaCalls.hpp"
51 #include "runtime/mutexLocker.hpp"
52 #include "runtime/objectMonitor.hpp"
53 #include "runtime/orderAccess.inline.hpp"
54 #include "runtime/osThread.hpp"
55 #include "runtime/perfMemory.hpp"
56 #include "runtime/sharedRuntime.hpp"
57 #include "runtime/statSampler.hpp"
58 #include "runtime/stubRoutines.hpp"
59 #include "runtime/thread.inline.hpp"
60 #include "runtime/threadCritical.hpp"
61 #include "runtime/timer.hpp"
62 #include "runtime/vm_version.hpp"
63 #include "services/attachListener.hpp"
64 #include "services/memTracker.hpp"
65 #include "services/runtimeService.hpp"
66 #include "utilities/decoder.hpp"
67 #include "utilities/defaultStream.hpp"
68 #include "utilities/events.hpp"
69 #include "utilities/growableArray.hpp"
70 #include "utilities/vmError.hpp"
71
72 // put OS-includes here
73 # include <dlfcn.h>
74 # include <errno.h>
75 # include <exception>
76 # include <link.h>
77 # include <poll.h>
78 # include <pthread.h>
79 # include <pwd.h>
80 # include <schedctl.h>
81 # include <setjmp.h>
82 # include <signal.h>
83 # include <stdio.h>
84 # include <alloca.h>
85 # include <sys/filio.h>
86 # include <sys/ipc.h>
87 # include <sys/lwp.h>
88 # include <sys/machelf.h> // for elf Sym structure used by dladdr1
89 # include <sys/mman.h>
2249
2250 // sun.misc.Signal
2251
2252 extern "C" {
2253 static void UserHandler(int sig, void *siginfo, void *context) {
2254 // Ctrl-C is pressed during error reporting, likely because the error
2255 // handler fails to abort. Let VM die immediately.
2256 if (sig == SIGINT && is_error_reported()) {
2257 os::die();
2258 }
2259
2260 os::signal_notify(sig);
2261 // We do not need to reinstate the signal handler each time...
2262 }
2263 }
2264
2265 void* os::user_handler() {
2266 return CAST_FROM_FN_PTR(void*, UserHandler);
2267 }
2268
2269 class Semaphore : public StackObj {
2270 public:
2271 Semaphore();
2272 ~Semaphore();
2273 void signal();
2274 void wait();
2275 bool trywait();
2276 bool timedwait(unsigned int sec, int nsec);
2277 private:
2278 sema_t _semaphore;
2279 };
2280
2281
2282 Semaphore::Semaphore() {
2283 sema_init(&_semaphore, 0, NULL, NULL);
2284 }
2285
2286 Semaphore::~Semaphore() {
2287 sema_destroy(&_semaphore);
2288 }
2289
2290 void Semaphore::signal() {
2291 sema_post(&_semaphore);
2292 }
2293
2294 void Semaphore::wait() {
2295 sema_wait(&_semaphore);
2296 }
2297
2298 bool Semaphore::trywait() {
2299 return sema_trywait(&_semaphore) == 0;
2300 }
2301
2302 bool Semaphore::timedwait(unsigned int sec, int nsec) {
2303 struct timespec ts;
2304 unpackTime(&ts, false, (sec * NANOSECS_PER_SEC) + nsec);
2305
2306 while (1) {
2307 int result = sema_timedwait(&_semaphore, &ts);
2308 if (result == 0) {
2309 return true;
2310 } else if (errno == EINTR) {
2311 continue;
2312 } else if (errno == ETIME) {
2313 return false;
2314 } else {
2315 return false;
|
50 #include "runtime/javaCalls.hpp"
51 #include "runtime/mutexLocker.hpp"
52 #include "runtime/objectMonitor.hpp"
53 #include "runtime/orderAccess.inline.hpp"
54 #include "runtime/osThread.hpp"
55 #include "runtime/perfMemory.hpp"
56 #include "runtime/sharedRuntime.hpp"
57 #include "runtime/statSampler.hpp"
58 #include "runtime/stubRoutines.hpp"
59 #include "runtime/thread.inline.hpp"
60 #include "runtime/threadCritical.hpp"
61 #include "runtime/timer.hpp"
62 #include "runtime/vm_version.hpp"
63 #include "services/attachListener.hpp"
64 #include "services/memTracker.hpp"
65 #include "services/runtimeService.hpp"
66 #include "utilities/decoder.hpp"
67 #include "utilities/defaultStream.hpp"
68 #include "utilities/events.hpp"
69 #include "utilities/growableArray.hpp"
70 #include "utilities/semaphore.hpp"
71 #include "utilities/vmError.hpp"
72
73 // put OS-includes here
74 # include <dlfcn.h>
75 # include <errno.h>
76 # include <exception>
77 # include <link.h>
78 # include <poll.h>
79 # include <pthread.h>
80 # include <pwd.h>
81 # include <schedctl.h>
82 # include <setjmp.h>
83 # include <signal.h>
84 # include <stdio.h>
85 # include <alloca.h>
86 # include <sys/filio.h>
87 # include <sys/ipc.h>
88 # include <sys/lwp.h>
89 # include <sys/machelf.h> // for elf Sym structure used by dladdr1
90 # include <sys/mman.h>
2250
2251 // sun.misc.Signal
2252
2253 extern "C" {
2254 static void UserHandler(int sig, void *siginfo, void *context) {
2255 // Ctrl-C is pressed during error reporting, likely because the error
2256 // handler fails to abort. Let VM die immediately.
2257 if (sig == SIGINT && is_error_reported()) {
2258 os::die();
2259 }
2260
2261 os::signal_notify(sig);
2262 // We do not need to reinstate the signal handler each time...
2263 }
2264 }
2265
2266 void* os::user_handler() {
2267 return CAST_FROM_FN_PTR(void*, UserHandler);
2268 }
2269
2270 static int sem_max_value = -1;
2271
2272 Semaphore::Semaphore(uint value, uint max) {
2273 guarantee(value <= max, "value lower than max");
2274
2275 static long sem_value_max = sysconf(_SC_SEM_VALUE_MAX);
2276 guarantee(max == Semaphore::NoMaxCount || value <= sem_max_value,
2277 err_msg("Max value: %u set higher than SEM_VALUE_MAX: %l", sem_value_max));
2278
2279 int ret = sema_init(&_semaphore, value, NULL, NULL);
2280
2281 guarantee(ret == 0, "Failed to initialize semaphore");
2282 }
2283
2284 Semaphore::~Semaphore() {
2285 sema_destroy(&_semaphore);
2286 }
2287
2288 void Semaphore::signal() {
2289 int ret = sema_post(&_semaphore);
2290
2291 assert(ret == 0, err_msg("Semaphore signal failed: %d", errno));
2292 }
2293
2294 void Semaphore::signal(uint count) {
2295 for (uint i = 0; i < count; i++) {
2296 signal();
2297 }
2298 }
2299
2300 void Semaphore::wait() {
2301 int ret;
2302
2303 do {
2304 ret = sema_wait(&_semaphore);
2305 // Retry if the wait was interrupted by a signal.
2306 } while (errno == EINTR);
2307
2308 assert(ret == 0, err_msg("Semaphore wait failed: %d", errno));
2309 }
2310
2311 bool Semaphore::trywait() {
2312 return sema_trywait(&_semaphore) == 0;
2313 }
2314
2315 bool Semaphore::timedwait(unsigned int sec, int nsec) {
2316 struct timespec ts;
2317 unpackTime(&ts, false, (sec * NANOSECS_PER_SEC) + nsec);
2318
2319 while (1) {
2320 int result = sema_timedwait(&_semaphore, &ts);
2321 if (result == 0) {
2322 return true;
2323 } else if (errno == EINTR) {
2324 continue;
2325 } else if (errno == ETIME) {
2326 return false;
2327 } else {
2328 return false;
|