Skip to content

Commit

Permalink
Merge pull request #1233 from trapexit/atomic
Browse files Browse the repository at this point in the history
Use relaxed memory order for atomic counters
  • Loading branch information
trapexit authored Aug 21, 2023
2 parents b6d3c34 + 0ed03a1 commit 51b65cd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 135 deletions.
7 changes: 4 additions & 3 deletions libfuse/lib/bounded_thread_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class BoundedThreadPool
void
enqueue_work(F&& f_)
{
auto i = _index++;
auto i = _index.fetch_add(1,std::memory_order_relaxed);

for(std::size_t n = 0; n < (_count * K); ++n)
{
Expand All @@ -104,10 +104,11 @@ class BoundedThreadPool
using TaskReturnType = typename std::result_of<F()>::type;
using Promise = std::promise<TaskReturnType>;

auto i = _index++;
auto i = _index.fetch_add(1,std::memory_order_relaxed);
auto promise = std::make_shared<Promise>();
auto future = promise->get_future();
auto work = [=]() {
auto work = [=]()
{
auto rv = f_();
promise->set_value(rv);
};
Expand Down
8 changes: 4 additions & 4 deletions libfuse/lib/fuse_msgbuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ _msgbuf_alloc(msgbuf_setup_func_t setup_func_)
if(msgbuf == NULL)
return NULL;

g_MSGBUF_ALLOC_COUNT++;
g_MSGBUF_ALLOC_COUNT.fetch_add(1,std::memory_order_relaxed);
}
else
{
Expand Down Expand Up @@ -160,7 +160,7 @@ msgbuf_free(fuse_msgbuf_t *msgbuf_)
if(msgbuf_->size != (g_BUFSIZE - g_PAGESIZE))
{
msgbuf_destroy(msgbuf_);
g_MSGBUF_ALLOC_COUNT--;
g_MSGBUF_ALLOC_COUNT.fetch_sub(1,std::memory_order_relaxed);
return;
}

Expand Down Expand Up @@ -206,7 +206,7 @@ msgbuf_gc_10percent()
for(auto msgbuf : togc)
{
msgbuf_destroy(msgbuf);
g_MSGBUF_ALLOC_COUNT--;
g_MSGBUF_ALLOC_COUNT.fetch_sub(1,std::memory_order_relaxed);
}
}

Expand All @@ -223,6 +223,6 @@ msgbuf_gc()
for(auto msgbuf: oldstack)
{
msgbuf_destroy(msgbuf);
g_MSGBUF_ALLOC_COUNT--;
g_MSGBUF_ALLOC_COUNT.fetch_sub(1,std::memory_order_relaxed);
}
}
125 changes: 0 additions & 125 deletions libfuse/lib/thread_pool.hpp

This file was deleted.

7 changes: 4 additions & 3 deletions src/unbounded_thread_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ThreadPool
void
enqueue_work(F&& f_)
{
auto i = _index++;
auto i = _index.fetch_add(1,std::memory_order_relaxed);

for(std::size_t n = 0; n < (_count * K); ++n)
{
Expand All @@ -91,10 +91,11 @@ class ThreadPool
using TaskReturnType = typename std::result_of<F()>::type;
using Promise = std::promise<TaskReturnType>;

auto i = _index++;
auto i = _index.fetch_add(1,std::memory_order_relaxed);
auto promise = std::make_shared<Promise>();
auto future = promise->get_future();
auto work = [=]() {
auto work = [=]()
{
auto rv = f_();
promise->set_value(rv);
};
Expand Down

0 comments on commit 51b65cd

Please sign in to comment.