< prev index next >

src/os/bsd/vm/os_bsd.cpp

Print this page




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() {




2028   return sem_trywait(&_semaphore) == 0;
2029 }
2030 
2031 bool Semaphore::timedwait(unsigned int sec, int nsec) {
2032   struct timespec ts;
2033   unpackTime(&ts, false, (sec * NANOSECS_PER_SEC) + nsec);
2034 
2035   while (1) {
2036     int result = sem_timedwait(&_semaphore, &ts);
2037     if (result == 0) {
2038       return true;
2039     } else if (errno == EINTR) {
2040       continue;
2041     } else if (errno == ETIMEDOUT) {
2042       return false;
2043     } else {
2044       return false;
2045     }
2046   }
2047 }
2048 
2049 #endif // __APPLE__
2050 
2051 static os_semaphore_t sig_sem;
2052 static Semaphore sr_semaphore;
2053 
2054 void os::signal_init_pd() {
2055   // Initialize signal structures
2056   ::memset((void*)pending_signals, 0, sizeof(pending_signals));
2057 
2058   // Initialize signal semaphore
2059   ::SEM_INIT(sig_sem, 0);
2060 }
2061 
2062 void os::signal_notify(int sig) {
2063   Atomic::inc(&pending_signals[sig]);
2064   ::SEM_POST(sig_sem);
2065 }
2066 
2067 static int check_pending_signals(bool wait) {
2068   Atomic::store(0, &sigint_count);
2069   for (;;) {
2070     for (int i = 0; i < NSIG + 1; i++) {
2071       jint n = pending_signals[i];
2072       if (n > 0 && n == Atomic::cmpxchg(n - 1, &pending_signals[i], n)) {




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 #ifdef __APPLE__
1957 // OS X doesn't support unamed POSIX semaphores, so the implementation in os_posix.cpp can't be used.
1958 
1959 Semaphore::Semaphore(uint value, uint max) : _semaphore(0) {
1960   SEM_INIT(_semaphore, value);
1961 }
1962 
1963 Semaphore::~Semaphore() {
1964   SEM_DESTROY(_semaphore);
1965 }
1966 
1967 void Semaphore::signal() {
1968   SEM_POST(_semaphore);
1969 }
1970 
1971 void Semaphore::signal(uint count) {
1972   for (uint i = 0; i < count; i++) {
1973     signal();
1974   }
1975 }
1976 
1977 void Semaphore::wait() {

1978   while (SEM_WAIT(_semaphore) == KERN_ABORTED) {



1979     // Semaphore was interrupted. Retry.
1980   }
1981 }
1982 
1983 class BsdSemaphore : public Semaphore {
1984  private:
1985   static jlong currenttime() {
1986     struct timeval tv;
1987     gettimeofday(&tv, NULL);
1988     return (tv.tv_sec * NANOSECS_PER_SEC) + (tv.tv_usec * 1000);
1989   }
1990 
1991  public:
1992   BsdSemaphore(uint value = 0) : Semaphore(value) {}
1993 
1994   bool trywait() {
1995     return timedwait(0, 0);
1996   }
1997 
1998   bool timedwait(unsigned int sec, int nsec) {
1999     kern_return_t kr = KERN_ABORTED;
2000     mach_timespec_t waitspec;
2001     waitspec.tv_sec = sec;
2002     waitspec.tv_nsec = nsec;
2003 
2004     jlong starttime = currenttime();
2005 
2006     kr = semaphore_timedwait(_semaphore, waitspec);
2007     while (kr == KERN_ABORTED) {
2008       jlong totalwait = (sec * NANOSECS_PER_SEC) + nsec;
2009 
2010       jlong current = currenttime();
2011       jlong passedtime = current - starttime;
2012 
2013       if (passedtime >= totalwait) {
2014         waitspec.tv_sec = 0;
2015         waitspec.tv_nsec = 0;
2016       } else {
2017         jlong waittime = totalwait - (current - starttime);
2018         waitspec.tv_sec = waittime / NANOSECS_PER_SEC;
2019         waitspec.tv_nsec = waittime % NANOSECS_PER_SEC;
2020       }
2021 
2022       kr = semaphore_timedwait(_semaphore, waitspec);
2023     }
2024 
2025     return kr == KERN_SUCCESS;
2026   }
2027 };
2028 
2029 #else
2030 
2031 class BsdSemaphore : public os::PosixSemaphore {
2032  public:
2033   BsdSemaphore(uint value = 0) : os::PosixSemaphore(value) {}
2034 
2035   bool BsdSemaphore::trywait() {
2036     return sem_trywait(&_semaphore) == 0;
2037   }
2038 
2039   bool BsdSemaphore::timedwait(unsigned int sec, int nsec) {
2040     struct timespec ts;
2041     unpackTime(&ts, false, (sec * NANOSECS_PER_SEC) + nsec);
2042 
2043     return os::PosixSemaphore::timedwait(ts);









2044   }
2045 };

2046 
2047 #endif // __APPLE__
2048 
2049 static os_semaphore_t sig_sem;
2050 static BsdSemaphore sr_semaphore;
2051 
2052 void os::signal_init_pd() {
2053   // Initialize signal structures
2054   ::memset((void*)pending_signals, 0, sizeof(pending_signals));
2055 
2056   // Initialize signal semaphore
2057   ::SEM_INIT(sig_sem, 0);
2058 }
2059 
2060 void os::signal_notify(int sig) {
2061   Atomic::inc(&pending_signals[sig]);
2062   ::SEM_POST(sig_sem);
2063 }
2064 
2065 static int check_pending_signals(bool wait) {
2066   Atomic::store(0, &sigint_count);
2067   for (;;) {
2068     for (int i = 0; i < NSIG + 1; i++) {
2069       jint n = pending_signals[i];
2070       if (n > 0 && n == Atomic::cmpxchg(n - 1, &pending_signals[i], n)) {


< prev index next >