Commit 3ff10baa by FritzFlorian

Add try_lock method to spin_lock implementations.

parent b9bb90a4
Pipeline #1154 passed with stages
in 3 minutes 35 seconds
...@@ -28,7 +28,7 @@ namespace pls { ...@@ -28,7 +28,7 @@ namespace pls {
tas_spin_lock(const tas_spin_lock& other): flag_{ATOMIC_FLAG_INIT}, yield_at_tries_{other.yield_at_tries_} {} tas_spin_lock(const tas_spin_lock& other): flag_{ATOMIC_FLAG_INIT}, yield_at_tries_{other.yield_at_tries_} {}
void lock(); void lock();
bool try_lock(); bool try_lock(unsigned int num_tries=1);
void unlock(); void unlock();
}; };
} }
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
#ifndef PLS_TTAS_SPIN_LOCK_H #ifndef PLS_TTAS_SPIN_LOCK_H
#define PLS_TTAS_SPIN_LOCK_H #define PLS_TTAS_SPIN_LOCK_H
#include "tas_spin_lock.h"
#include <atomic> #include <atomic>
#include <iostream> #include <iostream>
...@@ -20,7 +18,7 @@ namespace pls { ...@@ -20,7 +18,7 @@ namespace pls {
*/ */
class ttas_spin_lock { class ttas_spin_lock {
std::atomic<int> flag_; std::atomic<int> flag_;
unsigned int yield_at_tries_; const unsigned int yield_at_tries_;
public: public:
...@@ -28,7 +26,7 @@ namespace pls { ...@@ -28,7 +26,7 @@ namespace pls {
ttas_spin_lock(const ttas_spin_lock& other): flag_{0}, yield_at_tries_{other.yield_at_tries_} {} ttas_spin_lock(const ttas_spin_lock& other): flag_{0}, yield_at_tries_{other.yield_at_tries_} {}
void lock(); void lock();
bool try_lock(); bool try_lock(unsigned int num_tries=1);
void unlock(); void unlock();
}; };
} }
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#ifdef ENABLE_EASY_PROFILER #ifdef ENABLE_EASY_PROFILER
#include <easy/profiler.h> #include <easy/profiler.h>
#include <easy/arbitrary_value.h>
#define PROFILE_WORK_BLOCK(msg) EASY_BLOCK(msg, profiler::colors::LightGreen) #define PROFILE_WORK_BLOCK(msg) EASY_BLOCK(msg, profiler::colors::LightGreen)
#define PROFILE_FORK_JOIN_STEALING(msg) EASY_BLOCK(msg, profiler::colors::LightBlue) #define PROFILE_FORK_JOIN_STEALING(msg) EASY_BLOCK(msg, profiler::colors::LightBlue)
...@@ -16,6 +17,8 @@ ...@@ -16,6 +17,8 @@
#define PROFILE_ENABLE EASY_PROFILER_ENABLE #define PROFILE_ENABLE EASY_PROFILER_ENABLE
#define PROFILE_MAIN_THREAD EASY_MAIN_THREAD #define PROFILE_MAIN_THREAD EASY_MAIN_THREAD
#define PROFILE_VALUE(name, value) EASY_VALUE(name, value)
#else //ENABLE_EASY_PROFILER #else //ENABLE_EASY_PROFILER
#define PROFILE_WORK_BLOCK(msg) #define PROFILE_WORK_BLOCK(msg)
...@@ -29,5 +32,7 @@ ...@@ -29,5 +32,7 @@
#define PROFILE_ENABLE #define PROFILE_ENABLE
#define PROFILE_MAIN_THREAD #define PROFILE_MAIN_THREAD
#define PROFILE_VALUE(name, value)
#endif //ENABLE_EASY_PROFILER #endif //ENABLE_EASY_PROFILER
#endif //PLS_PROFILER_H #endif //PLS_PROFILER_H
...@@ -15,8 +15,15 @@ namespace pls { ...@@ -15,8 +15,15 @@ namespace pls {
} }
} }
bool tas_spin_lock::try_lock() { bool tas_spin_lock::try_lock(unsigned int num_tries) {
return flag_.test_and_set(std::memory_order_acquire) == 0; PROFILE_LOCK("Try Acquire Lock")
while (flag_.test_and_set(std::memory_order_acquire)) {
num_tries--;
if (num_tries <= 0) {
return false;
}
}
return true;
} }
void tas_spin_lock::unlock() { void tas_spin_lock::unlock() {
......
...@@ -21,9 +21,22 @@ namespace pls { ...@@ -21,9 +21,22 @@ namespace pls {
} while (!flag_.compare_exchange_weak(expected, 1, std::memory_order_acquire)); } while (!flag_.compare_exchange_weak(expected, 1, std::memory_order_acquire));
} }
bool ttas_spin_lock::try_lock() { bool ttas_spin_lock::try_lock(unsigned int num_tries) {
PROFILE_LOCK("Try Acquire Lock")
int expected = 0; int expected = 0;
return flag_.load(std::memory_order_relaxed) == 0 && flag_.compare_exchange_weak(expected, 1, std::memory_order_acquire);
do {
while (flag_.load(std::memory_order_relaxed) == 1) {
num_tries--;
if (num_tries <= 0) {
return false;
}
}
expected = 0;
} while (!flag_.compare_exchange_weak(expected, 1, std::memory_order_acquire));
return true;
} }
void ttas_spin_lock::unlock() { void ttas_spin_lock::unlock() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment