Skip to content

Made the mem_cache_block lockfree #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions include/boost/regex/v4/mem_block_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,54 @@
# include BOOST_ABI_PREFIX
#endif

#ifndef BOOST_NO_CXX11_HDR_ATOMIC
#include <atomic>
#if ATOMIC_POINTER_LOCK_FREE == 2
#define BOOST_REGEX_MEM_BLOCK_CACHE_LOCK_FREE
#define BOOST_REGEX_ATOMIC_POINTER std::atomic
#endif
#endif

namespace boost{
namespace BOOST_REGEX_DETAIL_NS{

#ifdef BOOST_REGEX_MEM_BLOCK_CACHE_LOCK_FREE /* lock free implementation */
struct mem_block_cache
{
std::atomic<void*> cache[BOOST_REGEX_MAX_CACHE_BLOCKS];

~mem_block_cache()
{
for (size_t i = 0;i < BOOST_REGEX_MAX_CACHE_BLOCKS; ++i) {
if (cache[i].load()) ::operator delete(cache[i].load());
}
}
void* get()
{
for (size_t i = 0;i < BOOST_REGEX_MAX_CACHE_BLOCKS; ++i) {
void* p = cache[i].load();
if (p != NULL) {
if (cache[i].compare_exchange_strong(p, NULL)) return p;
}
}
return ::operator new(BOOST_REGEX_BLOCKSIZE);
}
void put(void* ptr)
{
for (size_t i = 0;i < BOOST_REGEX_MAX_CACHE_BLOCKS; ++i) {
void* p = cache[i].load();
if (p == NULL) {
if (cache[i].compare_exchange_strong(p, ptr)) return;
}
}
::operator delete(ptr);
}
};


#else /* lock-based implementation */


struct mem_block_node
{
mem_block_node* next;
Expand Down Expand Up @@ -85,6 +130,7 @@ struct mem_block_cache
}
}
};
#endif

extern mem_block_cache block_cache;

Expand Down
4 changes: 3 additions & 1 deletion src/regex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ BOOST_REGEX_DECL void BOOST_REGEX_CALL put_mem_block(void* p)

#else

#ifdef BOOST_HAS_THREADS
#if defined(BOOST_REGEX_MEM_BLOCK_CACHE_LOCK_FREE)
mem_block_cache block_cache = { { {nullptr} } } ;
#elif defined(BOOST_HAS_THREADS)
mem_block_cache block_cache = { 0, 0, BOOST_STATIC_MUTEX_INIT, };
#else
mem_block_cache block_cache = { 0, 0, };
Expand Down