summaryrefslogtreecommitdiff
path: root/map65/half_symbol_scheduler.h
blob: b4b1e2b5ec86dfca26bfbf64be1dd402a9d75263 (plain)
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
#ifndef MAP65_HALF_SYMBOL_SCHEDULER_H
#define MAP65_HALF_SYMBOL_SCHEDULER_H

#include <atomic>

class Map65HalfSymbolScheduler
{
public:
  void reset () noexcept { scheduled_half_symbol_ = 0; }

  bool claim_next (int available_half_symbol, std::atomic_bool& busy) noexcept
  {
    if (available_half_symbol <= scheduled_half_symbol_)
      {
        return false;
      }

    bool expected = false;
    if (!busy.compare_exchange_strong (expected, true,
                                       std::memory_order_acq_rel,
                                       std::memory_order_acquire))
      {
        return false;
      }

    ++scheduled_half_symbol_;
    return true;
  }

  int scheduled_half_symbol () const noexcept { return scheduled_half_symbol_; }

private:
  int scheduled_half_symbol_ {0};
};

#endif