Skip to content

Commit

Permalink
Switch to a readers-writer lock for the timer list
Browse files Browse the repository at this point in the history
  • Loading branch information
gansm committed Oct 14, 2023
1 parent a547d48 commit fbb7309
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2023-10-14 Markus Gans <guru.mail@muenster.de>
* Switch to a readers-writer lock for the timer list

2023-10-07 Markus Gans <guru.mail@muenster.de>
* Bugfix: Bottom transparent shadow changes were registered in wrong row

Expand Down
2 changes: 1 addition & 1 deletion examples/xpmimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ void XpmImage::parseXPM3 (XPMdataT& raw_xpm)
{
std::vector<std::string> xpm{};
xpm.reserve(getArraySize(xpm));
std::copy (raw_xpm.begin(), raw_xpm.end(), std::back_inserter(xpm));
std::copy (std::begin(raw_xpm), std::end(raw_xpm), std::back_inserter(xpm));
parseXPM3 (xpm);
}

Expand Down
2 changes: 1 addition & 1 deletion final/ftimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static auto getInstance() -> FTimer<FObject>*
return fobject_timer.get();
}

std::mutex timer_var::mutex{};
std::shared_timed_mutex timer_var::mutex{};

} // namespace internal

Expand Down
16 changes: 8 additions & 8 deletions final/ftimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include <algorithm>
#include <chrono>
#include <memory>
#include <mutex>
#include <shared_mutex>
#include <vector>

#include "final/fevent.h"
Expand All @@ -53,7 +53,7 @@ namespace internal

struct timer_var
{
static std::mutex mutex;
static std::shared_timed_mutex mutex;
};

} // namespace internal
Expand Down Expand Up @@ -158,7 +158,7 @@ auto FTimer<ObjectT>::addTimer (ObjectT* object, int interval) & -> int
// Create a timer and returns the timer identifier number
// (interval in ms)

std::lock_guard<std::mutex> lock_guard(internal::timer_var::mutex);
std::lock_guard<std::shared_timed_mutex> lock_guard(internal::timer_var::mutex);
auto& timer_list = globalTimerList();
int id = getNextId();
const auto time_interval = milliseconds(interval);
Expand Down Expand Up @@ -187,7 +187,7 @@ auto FTimer<ObjectT>::delTimer (int id) const & -> bool
if ( id <= 0 )
return false;

std::lock_guard<std::mutex> lock_guard(internal::timer_var::mutex);
std::lock_guard<std::shared_timed_mutex> lock_guard(internal::timer_var::mutex);
auto& timer_list = globalTimerList();

if ( ! timer_list || timer_list->empty() )
Expand All @@ -214,7 +214,7 @@ auto FTimer<ObjectT>::delOwnTimers(const ObjectT* object) const & -> bool
{
// Deletes all timers of this object

std::lock_guard<std::mutex> lock_guard(internal::timer_var::mutex);
std::lock_guard<std::shared_timed_mutex> lock_guard(internal::timer_var::mutex);
auto& timer_list = globalTimerList();

if ( ! timer_list || timer_list->empty() )
Expand All @@ -237,7 +237,7 @@ auto FTimer<ObjectT>::delAllTimers() const & -> bool
{
// Deletes all timers of all objects

std::lock_guard<std::mutex> lock_guard(internal::timer_var::mutex);
std::lock_guard<std::shared_timed_mutex> lock_guard(internal::timer_var::mutex);
auto& timer_list = globalTimerList();

if ( ! timer_list || timer_list->empty() )
Expand All @@ -255,8 +255,8 @@ template <typename CallbackT>
auto FTimer<ObjectT>::processTimerEvent (CallbackT callback) -> uInt
{
uInt activated{0};
std::unique_lock<std::mutex> lock ( internal::timer_var::mutex
, std::defer_lock );
std::shared_lock<std::shared_timed_mutex> lock ( internal::timer_var::mutex
, std::defer_lock );

if ( ! lock.try_lock() )
return 0;
Expand Down
15 changes: 8 additions & 7 deletions final/output/tty/fterm.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ class FTerm final
static auto scrollTermForward() -> bool;
static auto scrollTermReverse() -> bool;

template <typename... Args>
static void paddingPrintf (const std::string&, Args&&...);
template <typename StringT, typename... Args>
static void paddingPrintf (StringT&&, Args&&...);
static void paddingPrint (const std::string&, int = 1);
static void stringPrint (const std::string&);

Expand Down Expand Up @@ -315,19 +315,20 @@ inline void FTerm::unsetUTF8()
{ setUTF8(false); }

//----------------------------------------------------------------------
template <typename... Args>
inline void FTerm::paddingPrintf (const std::string& format, Args&&... args)
template <typename StringT, typename... Args>
inline void FTerm::paddingPrintf (StringT&& format, Args&&... args)
{
const int size = std::snprintf (nullptr, 0, format.data(), args...);
const int size = std::snprintf (nullptr, 0, format, args...);

if ( size <= 0 )
return;

std::string buffer{};
auto buffer_size = std::size_t(size + 1);
buffer.resize(buffer_size);
std::snprintf ( const_cast<char*>(buffer.data()), buffer_size
, format.data(), std::forward<Args>(args)... );
std::snprintf ( &*buffer.begin(), buffer_size
, std::forward<StringT>(format)
, std::forward<Args>(args)... );
buffer_size--;
buffer.resize(buffer_size);
paddingPrint (buffer, 1);
Expand Down

0 comments on commit fbb7309

Please sign in to comment.