blob: c2f85549f4daa03936f0de3cf3a4cd9dc00400ed [file] [log] [blame]
Owen Andersone3cd5ca2009-06-18 16:54:52 +00001//===-- llvm/System/Threading.cpp- Control multithreading mode --*- C++ -*-==//
Owen Anderson4c7ac182009-06-16 17:33:51 +00002//
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 implements llvm_start_multithreaded() and friends.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000014#include "llvm/Support/Threading.h"
15#include "llvm/Support/Atomic.h"
16#include "llvm/Support/Mutex.h"
Chris Lattnerc8808152009-07-06 17:24:48 +000017#include "llvm/Config/config.h"
Owen Anderson4c7ac182009-06-16 17:33:51 +000018#include <cassert>
Owen Anderson643dda12009-07-06 21:24:37 +000019
Owen Anderson4c7ac182009-06-16 17:33:51 +000020using namespace llvm;
21
22static bool multithreaded_mode = false;
23
24static sys::Mutex* global_lock = 0;
25
26bool llvm::llvm_start_multithreaded() {
27#ifdef LLVM_MULTITHREADED
28 assert(!multithreaded_mode && "Already multithreaded!");
29 multithreaded_mode = true;
30 global_lock = new sys::Mutex(true);
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000031
Owen Anderson4c7ac182009-06-16 17:33:51 +000032 // We fence here to ensure that all initialization is complete BEFORE we
33 // return from llvm_start_multithreaded().
34 sys::MemoryFence();
35 return true;
36#else
37 return false;
38#endif
39}
40
41void llvm::llvm_stop_multithreaded() {
42#ifdef LLVM_MULTITHREADED
43 assert(multithreaded_mode && "Not currently multithreaded!");
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000044
Owen Anderson4c7ac182009-06-16 17:33:51 +000045 // We fence here to insure that all threaded operations are complete BEFORE we
46 // return from llvm_stop_multithreaded().
47 sys::MemoryFence();
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000048
Owen Anderson4c7ac182009-06-16 17:33:51 +000049 multithreaded_mode = false;
50 delete global_lock;
51#endif
52}
53
54bool llvm::llvm_is_multithreaded() {
55 return multithreaded_mode;
56}
57
58void llvm::llvm_acquire_global_lock() {
59 if (multithreaded_mode) global_lock->acquire();
60}
61
62void llvm::llvm_release_global_lock() {
63 if (multithreaded_mode) global_lock->release();
Owen Anderson1cca27e2009-06-16 20:53:09 +000064}
Daniel Dunbarea8e2062010-11-04 01:26:25 +000065
66#if defined(LLVM_MULTITHREADED) && defined(HAVE_PTHREAD_H)
67#include <pthread.h>
68
69struct ThreadInfo {
70 void (*UserFn)(void *);
71 void *UserData;
72};
73static void *ExecuteOnThread_Dispatch(void *Arg) {
74 ThreadInfo *TI = reinterpret_cast<ThreadInfo*>(Arg);
75 TI->UserFn(TI->UserData);
76 return 0;
77}
78
79void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData,
80 unsigned RequestedStackSize) {
81 ThreadInfo Info = { Fn, UserData };
82 pthread_attr_t Attr;
83 pthread_t Thread;
84
85 // Construct the attributes object.
86 if (::pthread_attr_init(&Attr) != 0)
87 return;
88
89 // Set the requested stack size, if given.
90 if (RequestedStackSize != 0) {
91 if (::pthread_attr_setstacksize(&Attr, RequestedStackSize) != 0)
92 goto error;
93 }
94
95 // Construct and execute the thread.
96 if (::pthread_create(&Thread, &Attr, ExecuteOnThread_Dispatch, &Info) != 0)
97 goto error;
98
99 // Wait for the thread and clean up.
100 ::pthread_join(Thread, 0);
Michael J. Spencer1f6efa32010-11-29 18:16:10 +0000101
Daniel Dunbarea8e2062010-11-04 01:26:25 +0000102 error:
103 ::pthread_attr_destroy(&Attr);
104}
105
106#else
107
108// No non-pthread implementation, currently.
109
110void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData,
111 unsigned RequestedStackSize) {
112 (void) RequestedStackSize;
113 Fn(UserData);
114}
115
116#endif