< prev index next >

src/os/bsd/vm/os_bsd.cpp

Print this page




  49 #include "runtime/java.hpp"
  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 "services/attachListener.hpp"
  63 #include "services/memTracker.hpp"
  64 #include "services/runtimeService.hpp"
  65 #include "utilities/decoder.hpp"
  66 #include "utilities/defaultStream.hpp"
  67 #include "utilities/events.hpp"
  68 #include "utilities/growableArray.hpp"

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


1924   return CAST_FROM_FN_PTR(void*, oldSigAct.sa_handler);
1925 }
1926 
1927 void os::signal_raise(int signal_number) {
1928   ::raise(signal_number);
1929 }
1930 
1931 // The following code is moved from os.cpp for making this
1932 // code platform specific, which it is by its very nature.
1933 
1934 // Will be modified when max signal is changed to be dynamic
1935 int os::sigexitnum_pd() {
1936   return NSIG;
1937 }
1938 
1939 // a counter for each possible signal value
1940 static volatile jint pending_signals[NSIG+1] = { 0 };
1941 
1942 // Bsd(POSIX) specific hand shaking semaphore.
1943 #ifdef __APPLE__
1944 typedef semaphore_t os_semaphore_t;
1945 
1946   #define SEM_INIT(sem, value)    semaphore_create(mach_task_self(), &sem, SYNC_POLICY_FIFO, value)
1947   #define SEM_WAIT(sem)           semaphore_wait(sem)
1948   #define SEM_POST(sem)           semaphore_signal(sem)
1949   #define SEM_DESTROY(sem)        semaphore_destroy(mach_task_self(), sem)
1950 #else
1951 typedef sem_t os_semaphore_t;
1952 
1953   #define SEM_INIT(sem, value)    sem_init(&sem, 0, value)
1954   #define SEM_WAIT(sem)           sem_wait(&sem)
1955   #define SEM_POST(sem)           sem_post(&sem)
1956   #define SEM_DESTROY(sem)        sem_destroy(&sem)
1957 #endif
1958 
1959 class Semaphore : public StackObj {
1960  public:
1961   Semaphore();
1962   ~Semaphore();
1963   void signal();
1964   void wait();
1965   bool trywait();
1966   bool timedwait(unsigned int sec, int nsec);
1967  private:
1968   jlong currenttime() const;
1969   os_semaphore_t _semaphore;
1970 };
1971 
1972 Semaphore::Semaphore() : _semaphore(0) {
1973   SEM_INIT(_semaphore, 0);
1974 }
1975 
1976 Semaphore::~Semaphore() {
1977   SEM_DESTROY(_semaphore);
1978 }
1979 
1980 void Semaphore::signal() {
1981   SEM_POST(_semaphore);
1982 }
1983 






1984 void Semaphore::wait() {
1985   SEM_WAIT(_semaphore);






1986 }
1987 
1988 jlong Semaphore::currenttime() const {
1989   struct timeval tv;
1990   gettimeofday(&tv, NULL);
1991   return (tv.tv_sec * NANOSECS_PER_SEC) + (tv.tv_usec * 1000);
1992 }
1993 
1994 #ifdef __APPLE__
1995 bool Semaphore::trywait() {
1996   return timedwait(0, 0);
1997 }
1998 
1999 bool Semaphore::timedwait(unsigned int sec, int nsec) {
2000   kern_return_t kr = KERN_ABORTED;
2001   mach_timespec_t waitspec;
2002   waitspec.tv_sec = sec;
2003   waitspec.tv_nsec = nsec;
2004 
2005   jlong starttime = currenttime();
2006 
2007   kr = semaphore_timedwait(_semaphore, waitspec);
2008   while (kr == KERN_ABORTED) {
2009     jlong totalwait = (sec * NANOSECS_PER_SEC) + nsec;
2010 
2011     jlong current = currenttime();
2012     jlong passedtime = current - starttime;
2013 
2014     if (passedtime >= totalwait) {
2015       waitspec.tv_sec = 0;
2016       waitspec.tv_nsec = 0;
2017     } else {
2018       jlong waittime = totalwait - (current - starttime);
2019       waitspec.tv_sec = waittime / NANOSECS_PER_SEC;
2020       waitspec.tv_nsec = waittime % NANOSECS_PER_SEC;
2021     }
2022 
2023     kr = semaphore_timedwait(_semaphore, waitspec);
2024   }
2025 
2026   return kr == KERN_SUCCESS;
2027 }
2028 
2029 #else
2030 
2031 bool Semaphore::trywait() {




  49 #include "runtime/java.hpp"
  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 "services/attachListener.hpp"
  63 #include "services/memTracker.hpp"
  64 #include "services/runtimeService.hpp"
  65 #include "utilities/decoder.hpp"
  66 #include "utilities/defaultStream.hpp"
  67 #include "utilities/events.hpp"
  68 #include "utilities/growableArray.hpp"
  69 #include "utilities/semaphore.hpp"
  70 #include "utilities/vmError.hpp"
  71 
  72 // put OS-includes here
  73 # include <sys/types.h>
  74 # include <sys/mman.h>
  75 # include <sys/stat.h>
  76 # include <sys/select.h>
  77 # include <pthread.h>
  78 # include <signal.h>
  79 # include <errno.h>
  80 # include <dlfcn.h>
  81 # include <stdio.h>
  82 # include <unistd.h>
  83 # include <sys/resource.h>
  84 # include <pthread.h>
  85 # include <sys/stat.h>
  86 # include <sys/time.h>
  87 # include <sys/times.h>
  88 # include <sys/utsname.h>
  89 # include <sys/socket.h>


1925   return CAST_FROM_FN_PTR(void*, oldSigAct.sa_handler);
1926 }
1927 
1928 void os::signal_raise(int signal_number) {
1929   ::raise(signal_number);
1930 }
1931 
1932 // The following code is moved from os.cpp for making this
1933 // code platform specific, which it is by its very nature.
1934 
1935 // Will be modified when max signal is changed to be dynamic
1936 int os::sigexitnum_pd() {
1937   return NSIG;
1938 }
1939 
1940 // a counter for each possible signal value
1941 static volatile jint pending_signals[NSIG+1] = { 0 };
1942 
1943 // Bsd(POSIX) specific hand shaking semaphore.
1944 #ifdef __APPLE__


