mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-25 18:11:33 +08:00
Declare variables as size_t rather than unsigned.
Modify `unsigned front, front1, back;` to `size_t front, front1, back;`
This commit is contained in:
parent
f70b2ef3b8
commit
34a9f24dba
@ -1,168 +1,168 @@
|
|||||||
/*
|
/*
|
||||||
A modified version of Bounded MPMC queue by Dmitry Vyukov.
|
A modified version of Bounded MPMC queue by Dmitry Vyukov.
|
||||||
|
|
||||||
Original code from:
|
Original code from:
|
||||||
http://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue
|
http://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue
|
||||||
|
|
||||||
licensed by Dmitry Vyukov under the terms below:
|
licensed by Dmitry Vyukov under the terms below:
|
||||||
|
|
||||||
Simplified BSD license
|
Simplified BSD license
|
||||||
|
|
||||||
Copyright (c) 2010-2011 Dmitry Vyukov. All rights reserved.
|
Copyright (c) 2010-2011 Dmitry Vyukov. All rights reserved.
|
||||||
Redistribution and use in source and binary forms, with or without modification,
|
Redistribution and use in source and binary forms, with or without modification,
|
||||||
are permitted provided that the following conditions are met:
|
are permitted provided that the following conditions are met:
|
||||||
1. Redistributions of source code must retain the above copyright notice, this list of
|
1. Redistributions of source code must retain the above copyright notice, this list of
|
||||||
conditions and the following disclaimer.
|
conditions and the following disclaimer.
|
||||||
|
|
||||||
2. Redistributions in binary form must reproduce the above copyright notice, this list
|
2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||||
of conditions and the following disclaimer in the documentation and/or other materials
|
of conditions and the following disclaimer in the documentation and/or other materials
|
||||||
provided with the distribution.
|
provided with the distribution.
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY DMITRY VYUKOV "AS IS" AND ANY EXPRESS OR IMPLIED
|
THIS SOFTWARE IS PROVIDED BY DMITRY VYUKOV "AS IS" AND ANY EXPRESS OR IMPLIED
|
||||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||||
SHALL DMITRY VYUKOV OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
SHALL DMITRY VYUKOV OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
||||||
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||||
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
The views and conclusions contained in the software and documentation are those of the authors and
|
The views and conclusions contained in the software and documentation are those of the authors and
|
||||||
should not be interpreted as representing official policies, either expressed or implied, of Dmitry Vyukov.
|
should not be interpreted as representing official policies, either expressed or implied, of Dmitry Vyukov.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
The code in its current form adds the license below:
|
The code in its current form adds the license below:
|
||||||
|
|
||||||
Copyright(c) 2015 Gabi Melman.
|
Copyright(c) 2015 Gabi Melman.
|
||||||
Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include "spdlog/common.h"
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
namespace spdlog
|
namespace spdlog
|
||||||
{
|
{
|
||||||
namespace details
|
namespace details
|
||||||
{
|
{
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class mpmc_bounded_queue
|
class mpmc_bounded_queue
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
using item_type = T;
|
using item_type = T;
|
||||||
mpmc_bounded_queue(size_t buffer_size)
|
mpmc_bounded_queue(size_t buffer_size)
|
||||||
:max_size_(buffer_size),
|
:max_size_(buffer_size),
|
||||||
buffer_(new cell_t[buffer_size]),
|
buffer_(new cell_t[buffer_size]),
|
||||||
buffer_mask_(buffer_size - 1)
|
buffer_mask_(buffer_size - 1)
|
||||||
{
|
{
|
||||||
//queue size must be power of two
|
//queue size must be power of two
|
||||||
if (!((buffer_size >= 2) && ((buffer_size & (buffer_size - 1)) == 0)))
|
if (!((buffer_size >= 2) && ((buffer_size & (buffer_size - 1)) == 0)))
|
||||||
throw spdlog_ex("async logger queue size must be power of two");
|
throw spdlog_ex("async logger queue size must be power of two");
|
||||||
|
|
||||||
for (size_t i = 0; i != buffer_size; i += 1)
|
for (size_t i = 0; i != buffer_size; i += 1)
|
||||||
buffer_[i].sequence_.store(i, std::memory_order_relaxed);
|
buffer_[i].sequence_.store(i, std::memory_order_relaxed);
|
||||||
enqueue_pos_.store(0, std::memory_order_relaxed);
|
enqueue_pos_.store(0, std::memory_order_relaxed);
|
||||||
dequeue_pos_.store(0, std::memory_order_relaxed);
|
dequeue_pos_.store(0, std::memory_order_relaxed);
|
||||||
}
|
}
|
||||||
|
|
||||||
~mpmc_bounded_queue()
|
~mpmc_bounded_queue()
|
||||||
{
|
{
|
||||||
delete[] buffer_;
|
delete[] buffer_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool enqueue(T&& data)
|
bool enqueue(T&& data)
|
||||||
{
|
{
|
||||||
cell_t* cell;
|
cell_t* cell;
|
||||||
size_t pos = enqueue_pos_.load(std::memory_order_relaxed);
|
size_t pos = enqueue_pos_.load(std::memory_order_relaxed);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
cell = &buffer_[pos & buffer_mask_];
|
cell = &buffer_[pos & buffer_mask_];
|
||||||
size_t seq = cell->sequence_.load(std::memory_order_acquire);
|
size_t seq = cell->sequence_.load(std::memory_order_acquire);
|
||||||
intptr_t dif = static_cast<intptr_t>(seq) - static_cast<intptr_t>(pos);
|
intptr_t dif = static_cast<intptr_t>(seq) - static_cast<intptr_t>(pos);
|
||||||
if (dif == 0) {
|
if (dif == 0) {
|
||||||
if (enqueue_pos_.compare_exchange_weak(pos, pos + 1, std::memory_order_relaxed))
|
if (enqueue_pos_.compare_exchange_weak(pos, pos + 1, std::memory_order_relaxed))
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (dif < 0) {
|
else if (dif < 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
pos = enqueue_pos_.load(std::memory_order_relaxed);
|
pos = enqueue_pos_.load(std::memory_order_relaxed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cell->data_ = std::move(data);
|
cell->data_ = std::move(data);
|
||||||
cell->sequence_.store(pos + 1, std::memory_order_release);
|
cell->sequence_.store(pos + 1, std::memory_order_release);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool dequeue(T& data)
|
bool dequeue(T& data)
|
||||||
{
|
{
|
||||||
cell_t* cell;
|
cell_t* cell;
|
||||||
size_t pos = dequeue_pos_.load(std::memory_order_relaxed);
|
size_t pos = dequeue_pos_.load(std::memory_order_relaxed);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
cell = &buffer_[pos & buffer_mask_];
|
cell = &buffer_[pos & buffer_mask_];
|
||||||
size_t seq =
|
size_t seq =
|
||||||
cell->sequence_.load(std::memory_order_acquire);
|
cell->sequence_.load(std::memory_order_acquire);
|
||||||
intptr_t dif = static_cast<intptr_t>(seq) - static_cast<intptr_t>(pos + 1);
|
intptr_t dif = static_cast<intptr_t>(seq) - static_cast<intptr_t>(pos + 1);
|
||||||
if (dif == 0) {
|
if (dif == 0) {
|
||||||
if (dequeue_pos_.compare_exchange_weak(pos, pos + 1, std::memory_order_relaxed))
|
if (dequeue_pos_.compare_exchange_weak(pos, pos + 1, std::memory_order_relaxed))
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (dif < 0)
|
else if (dif < 0)
|
||||||
return false;
|
return false;
|
||||||
else
|
else
|
||||||
pos = dequeue_pos_.load(std::memory_order_relaxed);
|
pos = dequeue_pos_.load(std::memory_order_relaxed);
|
||||||
}
|
}
|
||||||
data = std::move(cell->data_);
|
data = std::move(cell->data_);
|
||||||
cell->sequence_.store(pos + buffer_mask_ + 1, std::memory_order_release);
|
cell->sequence_.store(pos + buffer_mask_ + 1, std::memory_order_release);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_empty()
|
bool is_empty()
|
||||||
{
|
{
|
||||||
unsigned front, front1, back;
|
size_t front, front1, back;
|
||||||
// try to take a consistent snapshot of front/tail.
|
// try to take a consistent snapshot of front/tail.
|
||||||
do {
|
do {
|
||||||
front = enqueue_pos_.load(std::memory_order_acquire);
|
front = enqueue_pos_.load(std::memory_order_acquire);
|
||||||
back = dequeue_pos_.load(std::memory_order_acquire);
|
back = dequeue_pos_.load(std::memory_order_acquire);
|
||||||
front1 = enqueue_pos_.load(std::memory_order_relaxed);
|
front1 = enqueue_pos_.load(std::memory_order_relaxed);
|
||||||
} while (front != front1);
|
} while (front != front1);
|
||||||
return back == front;
|
return back == front;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct cell_t
|
struct cell_t
|
||||||
{
|
{
|
||||||
std::atomic<size_t> sequence_;
|
std::atomic<size_t> sequence_;
|
||||||
T data_;
|
T data_;
|
||||||
};
|
};
|
||||||
|
|
||||||
size_t const max_size_;
|
size_t const max_size_;
|
||||||
|
|
||||||
static size_t const cacheline_size = 64;
|
static size_t const cacheline_size = 64;
|
||||||
typedef char cacheline_pad_t[cacheline_size];
|
typedef char cacheline_pad_t[cacheline_size];
|
||||||
|
|
||||||
cacheline_pad_t pad0_;
|
cacheline_pad_t pad0_;
|
||||||
cell_t* const buffer_;
|
cell_t* const buffer_;
|
||||||
size_t const buffer_mask_;
|
size_t const buffer_mask_;
|
||||||
cacheline_pad_t pad1_;
|
cacheline_pad_t pad1_;
|
||||||
std::atomic<size_t> enqueue_pos_;
|
std::atomic<size_t> enqueue_pos_;
|
||||||
cacheline_pad_t pad2_;
|
cacheline_pad_t pad2_;
|
||||||
std::atomic<size_t> dequeue_pos_;
|
std::atomic<size_t> dequeue_pos_;
|
||||||
cacheline_pad_t pad3_;
|
cacheline_pad_t pad3_;
|
||||||
|
|
||||||
mpmc_bounded_queue(mpmc_bounded_queue const&) = delete;
|
mpmc_bounded_queue(mpmc_bounded_queue const&) = delete;
|
||||||
void operator= (mpmc_bounded_queue const&) = delete;
|
void operator= (mpmc_bounded_queue const&) = delete;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // ns details
|
} // ns details
|
||||||
} // ns spdlog
|
} // ns spdlog
|
||||||
|
Loading…
Reference in New Issue
Block a user