aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/threading/equeue/equeue.h
blob: c61b9f7b8570942fb7c221b5eef068c20baeaac8 (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 <library/cpp/deprecated/atomic/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;
};