< prev index next >

src/os/linux/vm/os_linux.cpp

Print this page




  52 #include "runtime/mutexLocker.hpp"
  53 #include "runtime/objectMonitor.hpp"
  54 #include "runtime/orderAccess.inline.hpp"
  55 #include "runtime/osThread.hpp"
  56 #include "runtime/perfMemory.hpp"
  57 #include "runtime/sharedRuntime.hpp"
  58 #include "runtime/statSampler.hpp"
  59 #include "runtime/stubRoutines.hpp"
  60 #include "runtime/thread.inline.hpp"
  61 #include "runtime/threadCritical.hpp"
  62 #include "runtime/timer.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/elfFile.hpp"
  70 #include "utilities/growableArray.hpp"
  71 #include "utilities/macros.hpp"

  72 #include "utilities/vmError.hpp"
  73 
  74 // put OS-includes here
  75 # include <sys/types.h>
  76 # include <sys/mman.h>
  77 # include <sys/stat.h>
  78 # include <sys/select.h>
  79 # include <pthread.h>
  80 # include <signal.h>
  81 # include <errno.h>
  82 # include <dlfcn.h>
  83 # include <stdio.h>
  84 # include <unistd.h>
  85 # include <sys/resource.h>
  86 # include <pthread.h>
  87 # include <sys/stat.h>
  88 # include <sys/time.h>
  89 # include <sys/times.h>
  90 # include <sys/utsname.h>
  91 # include <sys/socket.h>


