Skip to content

Commit

Permalink
Push local pool memory to a global list at thread termination
Browse files Browse the repository at this point in the history
This change adds a global list of free blocks to the pool allocator,
similar to the existing global list of free lists. When a runtime
thread terminates, it will push its local free blocks to the global
list, which can then be retreived by other threads.

The goal of this change is to avoid memory leaks when starting and
stopping the runtime multiple times in a program, for example in the
compiler test suite.

This pool allocator cleanup functionality is exposed to user threads
through the new pony_unregister_thread function.
  • Loading branch information
Benoit Vey committed Mar 3, 2017
1 parent b7299f6 commit 9862796
Show file tree
Hide file tree
Showing 10 changed files with 418 additions and 100 deletions.
35 changes: 19 additions & 16 deletions src/common/pony/detail/atomics.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,39 +78,41 @@ using std::atomic_flag;
namespace ponyint_atomics
{
template <typename T>
struct aba_protected_t
struct aba_protected_ptr_t
{
static_assert(sizeof(T) <= sizeof(void*), "");
T object;
uintptr_t counter;
// Nested struct for uniform initialisation with GCC/Clang.
struct
{
T* object;
uintptr_t counter;
};
};
}
# define PONY_ABA_PROTECTED_DECLARE(T)
# define PONY_ABA_PROTECTED(T) ponyint_atomics::aba_protected_t<T>
# define PONY_ABA_PROTECTED_PTR_DECLARE(T)
# define PONY_ABA_PROTECTED_PTR(T) ponyint_atomics::aba_protected_ptr_t<T>
#else
# if defined(__LP64__) || defined(_WIN64)
# define PONY_DOUBLEWORD __int128_t
# else
# define PONY_DOUBLEWORD int64_t
# endif
# define PONY_ABA_PROTECTED_DECLARE(T) \
# define PONY_ABA_PROTECTED_PTR_DECLARE(T) \
typedef union \
{ \
struct \
{ \
_Static_assert(sizeof(T) <= sizeof(void*), ""); \
T object; \
T* object; \
uintptr_t counter; \
}; \
PONY_DOUBLEWORD raw; \
} aba_protected_T;
# define PONY_ABA_PROTECTED(T) aba_protected_T
} aba_protected_##T;
# define PONY_ABA_PROTECTED_PTR(T) aba_protected_##T
#endif

// Big atomic objects (larger than machine word size) aren't consistently
// implemented on the compilers we support. We add our own implementation to
// make sure the objects are correctly defined and aligned.
#define PONY_ATOMIC_ABA_PROTECTED(T) alignas(16) PONY_ABA_PROTECTED(T)
#define PONY_ATOMIC_ABA_PROTECTED_PTR(T) alignas(16) PONY_ABA_PROTECTED_PTR(T)

#ifdef PONY_WANT_ATOMIC_DEFS
# ifdef _MSC_VER
Expand All @@ -119,17 +121,18 @@ namespace ponyint_atomics
namespace ponyint_atomics
{
template <typename T>
inline PONY_ABA_PROTECTED(T) big_load(PONY_ABA_PROTECTED(T)* ptr)
inline PONY_ABA_PROTECTED_PTR(T) big_load(PONY_ABA_PROTECTED_PTR(T)* ptr)
{
PONY_ABA_PROTECTED(T) ret = {NULL, 0};
PONY_ABA_PROTECTED_PTR(T) ret = {NULL, 0};
_InterlockedCompareExchange128((LONGLONG*)ptr, 0, 0, (LONGLONG*)&ret);
return ret;
}

template <typename T>
inline void big_store(PONY_ABA_PROTECTED(T)* ptr, PONY_ABA_PROTECTED(T) val)
inline void big_store(PONY_ABA_PROTECTED_PTR(T)* ptr,
PONY_ABA_PROTECTED_PTR(T) val)
{
PONY_ABA_PROTECTED(T) tmp;
PONY_ABA_PROTECTED_PTR(T) tmp;
tmp.object = ptr->object;
tmp.counter = ptr->counter;
while(!_InterlockedCompareExchange128((LONGLONG*)ptr,
Expand Down
1 change: 1 addition & 0 deletions src/libponyrt/asio/epoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ DECLARE_THREAD_FN(ponyint_asio_backend_dispatch)
close(b->wakeup);
ponyint_messageq_destroy(&b->q);
POOL_FREE(asio_backend_t, b);
pony_unregister_thread();
return NULL;
}

Expand Down
1 change: 1 addition & 0 deletions src/libponyrt/asio/iocp.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ DECLARE_THREAD_FN(ponyint_asio_backend_dispatch)
CloseHandle(b->wakeup);
ponyint_messageq_destroy(&b->q);
POOL_FREE(asio_backend_t, b);
pony_unregister_thread();
return NULL;
}

Expand Down
1 change: 1 addition & 0 deletions src/libponyrt/asio/kqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ DECLARE_THREAD_FN(ponyint_asio_backend_dispatch)

ponyint_messageq_destroy(&b->q);
POOL_FREE(asio_backend_t, b);
pony_unregister_thread();
return NULL;
}

Expand Down
Loading

0 comments on commit 9862796

Please sign in to comment.