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

    }
}