aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/threading/equeue/equeue.h
blob: 40dd342585ee63e94e99b20ed8eea67649069a91 (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
#pragma once

#include <util/thread/pool.h>
#include <util/system/atomic.h>
#include <util/generic/ptr.h>

//actual queue limit will be (maxQueueSize - numBusyThreads) or 0
class TElasticQueue: public IThreadPool {
public:
    explicit TElasticQueue(THolder<IThreadPool> slaveQueue);

    bool Add(IObjectInQueue* obj) override;
    size_t Size() const noexcept override;

    void Start(size_t threadCount, size_t maxQueueSize) override;
    void Stop() noexcept override;

    size_t ObjectCount() const;
private:
    class TDecrementingWrapper;

    bool TryIncCounter();
private:
    THolder<IThreadPool> SlaveQueue_;
    size_t MaxQueueSize_ = 0;
    TAtomic ObjectCount_ = 0;
    TAtomic GuardCount_ = 0;
};