aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/sem.h
blob: e37799d1489d035f019d16b53ab0d8731d332d66 (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
#pragma once

#include "defaults.h"

#include <util/generic/ptr.h>

// named sempahore
class TSemaphore {
public:
    TSemaphore(const char* name, ui32 maxFreeCount);
    ~TSemaphore();

    // Increase the semaphore counter.
    void Release() noexcept;

    // Keep a thread held while the semaphore counter is equal 0.
    void Acquire() noexcept;

    // Try to enter the semaphore gate. A non-blocking variant of Acquire.
    // Returns 'true' if the semaphore counter decreased
    bool TryAcquire() noexcept;

private:
    class TImpl;
    THolder<TImpl> Impl_;
};

// unnamed semaphore, faster, than previous
class TFastSemaphore {
public:
    TFastSemaphore(ui32 maxFreeCount);
    ~TFastSemaphore();

    void Release() noexcept;
    void Acquire() noexcept;
    bool TryAcquire() noexcept;

private:
    class TImpl;
    THolder<TImpl> Impl_;
};