diff options
author | Chris Lattner <sabre@nondot.org> | 2004-01-17 19:54:29 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2004-01-17 19:54:29 +0000 |
commit | 440f87eea20f11ea86822816fae965956795d6d3 (patch) | |
tree | be84096b985cf4b719e9e556230c898afc2db9cc /include/llvm/Support/ThreadSupport-PThreads.h | |
parent | 64811a3e184a01cd8fde35a91f79e92d7314a63c (diff) |
Revision of Brian's threading support library to be a bit more generic and
platform independent. This code is completely untested (but never used),
and needs autoconf support for detecting pthreads, but it's a start, and
deletes two emails from my inbox. :)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@10906 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/llvm/Support/ThreadSupport-PThreads.h')
-rw-r--r-- | include/llvm/Support/ThreadSupport-PThreads.h | 75 |
1 files changed, 24 insertions, 51 deletions
diff --git a/include/llvm/Support/ThreadSupport-PThreads.h b/include/llvm/Support/ThreadSupport-PThreads.h index 2fcbe72a81..6bbe682c08 100644 --- a/include/llvm/Support/ThreadSupport-PThreads.h +++ b/include/llvm/Support/ThreadSupport-PThreads.h @@ -1,4 +1,4 @@ -//===-- Support/Lock.h - Platform-agnostic mutual exclusion -----*- C++ -*-===// +//===-- Support/ThreadSupport-PThreads.h - PThreads support -----*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -7,63 +7,36 @@ // //===----------------------------------------------------------------------===// // -// This file contains classes that implement locks (mutual exclusion -// variables) in a platform-agnostic way. Basically the user should -// just call Lock::create() to get a Lock object of the correct sort -// for the current platform, and use its acquire() and release() -// methods, or a LockHolder, to protect critical sections of code for -// thread-safety. +// This file defines pthreads implementations of the generic threading +// mechanisms. Users should never #include this file directly! // //===----------------------------------------------------------------------===// -#ifndef SUPPORT_LOCK_H -#define SUPPORT_LOCK_H +// Users should never #include this file directly! As such, no include guards +// are needed. + +#ifndef SUPPORT_THREADSUPPORT_H +#error "Code should not #include Support/ThreadSupport/PThreads.h directly!" +#endif #include <pthread.h> -#include <cstdlib> namespace llvm { -/// Lock - Abstract class that provides mutual exclusion (also known -/// as a mutex.) -/// -class Lock { -protected: - virtual ~Lock() {} // Derive from me -public: - virtual void acquire () { abort (); } - virtual void release () { abort (); } - - /// create - Static method that returns a Lock of the correct class - /// for the current host OS. + /// Mutex - This class allows user code to protect variables shared between + /// threads. It implements a "recursive" mutex, to simplify user code. /// - static Lock create (); -}; - -/// POSIXLock - Specialization of Lock class implemented using -/// pthread_mutex_t objects. -/// -class POSIXLock : public Lock { - pthread_mutex_t mutex; -public: - POSIXLock () { pthread_mutex_init (&mutex, 0); } - virtual ~POSIXLock () { pthread_mutex_destroy (&mutex); } - virtual void acquire () { pthread_mutex_lock (&mutex); } - virtual void release () { pthread_mutex_unlock (&mutex); } -}; - -/// LockHolder - Instances of this class acquire a given Lock when -/// constructed and hold that lock until destruction. Uncle Bjarne -/// says, "Resource acquisition is allocation." Or is it the other way -/// around? I never can remember. -/// -class LockHolder { - Lock &L; -public: - LockHolder (Lock &_L) : L (_L) { L.acquire (); } - ~LockHolder () { L.release (); } -}; - + class Mutex { + pthread_mutex_t mutex; + Mutex(const Mutex &); // DO NOT IMPLEMENT + void operator=(const Mutex &); // DO NOT IMPLEMENT + public: + Mutex() { + pthread_mutexattr_t Attr; + pthread_mutex_init(&mutex, &Attr); + } + ~Mutex() { pthread_mutex_destroy(&mutex); } + void acquire () { pthread_mutex_lock (&mutex); } + void release () { pthread_mutex_unlock (&mutex); } + }; } // end namespace llvm - -#endif // SUPPORT_LOCK_H |