Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame^] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | // Author: cshapiro@google.com (Carl Shapiro) |
| 3 | |
| 4 | #ifndef ART_SRC_THREAD_H_ |
| 5 | #define ART_SRC_THREAD_H_ |
| 6 | |
| 7 | #include "src/globals.h" |
| 8 | #include "src/logging.h" |
| 9 | #include "src/macros.h" |
| 10 | |
| 11 | namespace art { |
| 12 | |
| 13 | class Object; |
| 14 | class Thread; |
| 15 | |
| 16 | class Mutex { |
| 17 | public: |
| 18 | virtual ~Mutex() {} |
| 19 | |
| 20 | void Lock() {} |
| 21 | |
| 22 | bool TryLock() { return true; } |
| 23 | |
| 24 | void Unlock() {} |
| 25 | |
| 26 | const char* GetName() { return name_; } |
| 27 | |
| 28 | Thread* GetOwner() { return owner_; } |
| 29 | |
| 30 | public: // TODO: protected |
| 31 | explicit Mutex(const char* name) : name_(name), owner_(NULL) {} |
| 32 | |
| 33 | void SetOwner(Thread* thread) { owner_ = thread; } |
| 34 | |
| 35 | private: |
| 36 | const char* name_; |
| 37 | |
| 38 | Thread* owner_; |
| 39 | |
| 40 | DISALLOW_COPY_AND_ASSIGN(Mutex); |
| 41 | }; |
| 42 | |
| 43 | class MutexLock { |
| 44 | public: |
| 45 | explicit MutexLock(Mutex *mu) : mu_(mu) { |
| 46 | mu_->Lock(); |
| 47 | } |
| 48 | ~MutexLock() { mu_->Unlock(); } |
| 49 | private: |
| 50 | Mutex* const mu_; |
| 51 | DISALLOW_COPY_AND_ASSIGN(MutexLock); |
| 52 | }; |
| 53 | |
| 54 | class Thread { |
| 55 | public: |
| 56 | static Thread* Self() { |
| 57 | static Thread self; |
| 58 | return &self; // TODO |
| 59 | } |
| 60 | |
| 61 | uint32_t GetThreadId() { |
| 62 | return thread_id_; |
| 63 | } |
| 64 | |
| 65 | bool IsExceptionPending() const { |
| 66 | return false; // TODO exception_ != NULL; |
| 67 | } |
| 68 | |
| 69 | Object* GetException() const { |
| 70 | return exception_; |
| 71 | } |
| 72 | |
| 73 | void SetException(Object* new_exception) { |
| 74 | CHECK(new_exception != NULL); |
| 75 | // TODO: CHECK(exception_ == NULL); |
| 76 | exception_ = new_exception; // TODO |
| 77 | } |
| 78 | |
| 79 | void ClearException() { |
| 80 | exception_ = NULL; |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | Thread() : thread_id_(1234), exception_(NULL) {} |
| 85 | ~Thread() {} |
| 86 | |
| 87 | uint32_t thread_id_; |
| 88 | |
| 89 | Object* exception_; |
| 90 | |
| 91 | DISALLOW_COPY_AND_ASSIGN(Thread); |
| 92 | }; |
| 93 | |
| 94 | } // namespace art |
| 95 | |
| 96 | #endif // ART_SRC_THREAD_H_ |