object_pool_inl.h 20.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

gejun's avatar
gejun committed
18 19 20 21 22
// bthread - A M:N threading library to make applications more concurrent.

// Author: Ge,Jun (gejun@baidu.com)
// Date: Sun Jul 13 15:04:18 CST 2014

23 24
#ifndef BUTIL_OBJECT_POOL_INL_H
#define BUTIL_OBJECT_POOL_INL_H
gejun's avatar
gejun committed
25 26 27 28

#include <iostream>                      // std::ostream
#include <pthread.h>                     // pthread_mutex_t
#include <algorithm>                     // std::max, std::min
29 30 31 32
#include "butil/atomicops.h"              // butil::atomic
#include "butil/macros.h"                 // BAIDU_CACHELINE_ALIGNMENT
#include "butil/scoped_lock.h"            // BAIDU_SCOPED_LOCK
#include "butil/thread_local.h"           // BAIDU_THREAD_LOCAL
gejun's avatar
gejun committed
33 34
#include <vector>

35
#ifdef BUTIL_OBJECT_POOL_NEED_FREE_ITEM_NUM
gejun's avatar
gejun committed
36
#define BAIDU_OBJECT_POOL_FREE_ITEM_NUM_ADD1                    \
37
    (_global_nfree.fetch_add(1, butil::memory_order_relaxed))
gejun's avatar
gejun committed
38
#define BAIDU_OBJECT_POOL_FREE_ITEM_NUM_SUB1                    \
39
    (_global_nfree.fetch_sub(1, butil::memory_order_relaxed))
gejun's avatar
gejun committed
40 41 42 43 44
#else
#define BAIDU_OBJECT_POOL_FREE_ITEM_NUM_ADD1
#define BAIDU_OBJECT_POOL_FREE_ITEM_NUM_SUB1
#endif

45
namespace butil {
gejun's avatar
gejun committed
46 47 48 49 50 51

template <typename T, size_t NITEM>
struct ObjectPoolFreeChunk {
    size_t nfree;
    T* ptrs[NITEM];
}; 
gejun's avatar
gejun committed
52 53 54 55 56 57
// for gcc 3.4.5
template <typename T>
struct ObjectPoolFreeChunk<T, 0> {
    size_t nfree;
    T* ptrs[0];
}; 
gejun's avatar
gejun committed
58 59 60 61 62 63 64 65 66

struct ObjectPoolInfo {
    size_t local_pool_num;
    size_t block_group_num;
    size_t block_num;
    size_t item_num;
    size_t block_item_num;
    size_t free_chunk_item_num;
    size_t total_size;
67
#ifdef BUTIL_OBJECT_POOL_NEED_FREE_ITEM_NUM
gejun's avatar
gejun committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    size_t free_item_num;
#endif
};

static const size_t OP_MAX_BLOCK_NGROUP = 65536;
static const size_t OP_GROUP_NBLOCK_NBIT = 16;
static const size_t OP_GROUP_NBLOCK = (1UL << OP_GROUP_NBLOCK_NBIT);
static const size_t OP_INITIAL_FREE_LIST_SIZE = 1024;

template <typename T>
class ObjectPoolBlockItemNum {
    static const size_t N1 = ObjectPoolBlockMaxSize<T>::value / sizeof(T);
    static const size_t N2 = (N1 < 1 ? 1 : N1);
public:
    static const size_t value = (N2 > ObjectPoolBlockMaxItem<T>::value ?
                                 ObjectPoolBlockMaxItem<T>::value : N2);
};

template <typename T>
class BAIDU_CACHELINE_ALIGNMENT ObjectPool {
public:
    static const size_t BLOCK_NITEM = ObjectPoolBlockItemNum<T>::value;
gejun's avatar
gejun committed
90
    static const size_t FREE_CHUNK_NITEM = BLOCK_NITEM;
gejun's avatar
gejun committed
91 92 93 94

    // Free objects are batched in a FreeChunk before they're added to
    // global list(_free_chunks).
    typedef ObjectPoolFreeChunk<T, FREE_CHUNK_NITEM>    FreeChunk;
gejun's avatar
gejun committed
95
    typedef ObjectPoolFreeChunk<T, 0> DynamicFreeChunk;
gejun's avatar
gejun committed
96 97 98

