aboutsummaryrefslogtreecommitdiffstats
path: root/yt/yt/core/misc/mpsc_sharded_queue.h
blob: 81c16b8f2b27c166a7ee85186cd323cc50556245 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once

#include <yt/yt/core/profiling/tscp.h>

#include <library/cpp/yt/threading/spin_lock.h>

#include <vector>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

template <typename TItem>
class TSimpleMpscSpinLockQueue
{
public:
    void Enqueue(TItem item);
    void EnqueueMany(std::vector<TItem>&& items);
    std::vector<TItem>& DequeueAll();

    int GetSize();

private:
    YT_DECLARE_SPIN_LOCK(NThreading::TSpinLock, QueueLock_);
    std::vector<TItem> Queue_;

    std::vector<TItem> Dequeued_;
};

////////////////////////////////////////////////////////////////////////////////

template <typename TItem>
class TMpscShardedQueue
{
public:
    void Enqueue(TItem item);
    void EnqueueMany(std::vector<TItem>&& items);

    // TConsumer is a functor with a signature: void (vector<TItem>& batch)
    template <typename TConsumer>
    i64 ConsumeAll(TConsumer consumer);

    int GetShardSize();

private:
    struct alignas(2 * CacheLineSize) TShard
    {
        TSimpleMpscSpinLockQueue<TItem> Queue;
    };

    std::array<TShard, NProfiling::TTscp::MaxProcessorId> Shards_;
};

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT

#define MPSC_SHARDED_QUEUE_INL_H_
#include "mpsc_sharded_queue-inl.h"
#undef MPSC_SHARDED_QUEUE_INL_H_