Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb_moodycamel::LightweightSemaphore Class Reference

Public Types

typedef std::make_signed< std::size_t >::type ssize_t

Public Member Functions

 LightweightSemaphore (ssize_t initialCount=0)
bool tryWait ()
bool wait ()
bool wait (std::int64_t timeout_usecs)
ssize_t tryWaitMany (ssize_t max)
ssize_t waitMany (ssize_t max, std::int64_t timeout_usecs)
ssize_t waitMany (ssize_t max)
void signal (ssize_t count=1)
ssize_t availableApprox () const

Private Member Functions

bool waitWithPartialSpinning (std::int64_t timeout_usecs=-1)
ssize_t waitManyWithPartialSpinning (ssize_t max, std::int64_t timeout_usecs=-1)

Private Attributes

std::atomic< ssize_t > m_count
details::Semaphore m_sema

Constructor & Destructor Documentation

◆ LightweightSemaphore()

duckdb_moodycamel::LightweightSemaphore::LightweightSemaphore ( ssize_t initialCount = 0)
inline
334858 : m_count(initialCount)
334859 {
334860 assert(initialCount >= 0);
334861 }
#define assert(x)
Definition duckdb.cpp:486071

Member Function Documentation

◆ waitWithPartialSpinning()

bool duckdb_moodycamel::LightweightSemaphore::waitWithPartialSpinning ( std::int64_t timeout_usecs = -1)
inlineprivate
334781 {
334782 ssize_t oldCount;
334783 // Is there a better way to set the initial spin count?
334784 // If we lower it to 1000, testBenaphore becomes 15x slower on my Core i7-5930K Windows PC,
334785 // as threads start hitting the kernel semaphore.
334786 int spin = 10000;
334787 while (--spin >= 0)
334788 {
334789 oldCount = m_count.load(std::memory_order_relaxed);
334790 if ((oldCount > 0) && m_count.compare_exchange_strong(oldCount, oldCount - 1, std::memory_order_acquire, std::memory_order_relaxed))
334791 return true;
334792 std::atomic_signal_fence(std::memory_order_acquire); // Prevent the compiler from collapsing the loop.
334793 }
334794 oldCount = m_count.fetch_sub(1, std::memory_order_acquire);
334795 if (oldCount > 0)
334796 return true;
334797 if (timeout_usecs < 0)
334798 return m_sema.wait();
334799 if (m_sema.timed_wait((std::uint64_t)timeout_usecs))
334800 return true;
334801 // At this point, we've timed out waiting for the semaphore, but the
334802 // count is still decremented indicating we may still be waiting on
334803 // it. So we have to re-adjust the count, but only if the semaphore
334804 // wasn't signaled enough times for us too since then. If it was, we
334805 // need to release the semaphore too.
334806 while (true)
334807 {
334808 oldCount = m_count.load(std::memory_order_acquire);
334809 if (oldCount >= 0 && m_sema.try_wait())
334810 return true;
334811 if (oldCount < 0 && m_count.compare_exchange_strong(oldCount, oldCount + 1, std::memory_order_relaxed, std::memory_order_relaxed))
334812 return false;
334813 }
334814 }

◆ waitManyWithPartialSpinning()

