2012-04-29 20:52:25 +02:00
|
|
|
//
|
|
|
|
// Mutex.h
|
|
|
|
//
|
|
|
|
// Library: Foundation
|
|
|
|
// Package: Threading
|
|
|
|
// Module: Mutex
|
|
|
|
//
|
|
|
|
// Definition of the Mutex and FastMutex classes.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
|
|
|
|
// and Contributors.
|
|
|
|
//
|
2014-05-04 21:02:42 +02:00
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
2012-04-29 20:52:25 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef Foundation_Mutex_INCLUDED
|
|
|
|
#define Foundation_Mutex_INCLUDED
|
|
|
|
|
|
|
|
|
|
|
|
#include "Poco/Foundation.h"
|
|
|
|
#include "Poco/Exception.h"
|
|
|
|
#include "Poco/ScopedLock.h"
|
2018-06-02 21:02:33 +02:00
|
|
|
#include "Poco/Timestamp.h"
|
2022-07-27 22:19:06 +02:00
|
|
|
#include <atomic>
|
2012-04-29 20:52:25 +02:00
|
|
|
|
2023-05-05 16:49:06 +02:00
|
|
|
#ifdef POCO_ENABLE_STD_MUTEX
|
|
|
|
#include "Poco/Mutex_STD.h"
|
|
|
|
#else
|
2012-04-29 20:52:25 +02:00
|
|
|
#if defined(POCO_OS_FAMILY_WINDOWS)
|
|
|
|
#include "Poco/Mutex_WIN32.h"
|
|
|
|
#elif defined(POCO_VXWORKS)
|
|
|
|
#include "Poco/Mutex_VX.h"
|
|
|
|
#else
|
|
|
|
#include "Poco/Mutex_POSIX.h"
|
|
|
|
#endif
|
2023-05-05 16:49:06 +02:00
|
|
|
#endif
|
2012-04-29 20:52:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
namespace Poco {
|
|
|
|
|
|
|
|
|
|
|
|
class Foundation_API Mutex: private MutexImpl
|
2022-07-07 11:18:20 +02:00
|
|
|
/// A Mutex (mutual exclusion) is a synchronization
|
2012-04-29 20:52:25 +02:00
|
|
|
/// mechanism used to control access to a shared resource
|
|
|
|
/// in a concurrent (multithreaded) scenario.
|
2022-07-07 11:18:20 +02:00
|
|
|
/// Mutexes are recursive, that is, the same mutex can be
|
2012-04-29 20:52:25 +02:00
|
|
|
/// locked multiple times by the same thread (but, of course,
|
|
|
|
/// not by other threads).
|
|
|
|
/// Using the ScopedLock class is the preferred way to automatically
|
|
|
|
/// lock and unlock a mutex.
|
|
|
|
{
|
|
|
|
public:
|
2020-01-09 10:08:09 +01:00
|
|
|
using ScopedLock = Poco::ScopedLock<Mutex>;
|
2024-08-29 22:03:33 +02:00
|
|
|
using ScopedLockWithUnlock = Poco::ScopedLockWithUnlock<Mutex>;
|
2022-07-07 11:18:20 +02:00
|
|
|
|
2012-04-29 20:52:25 +02:00
|
|
|
Mutex();
|
|
|
|
/// creates the Mutex.
|
2022-07-07 11:18:20 +02:00
|
|
|
|
2012-04-29 20:52:25 +02:00
|
|
|
~Mutex();
|
|
|
|
/// destroys the Mutex.
|
|
|
|
|
|
|
|
void lock();
|
|
|
|
/// Locks the mutex. Blocks if the mutex
|
|
|
|
/// is held by another thread.
|
2022-07-07 11:18:20 +02:00
|
|
|
|
2012-04-29 20:52:25 +02:00
|
|
|
void lock(long milliseconds);
|
|
|
|
/// Locks the mutex. Blocks up to the given number of milliseconds
|
|
|
|
/// if the mutex is held by another thread. Throws a TimeoutException
|
|
|
|
/// if the mutex can not be locked within the given timeout.
|
|
|
|
///
|
2022-07-07 11:18:20 +02:00
|
|
|
/// Performance Note: On most platforms (including Windows), this member function is
|
2012-04-29 20:52:25 +02:00
|
|
|
/// implemented using a loop calling (the equivalent of) tryLock() and Thread::sleep().
|
|
|
|
/// On POSIX platforms that support pthread_mutex_timedlock(), this is used.
|
|
|
|
|
|
|
|
bool tryLock();
|
|
|
|
/// Tries to lock the mutex. Returns false immediately
|
|
|
|
/// if the mutex is already held by another thread.
|
|
|
|
/// Returns true if the mutex was successfully locked.
|
|
|
|
|
|
|
|
bool tryLock(long milliseconds);
|
|
|
|
/// Locks the mutex. Blocks up to the given number of milliseconds
|
|
|
|
/// if the mutex is held by another thread.
|
|
|
|
/// Returns true if the mutex was successfully locked.
|
|
|
|
///
|
2022-07-07 11:18:20 +02:00
|
|
|
/// Performance Note: On most platforms (including Windows), this member function is
|
2012-04-29 20:52:25 +02:00
|
|
|
/// implemented using a loop calling (the equivalent of) tryLock() and Thread::sleep().
|
|
|
|
/// On POSIX platforms that support pthread_mutex_timedlock(), this is used.
|
|
|
|
|
|
|
|
void unlock();
|
|
|
|
/// Unlocks the mutex so that it can be acquired by
|
|
|
|
/// other threads.
|
2022-07-07 11:18:20 +02:00
|
|
|
|
2012-04-29 20:52:25 +02:00
|
|
|
private:
|
|
|
|
Mutex(const Mutex&);
|
|
|
|
Mutex& operator = (const Mutex&);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Foundation_API FastMutex: private FastMutexImpl
|
|
|
|
/// A FastMutex (mutual exclusion) is similar to a Mutex.
|
|
|
|
/// Unlike a Mutex, however, a FastMutex is not recursive,
|
|
|
|
/// which means that a deadlock will occur if the same
|
|
|
|
/// thread tries to lock a mutex it has already locked again.
|
|
|
|
/// Locking a FastMutex is faster than locking a recursive Mutex.
|
|
|
|
/// Using the ScopedLock class is the preferred way to automatically
|
|
|
|
/// lock and unlock a mutex.
|
|
|
|
{
|
|
|
|
public:
|
2020-01-09 10:08:09 +01:00
|
|
|
using ScopedLock = Poco::ScopedLock<FastMutex>;
|
2024-08-29 22:03:33 +02:00
|
|
|
using ScopedLockWithUnlock = Poco::ScopedLockWithUnlock<FastMutex>;
|
2012-04-29 20:52:25 +02:00
|
|
|
|
|
|
|
FastMutex();
|
|
|
|
/// creates the Mutex.
|
2022-07-07 11:18:20 +02:00
|
|
|
|
2012-04-29 20:52:25 +02:00
|
|
|
~FastMutex();
|
|
|
|
/// destroys the Mutex.
|
|
|
|
|
|
|
|
void lock();
|
|
|
|
/// Locks the mutex. Blocks if the mutex
|
|
|
|
/// is held by another thread.
|
|
|
|
|
|
|
|
void lock(long milliseconds);
|
|
|
|
/// Locks the mutex. Blocks up to the given number of milliseconds
|
|
|
|
/// if the mutex is held by another thread. Throws a TimeoutException
|
|
|
|
/// if the mutex can not be locked within the given timeout.
|
|
|
|
///
|
2022-07-07 11:18:20 +02:00
|
|
|
/// Performance Note: On most platforms (including Windows), this member function is
|
2012-04-29 20:52:25 +02:00
|
|
|
/// implemented using a loop calling (the equivalent of) tryLock() and Thread::sleep().
|
|
|
|
/// On POSIX platforms that support pthread_mutex_timedlock(), this is used.
|
|
|
|
|
|
|
|
bool tryLock();
|
|
|
|
/// Tries to lock the mutex. Returns false immediately
|
|
|
|
/// if the mutex is already held by another thread.
|
|
|
|
/// Returns true if the mutex was successfully locked.
|
|
|
|
|
|
|
|
bool tryLock(long milliseconds);
|
|
|
|
/// Locks the mutex. Blocks up to the given number of milliseconds
|
|
|
|
/// if the mutex is held by another thread.
|
|
|
|
/// Returns true if the mutex was successfully locked.
|
|
|
|
///
|
2022-07-07 11:18:20 +02:00
|
|
|
/// Performance Note: On most platforms (including Windows), this member function is
|
2012-04-29 20:52:25 +02:00
|
|
|
/// implemented using a loop calling (the equivalent of) tryLock() and Thread::sleep().
|
|
|
|
/// On POSIX platforms that support pthread_mutex_timedlock(), this is used.
|
|
|
|
|
|
|
|
void unlock();
|
|
|
|
/// Unlocks the mutex so that it can be acquired by
|
|
|
|
/// other threads.
|
2022-07-07 11:18:20 +02:00
|
|
|
|
2012-04-29 20:52:25 +02:00
|
|
|
private:
|
|
|
|
FastMutex(const FastMutex&);
|
|
|
|
FastMutex& operator = (const FastMutex&);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-06-02 21:02:33 +02:00
|
|
|
class Foundation_API SpinlockMutex
|
|
|
|
/// A SpinlockMutex, implemented in terms of std::atomic_flag, as
|
|
|
|
/// busy-wait mutual exclusion.
|
|
|
|
///
|
|
|
|
/// While in some cases (eg. locking small blocks of code)
|
|
|
|
/// busy-waiting may be an optimal solution, in many scenarios
|
2023-03-22 15:11:13 +01:00
|
|
|
/// spinlock may not be the right choice (especially on single-core
|
|
|
|
/// systems) - it is up to the user to choose the proper mutex type
|
|
|
|
/// for their particular case.
|
2018-06-02 21:02:33 +02:00
|
|
|
///
|
|
|
|
/// Works with the ScopedLock class.
|
|
|
|
{
|
|
|
|
public:
|
2020-01-09 10:08:09 +01:00
|
|
|
using ScopedLock = Poco::ScopedLock<SpinlockMutex>;
|
2024-08-29 22:03:33 +02:00
|
|
|
using ScopedLockWithUnlock = Poco::ScopedLockWithUnlock<SpinlockMutex>;
|
2018-06-02 21:02:33 +02:00
|
|
|
|
|
|
|
SpinlockMutex();
|
|
|
|
/// Creates the SpinlockMutex.
|
|
|
|
|
|
|
|
~SpinlockMutex();
|
|
|
|
/// Destroys the SpinlockMutex.
|
|
|
|
|
|
|
|
void lock();
|
|
|
|
/// Locks the mutex. Blocks if the mutex
|
|
|
|
/// is held by another thread.
|
|
|
|
|
|
|
|
void lock(long milliseconds);
|
|
|
|
/// Locks the mutex. Blocks up to the given number of milliseconds
|
|
|
|
/// if the mutex is held by another thread. Throws a TimeoutException
|
|
|
|
/// if the mutex can not be locked within the given timeout.
|
|
|
|
|
|
|
|
bool tryLock();
|
|
|
|
/// Tries to lock the mutex. Returns immediately, false
|
|
|
|
/// if the mutex is already held by another thread, true
|
|
|
|
/// if the mutex was successfully locked.
|
|
|
|
|
|
|
|
bool tryLock(long milliseconds);
|
|
|
|
/// Locks the mutex. Blocks up to the given number of milliseconds
|
|
|
|
/// if the mutex is held by another thread.
|
|
|
|
/// Returns true if the mutex was successfully locked.
|
|
|
|
|
|
|
|
void unlock();
|
|
|
|
/// Unlocks the mutex so that it can be acquired by
|
|
|
|
/// other threads.
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::atomic_flag _flag = ATOMIC_FLAG_INIT;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-04-29 20:52:25 +02:00
|
|
|
class Foundation_API NullMutex
|
|
|
|
/// A NullMutex is an empty mutex implementation
|
|
|
|
/// which performs no locking at all. Useful in policy driven design
|
|
|
|
/// where the type of mutex used can be now a template parameter allowing the user to switch
|
|
|
|
/// between thread-safe and not thread-safe depending on his need
|
|
|
|
/// Works with the ScopedLock class
|
|
|
|
{
|
|
|
|
public:
|
2020-01-09 10:08:09 +01:00
|
|
|
using ScopedLock = Poco::ScopedLock<NullMutex>;
|
2024-08-29 22:03:33 +02:00
|
|
|
using ScopedLockWithUnlock = Poco::ScopedLockWithUnlock<NullMutex>;
|
2022-07-07 11:18:20 +02:00
|
|
|
|
2012-04-29 20:52:25 +02:00
|
|
|
NullMutex()
|
|
|
|
/// Creates the NullMutex.
|
|
|
|
{
|
|
|
|
}
|
2022-07-07 11:18:20 +02:00
|
|
|
|
2012-04-29 20:52:25 +02:00
|
|
|
~NullMutex()
|
|
|
|
/// Destroys the NullMutex.
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void lock()
|
|
|
|
/// Does nothing.
|
|
|
|
{
|
|
|
|
}
|
2022-07-07 11:18:20 +02:00
|
|
|
|
2012-04-29 20:52:25 +02:00
|
|
|
void lock(long)
|
|
|
|
/// Does nothing.
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tryLock()
|
|
|
|
/// Does nothing and always returns true.
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tryLock(long)
|
|
|
|
/// Does nothing and always returns true.
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock()
|
|
|
|
/// Does nothing.
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// inlines
|
|
|
|
//
|
2018-06-02 21:02:33 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Mutex
|
|
|
|
//
|
|
|
|
|
2012-04-29 20:52:25 +02:00
|
|
|
inline void Mutex::lock()
|
|
|
|
{
|
|
|
|
lockImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void Mutex::lock(long milliseconds)
|
|
|
|
{
|
|
|
|
if (!tryLockImpl(milliseconds))
|
|
|
|
throw TimeoutException();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Mutex::tryLock()
|
|
|
|
{
|
|
|
|
return tryLockImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Mutex::tryLock(long milliseconds)
|
|
|
|
{
|
|
|
|
return tryLockImpl(milliseconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void Mutex::unlock()
|
|
|
|
{
|
|
|
|
unlockImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-02 21:02:33 +02:00
|
|
|
//
|
|
|
|
// FastMutex
|
|
|
|
//
|
|
|
|
|
2012-04-29 20:52:25 +02:00
|
|
|
inline void FastMutex::lock()
|
|
|
|
{
|
|
|
|
lockImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void FastMutex::lock(long milliseconds)
|
|
|
|
{
|
|
|
|
if (!tryLockImpl(milliseconds))
|
|
|
|
throw TimeoutException();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool FastMutex::tryLock()
|
|
|
|
{
|
|
|
|
return tryLockImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool FastMutex::tryLock(long milliseconds)
|
|
|
|
{
|
|
|
|
return tryLockImpl(milliseconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void FastMutex::unlock()
|
|
|
|
{
|
|
|
|
unlockImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-02 21:02:33 +02:00
|
|
|
//
|
|
|
|
// SpinlockMutex
|
|
|
|
//
|
|
|
|
|
|
|
|
inline void SpinlockMutex::lock()
|
|
|
|
{
|
|
|
|
while (_flag.test_and_set(std::memory_order_acquire));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void SpinlockMutex::lock(long milliseconds)
|
|
|
|
{
|
|
|
|
Timestamp now;
|
|
|
|
Timestamp::TimeDiff diff(Timestamp::TimeDiff(milliseconds)*1000);
|
|
|
|
while (_flag.test_and_set(std::memory_order_acquire))
|
|
|
|
{
|
|
|
|
if (now.isElapsed(diff)) throw TimeoutException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool SpinlockMutex::tryLock()
|
|
|
|
{
|
|
|
|
return !_flag.test_and_set(std::memory_order_acquire);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool SpinlockMutex::tryLock(long milliseconds)
|
|
|
|
{
|
|
|
|
Timestamp now;
|
|
|
|
Timestamp::TimeDiff diff(Timestamp::TimeDiff(milliseconds)*1000);
|
|
|
|
while (_flag.test_and_set(std::memory_order_acquire))
|
|
|
|
{
|
|
|
|
if (now.isElapsed(diff)) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void SpinlockMutex::unlock()
|
|
|
|
{
|
|
|
|
_flag.clear(std::memory_order_release);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-29 20:52:25 +02:00
|
|
|
} // namespace Poco
|
|
|
|
|
|
|
|
|
|
|
|
#endif // Foundation_Mutex_INCLUDED
|