Skip to content

Commit

Permalink
thread: rename spin-lock in spin-mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
quesnel committed May 2, 2024
1 parent 0dc1981 commit efc831f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
6 changes: 3 additions & 3 deletions app/gui/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class notification_manager
private:
data_array<notification, notification_id> data;
ring_buffer<notification_id> r_buffer;
spin_lock mutex;
spin_mutex mutex;
};

//! @brief Show notification into a classical window in botton.
Expand Down Expand Up @@ -606,7 +606,7 @@ struct simulation_editor {
models_to_move; /**< Online simulation created models need to use ImNodes
API to move into the canvas. */

spin_lock mutex; /**< Sharing the simulation data from gui's tasks and gui's
spin_mutex mutex; /**< Sharing the simulation data from gui's tasks and gui's
draw functions. */
};

Expand Down Expand Up @@ -799,7 +799,7 @@ class component_selector
int files = 0; //! Number of component in registred directories
int unsaved = 0; //! Number of unsaved component

spin_lock m_mutex;
spin_mutex m_mutex;
};

class component_model_selector
Expand Down
12 changes: 6 additions & 6 deletions lib/include/irritator/modeling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ struct registred_path {
state status = state::unread;
bitflags<reg_flags> flags = reg_flags::none;
i8 priority = 0;
spin_lock mutex;
spin_mutex mutex;
};

class dir_path
Expand Down Expand Up @@ -801,7 +801,7 @@ class dir_path

state status = state::unread;
bitflags<dir_flags> flags = dir_flags::none;
spin_lock mutex;
spin_mutex mutex;

/**
* Refresh the `children` vector with new file in the filesystem.
Expand Down Expand Up @@ -837,7 +837,7 @@ struct file_path {
file_type type{ file_type::undefined_file };
state status = state::unread;
bitflags<file_flags> flags = file_flags::none;
spin_lock mutex;
spin_mutex mutex;
};

struct modeling_initializer {
Expand Down Expand Up @@ -1266,9 +1266,9 @@ class modeling

ring_buffer<log_entry> log_entries;

spin_lock reg_paths_mutex;
spin_lock dir_paths_mutex;
spin_lock file_paths_mutex;
spin_mutex reg_paths_mutex;
spin_mutex dir_paths_mutex;
spin_mutex file_paths_mutex;
};

class project
Expand Down
38 changes: 19 additions & 19 deletions lib/include/irritator/thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include <atomic>
#include <chrono>
#include <mutex>
#include <thread>
#include <type_traits>

Expand All @@ -33,30 +32,31 @@ static inline constexpr auto main_task_size = 2;
static inline constexpr auto max_temp_worker = 8;
static inline constexpr auto max_threads = main_task_size + max_temp_worker;

//! A `spin-lock` based on `std::atomic_flag::wait` and
//! A `std::mutex` like based on `std::atomic_flag::wait` and
//! `std::atomic_flag::notify_one` standard functions.
class spin_lock
class spin_mutex
{
std::atomic_flag m_flag = ATOMIC_FLAG_INIT;

public:
spin_lock() noexcept = default;
~spin_lock() noexcept = default;
spin_mutex() noexcept = default;
~spin_mutex() noexcept = default;

spin_lock(const spin_lock& other) noexcept;
spin_mutex(const spin_mutex& other) noexcept;

void lock() noexcept;
bool try_lock() noexcept;
void unlock() noexcept;
};

//! A `spin-lock` only for test. Burst CPU until another thread unlock.
class spin_bad_lock
//! A `std::mutex` like based on `std::atomic_flag` and
//! `std::thread::yied`.
class spin_yield_mutex_lock
{
std::atomic_flag m_flag = ATOMIC_FLAG_INIT;

public:
spin_bad_lock() noexcept;
spin_yield_mutex_lock() noexcept;
bool try_lock() noexcept;
void lock() noexcept;
void unlock() noexcept;
Expand Down Expand Up @@ -290,22 +290,22 @@ class task_manager
// spin_lock
//

inline spin_lock::spin_lock(const spin_lock& /*other*/) noexcept
: spin_lock()
inline spin_mutex::spin_mutex(const spin_mutex& /*other*/) noexcept
: spin_mutex()
{}

inline void spin_lock::lock() noexcept
inline void spin_mutex::lock() noexcept
{
while (m_flag.test_and_set(std::memory_order_acquire))
m_flag.wait(true, std::memory_order_relaxed);
}

inline bool spin_lock::try_lock() noexcept
inline bool spin_mutex::try_lock() noexcept
{
return not m_flag.test_and_set(std::memory_order_acquire);
}

inline void spin_lock::unlock() noexcept
inline void spin_mutex::unlock() noexcept
{
m_flag.clear(std::memory_order_release);
m_flag.notify_one();
Expand All @@ -315,21 +315,21 @@ inline void spin_lock::unlock() noexcept
// spin_bad_lock
//

inline spin_bad_lock::spin_bad_lock() noexcept { m_flag.clear(); }
inline spin_yield_mutex_lock::spin_yield_mutex_lock() noexcept { m_flag.clear(); }

inline bool spin_bad_lock::try_lock() noexcept
inline bool spin_yield_mutex_lock::try_lock() noexcept
{
return !m_flag.test_and_set(std::memory_order_acquire);
}

inline void spin_bad_lock::lock() noexcept
inline void spin_yield_mutex_lock::lock() noexcept
{
for (size_t i = 0; !try_lock(); ++i)
if (i % 100 == 0)
if ((i % 100) == 0)
std::this_thread::yield();
}

inline void spin_bad_lock::unlock() noexcept
inline void spin_yield_mutex_lock::unlock() noexcept
{
m_flag.clear(std::memory_order_release);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/test/threading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ int main()

"spin-lock"_test = [] {
int counter = 0;
irt::spin_lock spin;
irt::spin_mutex spin;

std::thread j1([&counter, &spin]() {
for (int i = 0; i < 1000; ++i) {
Expand Down Expand Up @@ -97,8 +97,8 @@ int main()
};

"scoped-lock"_test = [] {
irt::spin_lock mutex_1;
irt::spin_lock mutex_2;
irt::spin_mutex mutex_1;
irt::spin_mutex mutex_2;

for (int i = 0; i < 100; ++i) {
std::atomic_int mult = 0;
Expand Down

0 comments on commit efc831f

Please sign in to comment.