    // When a thread needs memory, it allocates a Block. To improve locality,
    // items in the Block are only used by the thread.
99
    // To support cache-aligned objects, align Block.items by cacheline.
gejun's avatar
gejun committed
100 101
    struct BAIDU_CACHELINE_ALIGNMENT Block {
        char items[sizeof(T) * BLOCK_NITEM];
102
        size_t nitem;
gejun's avatar
gejun committed
103 104 105 106 107 108 109 110

        Block() : nitem(0) {}
    };

    // An Object addresses at most OP_MAX_BLOCK_NGROUP BlockGroups,
    // each BlockGroup addresses at most OP_GROUP_NBLOCK blocks. So an
    // object addresses at most OP_MAX_BLOCK_NGROUP * OP_GROUP_NBLOCK Blocks.
    struct BlockGroup {
111 112
        butil::atomic<size_t> nblock;
        butil::atomic<Block*> blocks[OP_GROUP_NBLOCK];
gejun's avatar
gejun committed
113 114 115 116 117

        BlockGroup() : nblock(0) {
            // We fetch_add nblock in add_block() before setting the entry,
            // thus address_resource() may sees the unset entry. Initialize
            // all entries to NULL makes such address_resource() return NULL.
118
            memset(blocks, 0, sizeof(butil::atomic<Block*>) * OP_GROUP_NBLOCK);
gejun's avatar
gejun committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
        }
    };

    // Each thread has an instance of this class.
    class BAIDU_CACHELINE_ALIGNMENT LocalPool {
    public:
        explicit LocalPool(ObjectPool* pool)
            : _pool(pool)
            , _cur_block(NULL)
            , _cur_block_index(0) {
            _cur_free.nfree = 0;
        }

        ~LocalPool() {
            // Add to global _free if there're some free objects
            if (_cur_free.nfree) {
                _pool->push_free_chunk(_cur_free);
            }

            _pool->clear_from_destructor_of_local_pool();
        }

        static void delete_local_pool(void* arg) {
            delete (LocalPool*)arg;
        }

        // We need following macro to construct T with different CTOR_ARGS
        // which may include parenthesis because when T is POD, "new T()"
        // and "new T" are different: former one sets all fields to 0 which
        // we don't want.
#define BAIDU_OBJECT_POOL_GET(CTOR_ARGS)                                \
        /* Fetch local free ptr */                                      \
        if (_cur_free.nfree) {                                          \
            BAIDU_OBJECT_POOL_FREE_ITEM_NUM_SUB1;                       \
            return _cur_free.ptrs[--_cur_free.nfree];                   \
        }                                                               \
        /* Fetch a FreeChunk from global.                               \
           TODO: Popping from _free needs to copy a FreeChunk which is  \
           costly, but hardly impacts amortized performance. */         \
        if (_pool->pop_free_chunk(_cur_free)) {                         \
            BAIDU_OBJECT_POOL_FREE_ITEM_NUM_SUB1;                       \
            return _cur_free.ptrs[--_cur_free.nfree];                   \
        }                                                               \
        /* Fetch memory from local block */                             \
        if (_cur_block && _cur_block->nitem < BLOCK_NITEM) {            \
            T* obj = new ((T*)_cur_block->items + _cur_block->nitem) T CTOR_ARGS; \
            if (!ObjectPoolValidator<T>::validate(obj)) {               \
                obj->~T();                                              \
                return NULL;                                            \
            }                                                           \
            ++_cur_block->nitem;                                        \
            return obj;                                                 \
        }                                                               \
        /* Fetch a Block from global */                                 \
        _cur_block = add_block(&_cur_block_index);                      \
        if (_cur_block != NULL) {                                       \
            T* obj = new ((T*)_cur_block->items + _cur_block->nitem) T CTOR_ARGS; \
            if (!ObjectPoolValidator<T>::validate(obj)) {               \
                obj->~T();                                              \
                return NULL;                                            \
            }                                                           \
            ++_cur_block->nitem;                                        \
            return obj;                                                 \
        }                                                               \
        return NULL;                                                    \
 

        inline T* get() {
            BAIDU_OBJECT_POOL_GET();
        }

        template <typename A1>
        inline T* get(const A1& a1) {
            BAIDU_OBJECT_POOL_GET((a1));
        }

