aboutsummaryrefslogblamecommitdiffstats
path: root/library/cpp/messagebus/cc_semaphore.h
blob: 0df8a3d664e2e2db331e7a15128c856fcee3575f (plain) (tree)

























                                       

                                




                                         
#pragma once

#include "latch.h"

template <typename TThis>
class TComplexConditionSemaphore {
private:
    TLatch Latch;

public:
    void Updated() {
        if (GetThis()->TryWait()) {
            Latch.Unlock();
        }
    }

    void Wait() {
        while (!GetThis()->TryWait()) {
            Latch.Lock();
            if (GetThis()->TryWait()) {
                Latch.Unlock();
                return;
            }
            Latch.Wait();
        }
    }

    bool IsLocked() {
        return Latch.IsLocked();
    }

private:
    TThis* GetThis() {
        return static_cast<TThis*>(this);
    }
};