File: lock.hpp

package info (click to toggle)
ares 126-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,600 kB
  • sloc: cpp: 356,508; ansic: 20,394; makefile: 16; sh: 2
file content (50 lines) | stat: -rw-r--r-- 841 bytes parent folder | download | duplicates (4)
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
42
43
44
45
46
47
48
49
50
#pragma once

//shared functionality used for pObject on all platforms

struct Lock {
  struct Handle {
    Handle(const Lock* self) : self(self) {
      if(self) ++self->locks;
    }

    ~Handle() {
      release();
    }

    auto release() -> bool {
      if(self) {
        --self->locks;
        self = nullptr;
        return true;
      }
      return false;
    }

    const Lock* self = nullptr;
  };

  auto acquired() const -> bool {
    return locks || Application::state().quit;
  }

  auto acquire() const -> Handle {
    return {this};
  }

  //deprecated C-style manual functions
  //prefer RAII acquire() functionality instead in newly written code
  auto locked() const -> bool {
    return acquired();
  }

  auto lock() -> void {
    ++locks;
  }

  auto unlock() -> void {
    --locks;
  }

  mutable s32 locks = 0;
};