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

#include <util/system/atomic.h>
#include <util/system/condvar.h>
#include <util/system/mutex.h>

namespace NBus {
    namespace NPrivate {
        class TBusSessionJobCount {
        private:
            TAtomic JobCount;

            TMutex Mutex;
            TCondVar CondVar;

        public:
            TBusSessionJobCount();
            ~TBusSessionJobCount();

            void Add(unsigned delta) {
                AtomicAdd(JobCount, delta);
            }

            void Increment() {
                Add(1);
            }

            void Decrement() {
                if (AtomicDecrement(JobCount) == 0) {
                    TGuard<TMutex> guard(Mutex);
                    CondVar.BroadCast();
                }
            }

            void WaitForZero();
        };

    }
}