LLVM API Documentation

Threading.h
Go to the documentation of this file.
00001 //===-- llvm/Support/Threading.h - Control multithreading mode --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file declares helper functions for running LLVM in a multi-threaded
00011 // environment.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_SUPPORT_THREADING_H
00016 #define LLVM_SUPPORT_THREADING_H
00017 
00018 #include "llvm/Config/llvm-config.h" // for LLVM_ON_UNIX
00019 
00020 #if defined(LLVM_ON_UNIX)
00021 #include <mutex>
00022 #else
00023 #include "llvm/Support/Atomic.h"
00024 #endif
00025 
00026 namespace llvm {
00027   /// Returns true if LLVM is compiled with support for multi-threading, and
00028   /// false otherwise.
00029   bool llvm_is_multithreaded();
00030 
00031   /// llvm_execute_on_thread - Execute the given \p UserFn on a separate
00032   /// thread, passing it the provided \p UserData.
00033   ///
00034   /// This function does not guarantee that the code will actually be executed
00035   /// on a separate thread or honoring the requested stack size, but tries to do
00036   /// so where system support is available.
00037   ///
00038   /// \param UserFn - The callback to execute.
00039   /// \param UserData - An argument to pass to the callback function.
00040   /// \param RequestedStackSize - If non-zero, a requested size (in bytes) for
00041   /// the thread stack.
00042   void llvm_execute_on_thread(void (*UserFn)(void*), void *UserData,
00043                               unsigned RequestedStackSize = 0);
00044 
00045 #if defined(LLVM_ON_UNIX)
00046 typedef std::once_flag once_flag;
00047 #define LLVM_DEFINE_ONCE_FLAG(flag) static once_flag flag
00048 #else
00049 enum InitStatus {
00050   Done = -1,
00051   Uninitialized = 0,
00052   Wait = 1
00053 };
00054 typedef volatile sys::cas_flag once_flag;
00055 
00056 #define LLVM_DEFINE_ONCE_FLAG(flag) static once_flag flag = Uninitialized
00057 #endif
00058 
00059 /// \brief Execute the function specified as a parameter once.
00060 ///
00061 /// Typical usage:
00062 /// \code
00063 ///   void foo() {...};
00064 ///   ...
00065 ///   LLVM_DEFINE_ONCE_FLAG(flag);
00066 ///   call_once(flag, foo);
00067 /// \endcode
00068 ///
00069 /// \param flag Flag used for tracking whether or not this has run.
00070 /// \param UserFn Function to call once.
00071 void call_once(once_flag &flag, void (*UserFn)(void));
00072 }
00073 
00074 #endif