d6d6da61f9efb35c4da5fb88348aa4bad950e01f
[oota-llvm.git] / include / llvm / Support / Threading.h
1 //===-- llvm/Support/Threading.h - Control multithreading mode --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares helper functions for running LLVM in a multi-threaded
11 // environment.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_THREADING_H
16 #define LLVM_SUPPORT_THREADING_H
17
18 #include "llvm/Config/llvm-config.h" // for LLVM_ON_UNIX
19
20 #if defined(LLVM_ON_UNIX)
21 #include <mutex>
22 #else
23 #include "llvm/Support/Atomic.h"
24 #endif
25
26 namespace llvm {
27   /// Returns true if LLVM is compiled with support for multi-threading, and
28   /// false otherwise.
29   bool llvm_is_multithreaded();
30
31   /// llvm_execute_on_thread - Execute the given \p UserFn on a separate
32   /// thread, passing it the provided \p UserData.
33   ///
34   /// This function does not guarantee that the code will actually be executed
35   /// on a separate thread or honoring the requested stack size, but tries to do
36   /// so where system support is available.
37   ///
38   /// \param UserFn - The callback to execute.
39   /// \param UserData - An argument to pass to the callback function.
40   /// \param RequestedStackSize - If non-zero, a requested size (in bytes) for
41   /// the thread stack.
42   void llvm_execute_on_thread(void (*UserFn)(void*), void *UserData,
43                               unsigned RequestedStackSize = 0);
44
45 #if defined(LLVM_ON_UNIX)
46 typedef std::once_flag once_flag;
47 #define LLVM_DEFINE_ONCE_FLAG(flag) static once_flag flag
48 #else
49 enum InitStatus {
50   Done = -1,
51   Uninitialized = 0,
52   Wait = 1
53 };
54 typedef volatile sys::cas_flag once_flag;
55
56 #define LLVM_DEFINE_ONCE_FLAG(flag) static once_flag flag = Uninitialized
57 #endif
58
59 /// \brief Execute the function specified as a parameter once.
60 ///
61 /// Typical usage:
62 /// \code
63 ///   void foo() {...};
64 ///   ...
65 ///   LLVM_DEFINE_ONCE_FLAG(flag);
66 ///   call_once(flag, foo);
67 /// \endcode
68 ///
69 /// \param flag Flag used for tracking whether or not this has run.
70 /// \param UserFn Function to call once.
71 void call_once(once_flag&, void (*)(void));
72
73 }
74
75 #endif