From d7b65f764b3f7e9c2d412c4692a375d84ea8935d Mon Sep 17 00:00:00 2001 From: Mattias Ellert Date: Mon, 7 Dec 2020 04:47:42 +0100 Subject: [PATCH 1/2] 1 ms = 1000000 ns --- src/XrdSys/XrdSysPthread.hh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/XrdSys/XrdSysPthread.hh b/src/XrdSys/XrdSysPthread.hh index aae295b5dc6..de680b30d12 100644 --- a/src/XrdSys/XrdSysPthread.hh +++ b/src/XrdSys/XrdSysPthread.hh @@ -175,9 +175,9 @@ inline int TimedLock( int wait_ms ) { struct timespec wait, cur, dur; get_apple_realtime(wait); - wait.tv_nsec += wait_ms * 100000; - wait.tv_sec += (wait.tv_nsec / 100000000); - wait.tv_nsec = wait.tv_nsec % 100000000; + wait.tv_nsec += wait_ms * 1000000; + wait.tv_sec += (wait.tv_nsec / 1000000000); + wait.tv_nsec = wait.tv_nsec % 1000000000; int rc; while( ( rc = pthread_mutex_trylock( &cs ) ) == EBUSY ) @@ -210,9 +210,9 @@ inline int TimedLock( int wait_ms ) inline int TimedLock(int wait_ms) {struct timespec wait; clock_gettime(CLOCK_REALTIME, &wait); - wait.tv_nsec += wait_ms * 100000; - wait.tv_sec += (wait.tv_nsec / 100000000); - wait.tv_nsec = wait.tv_nsec % 100000000; + wait.tv_nsec += wait_ms * 1000000; + wait.tv_sec += (wait.tv_nsec / 1000000000); + wait.tv_nsec = wait.tv_nsec % 1000000000; return !pthread_mutex_timedlock(&cs, &wait); } #endif From 83489215dc8184048f318001b0f6652b2b5da01c Mon Sep 17 00:00:00 2001 From: Mattias Ellert Date: Mon, 7 Dec 2020 10:57:17 +0100 Subject: [PATCH 2/2] Avoid overflow when XrdSysMutex::TimedLock(int wait_ms) is called with wait_ms > 3294 ms --- src/XrdSys/XrdSysPthread.hh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/XrdSys/XrdSysPthread.hh b/src/XrdSys/XrdSysPthread.hh index de680b30d12..7036d44e343 100644 --- a/src/XrdSys/XrdSysPthread.hh +++ b/src/XrdSys/XrdSysPthread.hh @@ -175,7 +175,8 @@ inline int TimedLock( int wait_ms ) { struct timespec wait, cur, dur; get_apple_realtime(wait); - wait.tv_nsec += wait_ms * 1000000; + wait.tv_sec += (wait_ms / 1000); + wait.tv_nsec += (wait_ms % 1000) * 1000000; wait.tv_sec += (wait.tv_nsec / 1000000000); wait.tv_nsec = wait.tv_nsec % 1000000000; @@ -210,7 +211,8 @@ inline int TimedLock( int wait_ms ) inline int TimedLock(int wait_ms) {struct timespec wait; clock_gettime(CLOCK_REALTIME, &wait); - wait.tv_nsec += wait_ms * 1000000; + wait.tv_sec += (wait_ms / 1000); + wait.tv_nsec += (wait_ms % 1000) * 1000000; wait.tv_sec += (wait.tv_nsec / 1000000000); wait.tv_nsec = wait.tv_nsec % 1000000000; return !pthread_mutex_timedlock(&cs, &wait);