        template <typename A1, typename A2>
        inline T* get(const A1& a1, const A2& a2) {
            BAIDU_OBJECT_POOL_GET((a1, a2));
        }

#undef BAIDU_OBJECT_POOL_GET

        inline int return_object(T* ptr) {
            // Return to local free list
            if (_cur_free.nfree < ObjectPool::free_chunk_nitem()) {
                _cur_free.ptrs[_cur_free.nfree++] = ptr;
                BAIDU_OBJECT_POOL_FREE_ITEM_NUM_ADD1;
                return 0;
            }
            // Local free list is full, return it to global.
            // For copying issue, check comment in upper get()
            if (_pool->push_free_chunk(_cur_free)) {
                _cur_free.nfree = 1;
                _cur_free.ptrs[0] = ptr;
                BAIDU_OBJECT_POOL_FREE_ITEM_NUM_ADD1;
                return 0;
            }
            return -1;
        }

    private:
        ObjectPool* _pool;
        Block* _cur_block;
        size_t _cur_block_index;
        FreeChunk _cur_free;
    };

    inline T* get_object() {
        LocalPool* lp = get_or_new_local_pool();
eric's avatar
eric committed
229
        if (BAIDU_LIKELY(lp != NULL)) {
gejun's avatar
gejun committed
230 231 232 233 234 235 236 237
            return lp->get();
        }
        return NULL;
    }

    template <typename A1>
    inline T* get_object(const A1& arg1) {
        LocalPool* lp = get_or_new_local_pool();
eric's avatar
eric committed
238
        if (BAIDU_LIKELY(lp != NULL)) {
gejun's avatar
gejun committed
239 240 241 242 243 244 245 246
            return lp->get(arg1);
        }
        return NULL;
    }

    template <typename A1, typename A2>
    inline T* get_object(const A1& arg1, const A2& arg2) {
        LocalPool* lp = get_or_new_local_pool();
eric's avatar
eric committed
247
        if (BAIDU_LIKELY(lp != NULL)) {
gejun's avatar
gejun committed
248 249 250 251 252 253 254
            return lp->get(arg1, arg2);
        }
        return NULL;
    }

    inline int return_object(T* ptr) {
        LocalPool* lp = get_or_new_local_pool();
eric's avatar
eric committed
255
        if (BAIDU_LIKELY(lp != NULL)) {
gejun's avatar
gejun committed
256 257 258 259 260 261 262 263 264
            return lp->return_object(ptr);
        }
        return -1;
    }

    void clear_objects() {
        LocalPool* lp = _local_pool;
        if (lp) {
            _local_pool = NULL;
265
            butil::thread_atexit_cancel(LocalPool::delete_local_pool, lp);
gejun's avatar
gejun committed
266 267 268 269 270
            delete lp;
        }
    }

    inline static size_t free_chunk_nitem() {
gejun's avatar
gejun committed
271 272
        const size_t n = ObjectPoolFreeChunkMaxItem<T>::value();
        return (n < FREE_CHUNK_NITEM ? n : FREE_CHUNK_NITEM);
gejun's avatar
gejun committed
273 274 275 276 277
    }

    // Number of all allocated objects, including being used and free.
    ObjectPoolInfo describe_objects() const {
        ObjectPoolInfo info;
278 279
        info.local_pool_num = _nlocal.load(butil::memory_order_relaxed);
        info.block_group_num = _ngroup.load(butil::memory_order_acquire);
gejun's avatar
gejun committed
280 281 282 283
        info.block_num = 0;
        info.item_num = 0;
        info.free_chunk_item_num = free_chunk_nitem();
        info.block_item_num = BLOCK_NITEM;
284
#ifdef BUTIL_OBJECT_POOL_NEED_FREE_ITEM_NUM
285
        info.free_item_num = _global_nfree.load(butil::memory_order_relaxed);
gejun's avatar
gejun committed
286 287 288
#endif

        for (size_t i = 0; i < info.block_group_num; ++i) {
289
            BlockGroup* bg = _block_groups[i].load(butil::memory_order_consume);
gejun's avatar
gejun committed
290 291 292
            if (NULL == bg) {
                break;
            }
293
            size_t nblock = std::min(bg->nblock.load(butil::memory_order_relaxed),
gejun's avatar
gejun committed
294 295 296
                                     OP_GROUP_NBLOCK);
            info.block_num += nblock;
            for (size_t j = 0; j < nblock; ++j) {
297
                Block* b = bg->blocks[j].load(butil::memory_order_consume);
gejun's avatar
gejun committed
298 299 300 301 302 303 304 305 306 307
                if (NULL != b) {
                    info.item_num += b->nitem;
                }
            }
        }
        info.total_size = info.block_num * info.block_item_num * sizeof(T);
        return info;
    }

