diff options
author | Max Romanov <max.romanov@nginx.com> | 2017-07-07 16:01:34 +0300 |
---|---|---|
committer | Max Romanov <max.romanov@nginx.com> | 2017-07-07 16:01:34 +0300 |
commit | e58f4ef0d030f270c3ef2da7dcb891c961718372 (patch) | |
tree | 3d902bbe55b8edce47e8721edb2cf883bf600e95 /src/nxt_mp.c | |
parent | e15b975e9d285f84535c7697f1d87568bfb08269 (diff) | |
download | unit-e58f4ef0d030f270c3ef2da7dcb891c961718372.tar.gz unit-e58f4ef0d030f270c3ef2da7dcb891c961718372.tar.bz2 |
Memory pool thread safety checks in DEBUG build and usage fixes.
Diffstat (limited to 'src/nxt_mp.c')
-rw-r--r-- | src/nxt_mp.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/nxt_mp.c b/src/nxt_mp.c index f1ddd86b..464df1c1 100644 --- a/src/nxt_mp.c +++ b/src/nxt_mp.c @@ -109,6 +109,11 @@ struct nxt_mp_s { uint32_t cluster_size; uint32_t retain; +#if (NXT_DEBUG) + nxt_pid_t pid; + nxt_tid_t tid; +#endif + /* Lists of nxt_mp_page_t. */ nxt_queue_t free_pages; nxt_queue_t nget_pages; @@ -186,6 +191,49 @@ nxt_lg2(uint64_t v) #endif +#if (NXT_DEBUG) + +nxt_inline void +nxt_mp_thread_assert(nxt_mp_t *mp) +{ + nxt_tid_t tid; + nxt_thread_t *thread; + + thread = nxt_thread(); + tid = nxt_thread_tid(thread); + + if (nxt_fast_path(mp->tid == tid)) { + return; + } + + if (nxt_slow_path(nxt_pid != mp->pid)) { + mp->pid = nxt_pid; + mp->tid = tid; + + return; + } + + nxt_log_alert(thread->log, "mem_pool locked by thread %PT", mp->tid); + nxt_abort(); +} + +#else + +#define nxt_mp_thread_assert(mp) + +#endif + + +void +nxt_mp_thread_adopt(nxt_mp_t *mp) +{ +#if (NXT_DEBUG) + mp->pid = nxt_pid; + mp->tid = nxt_thread_tid(NULL); +#endif +} + + nxt_mp_t * nxt_mp_create(size_t cluster_size, size_t page_alignment, size_t page_size, size_t min_chunk_size) @@ -417,6 +465,8 @@ nxt_mp_alloc_small(nxt_mp_t *mp, size_t size) nxt_mp_page_t *page; nxt_queue_link_t *link; + nxt_mp_thread_assert(mp); + p = NULL; if (size <= mp->page_size / 2) { @@ -489,6 +539,8 @@ nxt_mp_get_small(nxt_mp_t *mp, nxt_queue_t *pages, size_t size) nxt_mp_page_t *page; nxt_queue_link_t *link, *next; + nxt_mp_thread_assert(mp); + for (link = nxt_queue_first(pages); link != nxt_queue_tail(pages); link = next) @@ -604,6 +656,8 @@ nxt_mp_alloc_large(nxt_mp_t *mp, size_t alignment, size_t size) uint8_t type; nxt_mp_block_t *block; + nxt_mp_thread_assert(mp); + /* Allocation must be less than 4G. */ if (nxt_slow_path(size >= 0xFFFFFFFF)) { return NULL; @@ -664,6 +718,8 @@ nxt_mp_free(nxt_mp_t *mp, void *p) nxt_thread_t *thread; nxt_mp_block_t *block; + nxt_mp_thread_assert(mp); + nxt_debug_alloc("mp free %p", p); block = nxt_mp_find_block(&mp->blocks, p); |