DPC++ Runtime
Runtime libraries for oneAPI DPC++
util.hpp
Go to the documentation of this file.
1 //===-- util.hpp - Shared SYCL runtime utilities interface -----*- C++ -*--===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
11 #ifndef __SYCL_DEVICE_ONLY
12 
13 #include <sycl/detail/defines.hpp>
14 #include <sycl/stl.hpp>
15 
16 #include <cstring>
17 #include <mutex>
18 
19 namespace sycl {
21 namespace detail {
22 
24 class Sync {
25 public:
28  static std::mutex &getGlobalLock() { return getInstance().GlobalLock; }
29 
30 private:
31  static Sync &getInstance();
32  std::mutex GlobalLock;
33 };
34 
35 // TempAssignGuard is the class for a guard object that will assign some OTHER
36 // variable to a temporary value but restore it when the guard itself goes out
37 // of scope.
38 template <typename T> struct TempAssignGuard {
39  T &field;
41  TempAssignGuard(T &fld, T tempVal) : field(fld), restoreValue(fld) {
42  field = tempVal;
43  }
44  ~TempAssignGuard() { field = restoreValue; }
45 };
46 
47 // const char* key hash for STL maps
48 struct HashCStr {
49  size_t operator()(const char *S) const {
50  constexpr size_t Prime = 31;
51  size_t Res = 0;
52  char Ch = 0;
53 
54  for (; (Ch = *S); S++) {
55  Res += Ch + (Prime * Res);
56  }
57  return Res;
58  }
59 };
60 
61 // const char* key comparison for STL maps
62 struct CmpCStr {
63  bool operator()(const char *A, const char *B) const {
64  return std::strcmp(A, B) == 0;
65  }
66 };
67 
68 using SerializedObj = std::vector<unsigned char>;
69 
70 } // namespace detail
71 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
72 } // namespace sycl
73 
74 #endif //__SYCL_DEVICE_ONLY
Groups and provides access to all the locks used the SYCL runtime.
Definition: util.hpp:24
static std::mutex & getGlobalLock()
Retuns a reference to the global lock.
Definition: util.hpp:28
#define __SYCL_INLINE_VER_NAMESPACE(X)
std::vector< unsigned char > SerializedObj
Definition: util.hpp:68
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
bool operator()(const char *A, const char *B) const
Definition: util.hpp:63
size_t operator()(const char *S) const
Definition: util.hpp:49
TempAssignGuard(T &fld, T tempVal)
Definition: util.hpp:41