    static inline ObjectPool* singleton() {
308
        ObjectPool* p = _singleton.load(butil::memory_order_consume);
gejun's avatar
gejun committed
309 310 311 312
        if (p) {
            return p;
        }
        pthread_mutex_lock(&_singleton_mutex);
313
        p = _singleton.load(butil::memory_order_consume);
gejun's avatar
gejun committed
314 315
        if (!p) {
            p = new ObjectPool();
316
            _singleton.store(p, butil::memory_order_release);
gejun's avatar
gejun committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
        }
        pthread_mutex_unlock(&_singleton_mutex);
        return p;
    }

private:
    ObjectPool() {
        _free_chunks.reserve(OP_INITIAL_FREE_LIST_SIZE);
        pthread_mutex_init(&_free_chunks_mutex, NULL);
    }

    ~ObjectPool() {
        pthread_mutex_destroy(&_free_chunks_mutex);
    }

    // Create a Block and append it to right-most BlockGroup.
    static Block* add_block(size_t* index) {
        Block* const new_block = new(std::nothrow) Block;
        if (NULL == new_block) {
            return NULL;
        }
        size_t ngroup;
        do {
340
            ngroup = _ngroup.load(butil::memory_order_acquire);
gejun's avatar
gejun committed
341 342
            if (ngroup >= 1) {
                BlockGroup* const g =
343
                    _block_groups[ngroup - 1].load(butil::memory_order_consume);
gejun's avatar
gejun committed
344
                const size_t block_index =
345
                    g->nblock.fetch_add(1, butil::memory_order_relaxed);
gejun's avatar
gejun committed
346 347
                if (block_index < OP_GROUP_NBLOCK) {
                    g->blocks[block_index].store(
348
                        new_block, butil::memory_order_release);
gejun's avatar
gejun committed
349 350 351
                    *index = (ngroup - 1) * OP_GROUP_NBLOCK + block_index;
                    return new_block;
                }
352
                g->nblock.fetch_sub(1, butil::memory_order_relaxed);
gejun's avatar
gejun committed
353 354 355 356 357 358 359 360 361 362 363 364 365
            }
        } while (add_block_group(ngroup));

        // Fail to add_block_group.
        delete new_block;
        return NULL;
    }

    // Create a BlockGroup and append it to _block_groups.
    // Shall be called infrequently because a BlockGroup is pretty big.
    static bool add_block_group(size_t old_ngroup) {
        BlockGroup* bg = NULL;
        BAIDU_SCOPED_LOCK(_block_group_mutex);
366
        const size_t ngroup = _ngroup.load(butil::memory_order_acquire);
gejun's avatar
gejun committed
367 368 369 370 371 372 373 374 375
        if (ngroup != old_ngroup) {
            // Other thread got lock and added group before this thread.
            return true;
        }
        if (ngroup < OP_MAX_BLOCK_NGROUP) {
            bg = new(std::nothrow) BlockGroup;
            if (NULL != bg) {
                // Release fence is paired with consume fence in add_block()
                // to avoid un-constructed bg to be seen by other threads.
376 377
                _block_groups[ngroup].store(bg, butil::memory_order_release);
                _ngroup.store(ngroup + 1, butil::memory_order_release);
gejun's avatar
gejun committed
378 379 380 381 382 383 384
            }
        }
        return bg != NULL;
    }

    inline LocalPool* get_or_new_local_pool() {
        LocalPool* lp = _local_pool;
eric's avatar
eric committed
385
        if (BAIDU_LIKELY(lp != NULL)) {
gejun's avatar
gejun committed
386 387 388 389 390 391 392 393
            return lp;
        }
        lp = new(std::nothrow) LocalPool(this);
        if (NULL == lp) {
            return NULL;
        }
        BAIDU_SCOPED_LOCK(_change_thread_mutex); //avoid race with clear()
        _local_pool = lp;
394 395
        butil::thread_atexit(LocalPool::delete_local_pool, lp);
        _nlocal.fetch_add(1, butil::memory_order_relaxed);
gejun's avatar
gejun committed
396 397 398 399 400 401 402 403
        return lp;
    }

