• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- A platform independent abstraction layer for cond vars --*- 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 #ifndef LLVM_LIBC___SUPPORT_SRC_THREADS_LINUX_CNDVAR_H
10 #define LLVM_LIBC___SUPPORT_SRC_THREADS_LINUX_CNDVAR_H
11 
12 #include "src/__support/threads/linux/futex_utils.h" // Futex
13 #include "src/__support/threads/linux/raw_mutex.h"   // RawMutex
14 #include "src/__support/threads/mutex.h"             // Mutex
15 
16 #include <stdint.h> // uint32_t
17 
18 namespace LIBC_NAMESPACE {
19 
20 class CndVar {
21   enum CndWaiterStatus : uint32_t {
22     WS_Waiting = 0xE,
23     WS_Signalled = 0x5,
24   };
25 
26   struct CndWaiter {
27     Futex futex_word = WS_Waiting;
28     CndWaiter *next = nullptr;
29   };
30 
31   CndWaiter *waitq_front;
32   CndWaiter *waitq_back;
33   RawMutex qmtx;
34 
35 public:
init(CndVar * cv)36   LIBC_INLINE static int init(CndVar *cv) {
37     cv->waitq_front = cv->waitq_back = nullptr;
38     RawMutex::init(&cv->qmtx);
39     return 0;
40   }
41 
destroy(CndVar * cv)42   LIBC_INLINE static void destroy(CndVar *cv) {
43     cv->waitq_front = cv->waitq_back = nullptr;
44   }
45 
46   // Returns 0 on success, -1 on error.
47   int wait(Mutex *m);
48   void notify_one();
49   void broadcast();
50 };
51 
52 } // namespace LIBC_NAMESPACE
53 
54 #endif // LLVM_LIBC_SRC___SUPPORT_THREADS_LINUX_CNDVAR_H
55