aboutsummaryrefslogtreecommitdiff
path: root/offload/plugins-nextgen/level_zero/include/TLS.h
blob: eb0e6d22ff407156517f47247e0e093b516857bc (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//===--- Level Zero Target RTL Implementation -----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Thread Level Storage abstraction.
//
//===----------------------------------------------------------------------===//

#ifndef OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_LEVEL_ZERO_TLS_H
#define OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_LEVEL_ZERO_TLS_H

#include <bitset>

#include "AsyncQueue.h"
#include "L0Memory.h"
#include "L0Trace.h"
#include "PerThreadTable.h"

namespace llvm::omp::target::plugin {

/// All thread-local data used by the Plugin.
class L0ThreadTLSTy {
  /// Async info tracking.
  static constexpr int32_t PerThreadQueues = 10;
  std::bitset<PerThreadQueues> InUseQueues{0};
  AsyncQueueTy AsyncQueues[PerThreadQueues];

public:
  L0ThreadTLSTy() = default;
  L0ThreadTLSTy(const L0ThreadTLSTy &) = delete;
  L0ThreadTLSTy(L0ThreadTLSTy &&) = delete;
  L0ThreadTLSTy &operator=(const L0ThreadTLSTy &) = delete;
  L0ThreadTLSTy &operator=(const L0ThreadTLSTy &&) = delete;
  ~L0ThreadTLSTy() = default;

  AsyncQueueTy *getAsyncQueue() {
    AsyncQueueTy *Ret = nullptr;
    if (!InUseQueues.all()) {
      // there's a free queue in this thread, find it.
      for (size_t Queue = 0; Queue < PerThreadQueues; Queue++) {
        if (!InUseQueues.test(Queue)) {
          InUseQueues.set(Queue);
          Ret = &AsyncQueues[Queue];
          break;
        }
      }
      assert(Ret && "A queue should have been found!");
    }
    return Ret;
  }

  bool releaseAsyncQueue(AsyncQueueTy *Queue) {
    if (Queue >= &AsyncQueues[0] && Queue < &AsyncQueues[PerThreadQueues]) {
      // it's a local queue
      size_t QueueId = Queue - &AsyncQueues[0];
      InUseQueues.reset(QueueId);
      return true;
    }
    return false;
  }
};

using L0ThreadTblTy = PerThread<L0ThreadTLSTy>;

} // namespace llvm::omp::target::plugin

#endif // OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_LEVEL_ZERO_TLS_H