    void clear_from_destructor_of_local_pool() {
        // Remove tls
        _local_pool = NULL;

        // Do nothing if there're active threads.
404
        if (_nlocal.fetch_sub(1, butil::memory_order_relaxed) != 1) {
gejun's avatar
gejun committed
405 406 407 408 409 410 411 412 413 414 415
            return;
        }

        // Can't delete global even if all threads(called ObjectPool
        // functions) quit because the memory may still be referenced by 
        // other threads. But we need to validate that all memory can
        // be deallocated correctly in tests, so wrap the function with 
        // a macro which is only defined in unittests.
#ifdef BAIDU_CLEAR_OBJECT_POOL_AFTER_ALL_THREADS_QUIT
        BAIDU_SCOPED_LOCK(_change_thread_mutex);  // including acquire fence.
        // Do nothing if there're active threads.
416
        if (_nlocal.load(butil::memory_order_relaxed) != 0) {
gejun's avatar
gejun committed
417 418 419 420 421 422 423 424 425 426
            return;
        }
        // All threads exited and we're holding _change_thread_mutex to avoid
        // racing with new threads calling get_object().

        // Clear global free list.
        FreeChunk dummy;
        while (pop_free_chunk(dummy));

        // Delete all memory
427
        const size_t ngroup = _ngroup.exchange(0, butil::memory_order_relaxed);
gejun's avatar
gejun committed
428
        for (size_t i = 0; i < ngroup; ++i) {
429
            BlockGroup* bg = _block_groups[i].load(butil::memory_order_relaxed);
gejun's avatar
gejun committed
430 431 432
            if (NULL == bg) {
                break;
            }
433
            size_t nblock = std::min(bg->nblock.load(butil::memory_order_relaxed),
gejun's avatar
gejun committed
434 435
                                     OP_GROUP_NBLOCK);
            for (size_t j = 0; j < nblock; ++j) {
436
                Block* b = bg->blocks[j].load(butil::memory_order_relaxed);
gejun's avatar
gejun committed
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
                if (NULL == b) {
                    continue;
                }
                for (size_t k = 0; k < b->nitem; ++k) {
                    T* const objs = (T*)b->items;
                    objs[k].~T();
                }
                delete b;
            }
            delete bg;
        }

        memset(_block_groups, 0, sizeof(BlockGroup*) * OP_MAX_BLOCK_NGROUP);
#endif
    }

private:
    bool pop_free_chunk(FreeChunk& c) {
gejun's avatar
gejun committed
455 456
        // Critical for the case that most return_object are called in
        // different threads of get_object.
gejun's avatar
gejun committed
457 458 459 460 461 462 463 464
        if (_free_chunks.empty()) {
            return false;
        }
        pthread_mutex_lock(&_free_chunks_mutex);
        if (_free_chunks.empty()) {
            pthread_mutex_unlock(&_free_chunks_mutex);
            return false;
        }
gejun's avatar
gejun committed
465
        DynamicFreeChunk* p = _free_chunks.back();
gejun's avatar
gejun committed
466 467
        _free_chunks.pop_back();
        pthread_mutex_unlock(&_free_chunks_mutex);
gejun's avatar
gejun committed
468 469 470
        c.nfree = p->nfree;
        memcpy(c.ptrs, p->ptrs, sizeof(*p->ptrs) * p->nfree);
        free(p);
gejun's avatar
gejun committed
471 472 473 474
        return true;
    }