1945   #define SEM_INIT(sem, value)    semaphore_create(mach_task_self(), &sem, SYNC_POLICY_FIFO, value)
1946   #define SEM_WAIT(sem)           semaphore_wait(sem)
1947   #define SEM_POST(sem)           semaphore_signal(sem)
1948   #define SEM_DESTROY(sem)        semaphore_destroy(mach_task_self(), sem)
1949 #else


1950   #define SEM_INIT(sem, value)    sem_init(&sem, 0, value)
1951   #define SEM_WAIT(sem)           sem_wait(&sem)
1952   #define SEM_POST(sem)           sem_post(&sem)
1953   #define SEM_DESTROY(sem)        sem_destroy(&sem)
1954 #endif
1955 
1956 Semaphore::Semaphore(uint value, uint max) : _semaphore(0) {
1957   SEM_INIT(_semaphore, value);













1958 }
1959 
1960 Semaphore::~Semaphore() {
1961   SEM_DESTROY(_semaphore);
1962 }
1963 
1964 void Semaphore::signal() {
1965   SEM_POST(_semaphore);
1966 }
1967 
1968 void Semaphore::signal(uint count) {
1969   for (uint i = 0; i < count; i++) {
1970     signal();
1971   }
1972 }
1973 
1974 void Semaphore::wait() {
1975 #ifdef __APPLE__
1976   while (SEM_WAIT(_semaphore) == KERN_ABORTED) {
1977 #else
1978   while (SEM_WAIT(_semaphore) == -1 && errno == EINTR) {
1979 #endif
1980     // Semaphore was interrupted. Retry.
1981   }
1982 }
1983 
1984 static jlong semaphore_currenttime() {
1985   struct timeval tv;
1986   gettimeofday(&tv, NULL);
1987   return (tv.tv_sec * NANOSECS_PER_SEC) + (tv.tv_usec * 1000);
1988 }
1989 
1990 #ifdef __APPLE__
1991 bool Semaphore::trywait() {
1992   return timedwait(0, 0);
1993 }
1994 
1995 bool Semaphore::timedwait(unsigned int sec, int nsec) {
1996   kern_return_t kr = KERN_ABORTED;
1997   mach_timespec_t waitspec;
1998   waitspec.tv_sec = sec;
1999   waitspec.tv_nsec = nsec;
2000 
2001   jlong starttime = semaphore_currenttime();
2002 
2003   kr = semaphore_timedwait(_semaphore, waitspec);
2004   while (kr == KERN_ABORTED) {
2005     jlong totalwait = (sec * NANOSECS_PER_SEC) + nsec;
2006 
2007     jlong current = semaphore_currenttime();
2008     jlong passedtime = current - starttime;
2009 
2010     if (passedtime >= totalwait) {
2011       waitspec.tv_sec = 0;
2012       waitspec.tv_nsec = 0;
2013     } else {
2014       jlong waittime = totalwait - (current - starttime);
2015       waitspec.tv_sec = waittime / NANOSECS_PER_SEC;
2016       waitspec.tv_nsec = waittime % NANOSECS_PER_SEC;
2017     }
2018 
2019     kr = semaphore_timedwait(_semaphore, waitspec);
2020   }
2021 
2022   return kr == KERN_SUCCESS;
2023 }
2024 
2025 #else
2026 
2027 bool Semaphore::trywait() {


< prev index next >