aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/session_job_count.h
blob: 8163ad8a389e1a14d4974800234b36c1fb48f79b (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 <library/cpp/deprecated/atomic/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();
        };

    }
}