ssize_t duckdb_moodycamel::LightweightSemaphore::waitManyWithPartialSpinning ( ssize_t max,
std::int64_t timeout_usecs = -1 )
inlineprivate
334817 {
334818 assert(max > 0);
334819 ssize_t oldCount;
334820 int spin = 10000;
334821 while (--spin >= 0)
334822 {
334823 oldCount = m_count.load(std::memory_order_relaxed);
334824 if (oldCount > 0)
334825 {
334826 ssize_t newCount = oldCount > max ? oldCount - max : 0;
334827 if (m_count.compare_exchange_strong(oldCount, newCount, std::memory_order_acquire, std::memory_order_relaxed))
334828 return oldCount - newCount;
334829 }
334830 std::atomic_signal_fence(std::memory_order_acquire);
334831 }
334832 oldCount = m_count.fetch_sub(1, std::memory_order_acquire);
334833 if (oldCount <= 0)
334834 {
334835 if (timeout_usecs < 0)
334836 {
334837 if (!m_sema.wait())
334838 return 0;
334839 }
334840 else if (!m_sema.timed_wait((std::uint64_t)timeout_usecs))
334841 {
334842 while (true)
334843 {
334844 oldCount = m_count.load(std::memory_order_acquire);
334845 if (oldCount >= 0 && m_sema.try_wait())
334846 break;
334847 if (oldCount < 0 && m_count.compare_exchange_strong(oldCount, oldCount + 1, std::memory_order_relaxed, std::memory_order_relaxed))
334848 return 0;
334849 }
334850 }
334851 }
334852 if (max > 1)
334853 return 1 + tryWaitMany(max - 1);
334854 return 1;
334855 }
static softfloat max()

◆ tryWait()

bool duckdb_moodycamel::LightweightSemaphore::tryWait ( )
inline
334864 {
334865 ssize_t oldCount = m_count.load(std::memory_order_relaxed);
334866 while (oldCount > 0)
334867 {
334868 if (m_count.compare_exchange_weak(oldCount, oldCount - 1, std::memory_order_acquire, std::memory_order_relaxed))
334869 return true;
334870 }
334871 return false;
334872 }

◆ wait() [1/2]

bool duckdb_moodycamel::LightweightSemaphore::wait ( )
inline
334875 {
334876 return tryWait() || waitWithPartialSpinning();
334877 }

◆ wait() [2/2]

bool duckdb_moodycamel::LightweightSemaphore::wait ( std::int64_t timeout_usecs)
inline
334880 {
334881 return tryWait() || waitWithPartialSpinning(timeout_usecs);
334882 }

◆ tryWaitMany()

ssize_t duckdb_moodycamel::LightweightSemaphore::tryWaitMany ( ssize_t max)
inline
334886 {
334887 assert(max >= 0);
334888 ssize_t oldCount = m_count.load(std::memory_order_relaxed);
334889 while (oldCount > 0)
334890 {
334891 ssize_t newCount = oldCount > max ? oldCount - max : 0;
334892 if (m_count.compare_exchange_weak(oldCount, newCount, std::memory_order_acquire, std::memory_order_relaxed))
334893 return oldCount - newCount;
334894 }
334895 return 0;
334896 }

◆ waitMany() [1/2]

ssize_t duckdb_moodycamel::LightweightSemaphore::waitMany ( ssize_t max,
std::int64_t timeout_usecs )
inline
334900 {
334901 assert(max >= 0);
334902 ssize_t result = tryWaitMany(max);
334903 if (result == 0 && max > 0)
334904 result = waitManyWithPartialSpinning(max, timeout_usecs);
334905 return result;
334906 }

◆ waitMany() [2/2]

ssize_t duckdb_moodycamel::LightweightSemaphore::waitMany ( ssize_t max)
inline
334909 {
334910 ssize_t result = waitMany(max, -1);
334911 assert(result > 0);
334912 return result;
334913 }

◆ signal()

void duckdb_moodycamel::LightweightSemaphore::signal ( ssize_t count = 1)
inline
334916 {
334917 assert(count >= 0);
334918 ssize_t oldCount = m_count.fetch_add(count, std::memory_order_release);
334919 ssize_t toRelease = -oldCount < count ? -oldCount : count;
334920 if (toRelease > 0)
334921 {
334922 m_sema.signal((int)toRelease);
334923 }
334924 }

◆ availableApprox()

ssize_t duckdb_moodycamel::LightweightSemaphore::availableApprox ( ) const
inline
334927 {
334928 ssize_t count = m_count.load(std::memory_order_relaxed);
334929 return count > 0 ? count : 0;
334930 }

The documentation for this class was generated from the following file: