aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/remote_client_session_semaphore.h
blob: b25e5e6aab74714af708377074b91d2e7a929020 (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
#pragma once

#include "cc_semaphore.h"

#include <util/generic/noncopyable.h>
#include <library/cpp/deprecated/atomic/atomic.h>
#include <util/system/condvar.h>
#include <util/system/mutex.h>

namespace NBus {
    namespace NPrivate {
        class TRemoteClientSessionSemaphore: public TComplexConditionSemaphore<TRemoteClientSessionSemaphore> {
        private:
            const char* const Name;

            TAtomicBase const Limit;
            TAtomic Current;
            TAtomic StopSignal;

        public:
            TRemoteClientSessionSemaphore(TAtomicBase limit, const char* name = "unnamed");
            ~TRemoteClientSessionSemaphore();

            TAtomicBase GetCurrent() const {
                return AtomicGet(Current);
            }

            void Acquire();
            bool TryAcquire();
            void Increment();
            void IncrementMultiple(TAtomicBase count);
            bool TryWait();
            void Release();
            void ReleaseMultiple(TAtomicBase count);
            void Stop();

        private:
            void CheckNeedToUnlock();
        };

    }
}