    bool push_free_chunk(const FreeChunk& c) {
gejun's avatar
gejun committed
475 476 477
        DynamicFreeChunk* p = (DynamicFreeChunk*)malloc(
            offsetof(DynamicFreeChunk, ptrs) + sizeof(*c.ptrs) * c.nfree);
        if (!p) {
gejun's avatar
gejun committed
478 479
            return false;
        }
gejun's avatar
gejun committed
480 481
        p->nfree = c.nfree;
        memcpy(p->ptrs, c.ptrs, sizeof(*c.ptrs) * c.nfree);
gejun's avatar
gejun committed
482
        pthread_mutex_lock(&_free_chunks_mutex);
gejun's avatar
gejun committed
483
        _free_chunks.push_back(p);
gejun's avatar
gejun committed
484 485 486 487
        pthread_mutex_unlock(&_free_chunks_mutex);
        return true;
    }
    
488
    static butil::static_atomic<ObjectPool*> _singleton;
gejun's avatar
gejun committed
489 490
    static pthread_mutex_t _singleton_mutex;
    static BAIDU_THREAD_LOCAL LocalPool* _local_pool;
491 492
    static butil::static_atomic<long> _nlocal;
    static butil::static_atomic<size_t> _ngroup;
gejun's avatar
gejun committed
493 494
    static pthread_mutex_t _block_group_mutex;
    static pthread_mutex_t _change_thread_mutex;
495
    static butil::static_atomic<BlockGroup*> _block_groups[OP_MAX_BLOCK_NGROUP];
gejun's avatar
gejun committed
496

gejun's avatar
gejun committed
497
    std::vector<DynamicFreeChunk*> _free_chunks;
gejun's avatar
gejun committed
498 499
    pthread_mutex_t _free_chunks_mutex;

500
#ifdef BUTIL_OBJECT_POOL_NEED_FREE_ITEM_NUM
501
    static butil::static_atomic<size_t> _global_nfree;
gejun's avatar
gejun committed
502 503 504 505 506 507 508 509 510 511 512 513 514
#endif
};

// Declare template static variables:

template <typename T>
const size_t ObjectPool<T>::FREE_CHUNK_NITEM;

template <typename T>
BAIDU_THREAD_LOCAL typename ObjectPool<T>::LocalPool*
ObjectPool<T>::_local_pool = NULL;

template <typename T>
515
butil::static_atomic<ObjectPool<T>*> ObjectPool<T>::_singleton = BUTIL_STATIC_ATOMIC_INIT(NULL);
gejun's avatar
gejun committed
516 517 518 519 520

template <typename T>
pthread_mutex_t ObjectPool<T>::_singleton_mutex = PTHREAD_MUTEX_INITIALIZER;

template <typename T>
521
static_atomic<long> ObjectPool<T>::_nlocal = BUTIL_STATIC_ATOMIC_INIT(0);
gejun's avatar
gejun committed
522 523

template <typename T>
524
butil::static_atomic<size_t> ObjectPool<T>::_ngroup = BUTIL_STATIC_ATOMIC_INIT(0);
gejun's avatar
gejun committed
525 526 527 528 529 530 531 532

template <typename T>
pthread_mutex_t ObjectPool<T>::_block_group_mutex = PTHREAD_MUTEX_INITIALIZER;

template <typename T>
pthread_mutex_t ObjectPool<T>::_change_thread_mutex = PTHREAD_MUTEX_INITIALIZER;

template <typename T>
533
butil::static_atomic<typename ObjectPool<T>::BlockGroup*>
gejun's avatar
gejun committed
534 535
ObjectPool<T>::_block_groups[OP_MAX_BLOCK_NGROUP] = {};

536
#ifdef BUTIL_OBJECT_POOL_NEED_FREE_ITEM_NUM
gejun's avatar
gejun committed
537
template <typename T>
538
butil::static_atomic<size_t> ObjectPool<T>::_global_nfree = BUTIL_STATIC_ATOMIC_INIT(0);
gejun's avatar
gejun committed
539 540 541 542 543 544 545 546 547 548 549
#endif

inline std::ostream& operator<<(std::ostream& os,
                                ObjectPoolInfo const& info) {
    return os << "local_pool_num: " << info.local_pool_num
              << "\nblock_group_num: " << info.block_group_num
              << "\nblock_num: " << info.block_num
              << "\nitem_num: " << info.item_num
              << "\nblock_item_num: " << info.block_item_num
              << "\nfree_chunk_item_num: " << info.free_chunk_item_num
              << "\ntotal_size: " << info.total_size
550
#ifdef BUTIL_OBJECT_POOL_NEED_FREE_ITEM_NUM
gejun's avatar
gejun committed
551 552 553 554
              << "\nfree_num: " << info.free_item_num
#endif
        ;
}
555
}  // namespace butil
gejun's avatar
gejun committed
556

557
#endif  // BUTIL_OBJECT_POOL_INL_H