Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7208a015 authored by Jesse Hall's avatar Jesse Hall Committed by Gerrit Code Review
Browse files

Merge "Mutex: add timedLock() method"

parents fdb3da59 601424a8
Loading
Loading
Loading
Loading
+24 −4
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@
#endif

#include <utils/Errors.h>
#include <utils/Timers.h>

// ---------------------------------------------------------------------------
namespace android {
@@ -58,6 +59,16 @@ public:
    // lock if possible; returns 0 on success, error otherwise
    status_t    tryLock();

#if HAVE_ANDROID_OS
    // lock the mutex, but don't wait longer than timeoutMilliseconds.
    // Returns 0 on success, TIMED_OUT for failure due to timeout expiration.
    //
    // OSX doesn't have pthread_mutex_timedlock() or equivalent. To keep
    // capabilities consistent across host OSes, this method is only available
    // when building Android binaries.
    status_t    timedLock(nsecs_t timeoutMilliseconds);
#endif

    // Manages the mutex automatically. It'll be locked when Autolock is
    // constructed and released when Autolock goes out of scope.
    class Autolock {
@@ -117,6 +128,15 @@ inline void Mutex::unlock() {
inline status_t Mutex::tryLock() {
    return -pthread_mutex_trylock(&mMutex);
}
#if HAVE_ANDROID_OS
inline status_t Mutex::timedLock(nsecs_t timeoutNs) {
    const struct timespec ts = {
        /* .tv_sec = */ timeoutNs / 1000000000,
        /* .tv_nsec = */ timeoutNs % 1000000000,
    };
    return -pthread_mutex_timedlock(&mMutex, &ts);
}
#endif

#endif // HAVE_PTHREADS