2376   // 4511530 - sem_post is serialized and handled by the manager thread. When
2377   // the program is interrupted by Ctrl-C, SIGINT is sent to every thread. We
2378   // don't want to flood the manager thread with sem_post requests.
2379   if (sig == SIGINT && Atomic::add(1, &sigint_count) > 1) {
2380     return;
2381   }
2382 
2383   // Ctrl-C is pressed during error reporting, likely because the error
2384   // handler fails to abort. Let VM die immediately.
2385   if (sig == SIGINT && is_error_reported()) {
2386     os::die();
2387   }
2388 
2389   os::signal_notify(sig);
2390 }
2391 
2392 void* os::user_handler() {
2393   return CAST_FROM_FN_PTR(void*, UserHandler);
2394 }
2395 
2396 class Semaphore : public StackObj {
2397  public:
2398   Semaphore();
2399   ~Semaphore();
2400   void signal();
2401   void wait();
2402   bool trywait();
2403   bool timedwait(unsigned int sec, int nsec);
2404  private:
2405   sem_t _semaphore;
2406 };
2407 
2408 Semaphore::Semaphore() {
2409   sem_init(&_semaphore, 0, 0);
2410 }
2411 
2412 Semaphore::~Semaphore() {
2413   sem_destroy(&_semaphore);
2414 }
2415 
2416 void Semaphore::signal() {
2417   sem_post(&_semaphore);







2418 }
2419 
2420 void Semaphore::wait() {
2421   sem_wait(&_semaphore);


2422 }
2423 
2424 bool Semaphore::trywait() {
2425   return sem_trywait(&_semaphore) == 0;
2426 }
2427 
2428 bool Semaphore::timedwait(unsigned int sec, int nsec) {
2429 
2430   struct timespec ts;
2431   // Semaphore's are always associated with CLOCK_REALTIME
2432   os::Linux::clock_gettime(CLOCK_REALTIME, &ts);
2433   // see unpackTime for discussion on overflow checking
2434   if (sec >= MAX_SECS) {
2435     ts.tv_sec += MAX_SECS;
2436     ts.tv_nsec = 0;
2437   } else {
2438     ts.tv_sec += sec;
2439     ts.tv_nsec += nsec;
2440     if (ts.tv_nsec >= NANOSECS_PER_SEC) {
2441       ts.tv_nsec -= NANOSECS_PER_SEC;




  52 #include "runtime/mutexLocker.hpp"
  53 #include "runtime/objectMonitor.hpp"
  54 #include "runtime/orderAccess.inline.hpp"
  55 #include "runtime/osThread.hpp"
  56 #include "runtime/perfMemory.hpp"
  57 #include "runtime/sharedRuntime.hpp"
  58 #include "runtime/statSampler.hpp"
  59 #include "runtime/stubRoutines.hpp"
  60 #include "runtime/thread.inline.hpp"
  61 #include "runtime/threadCritical.hpp"
  62 #include "runtime/timer.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/elfFile.hpp"
  70 #include "utilities/growableArray.hpp"
  71 #include "utilities/macros.hpp"
  72 #include "utilities/semaphore.hpp"
  73 #include "utilities/vmError.hpp"
  74 
  75 // put OS-includes here
  76 # include <sys/types.h>
  77 # include <sys/mman.h>
  78 # include <sys/stat.h>
  79 # include <sys/select.h>
  80 # include <pthread.h>
  81 # include <signal.h>
  82 # include <errno.h>
  83 # include <dlfcn.h>
  84 # include <stdio.h>
  85 # include <unistd.h>
  86 # include <sys/resource.h>
  87 # include <pthread.h>
  88 # include <sys/stat.h>
  89 # include <sys/time.h>
  90 # include <sys/times.h>
  91 # include <sys/utsname.h>
  92 # include <sys/socket.h>


2377   // 4511530 - sem_post is serialized and handled by the manager thread. When
2378   // the program is interrupted by Ctrl-C, SIGINT is sent to every thread. We
2379   // don't want to flood the manager thread with sem_post requests.
2380   if (sig == SIGINT && Atomic::add(1, &sigint_count) > 1) {
2381     return;
2382   }
2383 
2384   // Ctrl-C is pressed during error reporting, likely because the error
2385   // handler fails to abort. Let VM die immediately.
2386   if (sig == SIGINT && is_error_reported()) {
2387     os::die();
2388   }
2389 
2390   os::signal_notify(sig);
2391 }
2392 
2393 void* os::user_handler() {
2394   return CAST_FROM_FN_PTR(void*, UserHandler);
2395 }
2396 
2397 Semaphore::Semaphore(uint value, uint max) {
2398   guarantee(value <= max, "value lower than max");
2399   guarantee(max == Semaphore::NoMaxCount || max <= SEM_VALUE_MAX, "Max value set too high");








2400 
2401   int ret = sem_init(&_semaphore, 0, value);
2402   guarantee(ret == 0, "Failed to initialize semaphore");
2403 }
2404 
2405 Semaphore::~Semaphore() {
2406   sem_destroy(&_semaphore);
2407 }
2408 
2409 void Semaphore::signal() {
2410   int ret = sem_post(&_semaphore);
2411   guarantee(ret == 0, err_msg("sem_post failed: %d", ret));
2412 }
2413 
2414 void Semaphore::signal(uint count) {
2415   for (uint i = 0; i < count; i++) {
2416     signal();
2417   }
2418 }
2419 
2420 void Semaphore::wait() {
2421   while (sem_wait(&_semaphore) == -1 && errno == EINTR) {
2422     // Retry if the wait was interrupted by a signal.
2423   }
2424 }
2425 
2426 bool Semaphore::trywait() {
2427   return sem_trywait(&_semaphore) == 0;
2428 }
2429 
2430 bool Semaphore::timedwait(unsigned int sec, int nsec) {
2431 
2432   struct timespec ts;
2433   // Semaphore's are always associated with CLOCK_REALTIME
2434   os::Linux::clock_gettime(CLOCK_REALTIME, &ts);
2435   // see unpackTime for discussion on overflow checking
2436   if (sec >= MAX_SECS) {
2437     ts.tv_sec += MAX_SECS;
2438     ts.tv_nsec = 0;
2439   } else {
2440     ts.tv_sec += sec;
2441     ts.tv_nsec += nsec;
2442     if (ts.tv_nsec >= NANOSECS_PER_SEC) {
2443       ts.tv_nsec -= NANOSECS_PER_SEC;


< prev index next >