summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Gaeke <gaeke@uiuc.edu>2003-12-01 21:33:31 +0000
committerBrian Gaeke <gaeke@uiuc.edu>2003-12-01 21:33:31 +0000
commit59e47e3ee10840beb57ece10af04a6f9bf3495e9 (patch)
tree8d410a2f8e3420d942b380d657379b30c220ed1a
parenta8df7bdbacc0795bdca383035d1a2169445ffb7e (diff)
downloadllvm-59e47e3ee10840beb57ece10af04a6f9bf3495e9.tar.gz
llvm-59e47e3ee10840beb57ece10af04a6f9bf3495e9.tar.bz2
llvm-59e47e3ee10840beb57ece10af04a6f9bf3495e9.tar.xz
Lock abstraction, introduced with a view toward making the JIT thread-safe.
Eventually. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@10284 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/Support/ThreadSupport-PThreads.h69
-rw-r--r--include/llvm/Support/ThreadSupport-PThreads.h69
-rw-r--r--lib/Support/Mutex.cpp24
3 files changed, 162 insertions, 0 deletions
diff --git a/include/Support/ThreadSupport-PThreads.h b/include/Support/ThreadSupport-PThreads.h
new file mode 100644
index 0000000000..2fcbe72a81
--- /dev/null
+++ b/include/Support/ThreadSupport-PThreads.h
@@ -0,0 +1,69 @@
+//===-- Support/Lock.h - Platform-agnostic mutual exclusion -----*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// 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.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef SUPPORT_LOCK_H
+#define SUPPORT_LOCK_H
+
+#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.
+ ///
+ 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 (); }
+};
+
+} // end namespace llvm
+
+#endif // SUPPORT_LOCK_H
diff --git a/include/llvm/Support/ThreadSupport-PThreads.h b/include/llvm/Support/ThreadSupport-PThreads.h
new file mode 100644
index 0000000000..2fcbe72a81
--- /dev/null
+++ b/include/llvm/Support/ThreadSupport-PThreads.h
@@ -0,0 +1,69 @@
+//===-- Support/Lock.h - Platform-agnostic mutual exclusion -----*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// 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.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef SUPPORT_LOCK_H
+#define SUPPORT_LOCK_H
+
+#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.
+ ///
+ 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 (); }
+};
+
+} // end namespace llvm
+
+#endif // SUPPORT_LOCK_H
diff --git a/lib/Support/Mutex.cpp b/lib/Support/Mutex.cpp
new file mode 100644
index 0000000000..6271797da4
--- /dev/null
+++ b/lib/Support/Mutex.cpp
@@ -0,0 +1,24 @@
+//===-- Support/Lock.cpp - Platform-agnostic mutual exclusion -------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Implementation of various methods supporting platform-agnostic lock
+// abstraction. See Support/Lock.h for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "Support/Lock.h"
+
+using namespace llvm;
+
+Lock Lock::create () {
+ // Currently we only support creating POSIX pthread_mutex_t locks.
+ // In the future we might want to construct different kinds of locks
+ // based on what OS is running.
+ return POSIXLock ();
+}