Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/pal/src/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
#cmakedefine01 HAVE_CLOCK_MONOTONIC_COARSE
#cmakedefine01 HAVE_MACH_ABSOLUTE_TIME
#cmakedefine01 HAVE_CLOCK_THREAD_CPUTIME
#cmakedefine01 HAVE_PTHREAD_CONDATTR_SETCLOCK
#cmakedefine01 STATVFS64_PROTOTYPE_BROKEN
#cmakedefine01 HAVE_MMAP_DEV_ZERO
#cmakedefine01 MMAP_IGNORES_HINT
Expand Down
3 changes: 3 additions & 0 deletions src/pal/src/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,9 @@ int main()

exit(ret);
}" HAVE_CLOCK_MONOTONIC)

check_library_exists(pthread pthread_condattr_setclock "" HAVE_PTHREAD_CONDATTR_SETCLOCK)

check_cxx_source_runs("
#include <stdlib.h>
#include <time.h>
Expand Down
81 changes: 65 additions & 16 deletions src/pal/src/synchmgr/synchmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ namespace CorUnix
if (dwTimeout != INFINITE)
{
// Calculate absolute timeout
palErr = GetAbsoluteTimeout(dwTimeout, &tsAbsTmo);
palErr = GetAbsoluteTimeout(dwTimeout, &tsAbsTmo, /*fPreferMonotonicClock*/ TRUE);
if (NO_ERROR != palErr)
{
ERROR("Failed to convert timeout to absolute timeout\n");
Expand Down Expand Up @@ -1572,7 +1572,7 @@ namespace CorUnix
ptnwdWorkerThreadNativeData =
&pSynchManager->m_pthrWorker->synchronizationInfo.m_tnwdNativeData;

palErr = GetAbsoluteTimeout(WorkerThreadTerminationTimeout, &tsAbsTmo);
palErr = GetAbsoluteTimeout(WorkerThreadTerminationTimeout, &tsAbsTmo, /*fPreferMonotonicClock*/ TRUE);
if (NO_ERROR != palErr)
{
ERROR("Failed to convert timeout to absolute timeout\n");
Expand Down Expand Up @@ -4078,6 +4078,9 @@ namespace CorUnix
int iRet;
const int MaxUnavailableResourceRetries = 10;
int iEagains;
pthread_condattr_t attrs;
pthread_condattr_t *attrsPtr = nullptr;

m_shridWaitAwakened = RawSharedObjectAlloc(sizeof(DWORD),
DefaultSharedPool);
if (NULLSharedID == m_shridWaitAwakened)
Expand All @@ -4096,6 +4099,36 @@ namespace CorUnix
VolatileStore<DWORD>(pdwWaitState, TWS_ACTIVE);
m_tsThreadState = TS_STARTING;

#if HAVE_CLOCK_MONOTONIC && HAVE_PTHREAD_CONDATTR_SETCLOCK
attrsPtr = &attrs;
iRet = pthread_condattr_init(&attrs);
if (0 != iRet)
{
ERROR("Failed to initialize thread synchronization condition attribute "
"[error=%d (%s)]\n", iRet, strerror(iRet));
if (ENOMEM == iRet)
{
palErr = ERROR_NOT_ENOUGH_MEMORY;
}
else
{
palErr = ERROR_INTERNAL_ERROR;
}
goto IPrC_exit;
}

// Ensure that the pthread_cond_timedwait will use CLOCK_MONOTONIC
iRet = pthread_condattr_setclock(&attrs, CLOCK_MONOTONIC);
if (0 != iRet)
{
ERROR("Failed set thread synchronization condition timed wait clock "
"[error=%d (%s)]\n", iRet, strerror(iRet));
palErr = ERROR_INTERNAL_ERROR;
pthread_condattr_destroy(&attrs);
goto IPrC_exit;
}
#endif // HAVE_CLOCK_MONOTONIC && HAVE_PTHREAD_CONDATTR_SETCLOCK

iEagains = 0;
Mutex_retry:
iRet = pthread_mutex_init(&m_tnwdNativeData.mutex, NULL);
Expand All @@ -4121,7 +4154,9 @@ namespace CorUnix

iEagains = 0;
Cond_retry:
iRet = pthread_cond_init(&m_tnwdNativeData.cond, NULL);

iRet = pthread_cond_init(&m_tnwdNativeData.cond, attrsPtr);

if (0 != iRet)
{
ERROR("Failed creating thread synchronization condition "
Expand All @@ -4146,6 +4181,10 @@ namespace CorUnix
m_tnwdNativeData.fInitialized = true;

IPrC_exit:
if (attrsPtr != nullptr)
{
pthread_condattr_destroy(attrsPtr);
}
if (NO_ERROR != palErr)
{
m_tsThreadState = TS_FAILED;
Expand Down Expand Up @@ -4515,27 +4554,37 @@ namespace CorUnix

Converts a relative timeout to an absolute one.
--*/
PAL_ERROR CPalSynchronizationManager::GetAbsoluteTimeout(DWORD dwTimeout, struct timespec * ptsAbsTmo)
PAL_ERROR CPalSynchronizationManager::GetAbsoluteTimeout(DWORD dwTimeout, struct timespec * ptsAbsTmo, BOOL fPreferMonotonicClock)
{
PAL_ERROR palErr = NO_ERROR;
int iRet;

#if HAVE_WORKING_CLOCK_GETTIME
// Not every platform implements a (working) clock_gettime
iRet = clock_gettime(CLOCK_REALTIME, ptsAbsTmo);
#elif HAVE_WORKING_GETTIMEOFDAY
// Not every platform implements a (working) gettimeofday
struct timeval tv;
iRet = gettimeofday(&tv, NULL);
if (0 == iRet)
#if HAVE_CLOCK_MONOTONIC && HAVE_PTHREAD_CONDATTR_SETCLOCK
if (fPreferMonotonicClock)
{
ptsAbsTmo->tv_sec = tv.tv_sec;
ptsAbsTmo->tv_nsec = tv.tv_usec * tccMicroSecondsToNanoSeconds;
iRet = clock_gettime(CLOCK_MONOTONIC, ptsAbsTmo);
}
else
{
#endif
#if HAVE_WORKING_CLOCK_GETTIME
// Not every platform implements a (working) clock_gettime
iRet = clock_gettime(CLOCK_REALTIME, ptsAbsTmo);
#elif HAVE_WORKING_GETTIMEOFDAY
// Not every platform implements a (working) gettimeofday
struct timeval tv;
iRet = gettimeofday(&tv, NULL);
if (0 == iRet)
{
ptsAbsTmo->tv_sec = tv.tv_sec;
ptsAbsTmo->tv_nsec = tv.tv_usec * tccMicroSecondsToNanoSeconds;
}
#else
#error "Don't know how to get hi-res current time on this platform"
#error "Don't know how to get hi-res current time on this platform"
#endif // HAVE_WORKING_CLOCK_GETTIME, HAVE_WORKING_GETTIMEOFDAY

#if HAVE_CLOCK_MONOTONIC && HAVE_PTHREAD_CONDATTR_SETCLOCK
}
#endif
if (0 == iRet)
{
ptsAbsTmo->tv_sec += dwTimeout / tccSecondsToMillieSeconds;
Expand Down
3 changes: 2 additions & 1 deletion src/pal/src/synchmgr/synchmanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,8 @@ namespace CorUnix

static PAL_ERROR GetAbsoluteTimeout(
DWORD dwTimeout,
struct timespec * ptsAbsTmo);
struct timespec * ptsAbsTmo,
BOOL fPreferMonotonicClock);
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/pal/src/synchobj/mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ MutexTryAcquireLockResult MutexHelpers::TryAcquireLock(pthread_mutex_t *mutex, D
default:
{
struct timespec timeoutTime;
PAL_ERROR palError = CPalSynchronizationManager::GetAbsoluteTimeout(timeoutMilliseconds, &timeoutTime);
PAL_ERROR palError = CPalSynchronizationManager::GetAbsoluteTimeout(timeoutMilliseconds, &timeoutTime, /*fPreferMonotonicClock*/ FALSE);
_ASSERTE(palError == NO_ERROR);
lockResult = pthread_mutex_timedlock(mutex, &timeoutTime);
break;
Expand Down