comparison src/os/linux/vm/os_linux.cpp @ 17738:8cfe6fdbb99a

8037340: Linux semaphores to use CLOCK_REALTIME Reviewed-by: dholmes, sla
author mgronlun
date Thu, 20 Mar 2014 17:31:54 +0100
parents 0e6af9b390af
children 91dc38ae09f3
comparison
equal deleted inserted replaced
17737:0d2ce7411240 17738:8cfe6fdbb99a
106 #ifndef RUSAGE_THREAD 106 #ifndef RUSAGE_THREAD
107 #define RUSAGE_THREAD (1) /* only the calling thread */ 107 #define RUSAGE_THREAD (1) /* only the calling thread */
108 #endif 108 #endif
109 109
110 #define MAX_PATH (2 * K) 110 #define MAX_PATH (2 * K)
111
112 #define MAX_SECS 100000000
111 113
112 // for timer info max values which include all bits 114 // for timer info max values which include all bits
113 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF) 115 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)
114 116
115 #define LARGEPAGES_BIT (1 << 6) 117 #define LARGEPAGES_BIT (1 << 6)
2469 bool timedwait(unsigned int sec, int nsec); 2471 bool timedwait(unsigned int sec, int nsec);
2470 private: 2472 private:
2471 sem_t _semaphore; 2473 sem_t _semaphore;
2472 }; 2474 };
2473 2475
2474
2475 Semaphore::Semaphore() { 2476 Semaphore::Semaphore() {
2476 sem_init(&_semaphore, 0, 0); 2477 sem_init(&_semaphore, 0, 0);
2477 } 2478 }
2478 2479
2479 Semaphore::~Semaphore() { 2480 Semaphore::~Semaphore() {
2491 bool Semaphore::trywait() { 2492 bool Semaphore::trywait() {
2492 return sem_trywait(&_semaphore) == 0; 2493 return sem_trywait(&_semaphore) == 0;
2493 } 2494 }
2494 2495
2495 bool Semaphore::timedwait(unsigned int sec, int nsec) { 2496 bool Semaphore::timedwait(unsigned int sec, int nsec) {
2497
2496 struct timespec ts; 2498 struct timespec ts;
2497 unpackTime(&ts, false, (sec * NANOSECS_PER_SEC) + nsec); 2499 // Semaphore's are always associated with CLOCK_REALTIME
2500 os::Linux::clock_gettime(CLOCK_REALTIME, &ts);
2501 // see unpackTime for discussion on overflow checking
2502 if (sec >= MAX_SECS) {
2503 ts.tv_sec += MAX_SECS;
2504 ts.tv_nsec = 0;
2505 } else {
2506 ts.tv_sec += sec;
2507 ts.tv_nsec += nsec;
2508 if (ts.tv_nsec >= NANOSECS_PER_SEC) {
2509 ts.tv_nsec -= NANOSECS_PER_SEC;
2510 ++ts.tv_sec; // note: this must be <= max_secs
2511 }
2512 }
2498 2513
2499 while (1) { 2514 while (1) {
2500 int result = sem_timedwait(&_semaphore, &ts); 2515 int result = sem_timedwait(&_semaphore, &ts);
2501 if (result == 0) { 2516 if (result == 0) {
2502 return true; 2517 return true;
5806 * on the condvar. Contention seen when trying to park implies that someone 5821 * on the condvar. Contention seen when trying to park implies that someone
5807 * is unparking you, so don't wait. And spurious returns are fine, so there 5822 * is unparking you, so don't wait. And spurious returns are fine, so there
5808 * is no need to track notifications. 5823 * is no need to track notifications.
5809 */ 5824 */
5810 5825
5811 #define MAX_SECS 100000000
5812 /* 5826 /*
5813 * This code is common to linux and solaris and will be moved to a 5827 * This code is common to linux and solaris and will be moved to a
5814 * common place in dolphin. 5828 * common place in dolphin.
5815 * 5829 *
5816 * The passed in time value is either a relative time in nanoseconds 5830 * The passed in time value is either a relative time in nanoseconds