00001 #if !defined(HFILE_libinline) 00002 #define HFILE_libinline 00003 00004 #include <sys/types.h> 00005 00006 namespace android { 00007 00008 typedef int32_t status_t; 00009 00010 #if 1 00011 template <typename T> class sp { 00012 public: 00013 inline sp() { m_ptr = 0; } 00014 inline sp(T* other) { m_ptr = other; if (other) other->incStrong(this); } 00015 inline ~sp() { if (m_ptr) m_ptr->decStrong(this); } 00016 inline sp& operator=(T* other) { if (other) other->incStrong(this); if (m_ptr) m_ptr->decStrong(this); m_ptr = other; return *this; } 00017 inline sp& operator=(const sp<T>& other) { T* otherPtr(other.m_ptr); if (otherPtr) otherPtr->incStrong(this); if (m_ptr) m_ptr->decStrong(this); m_ptr = otherPtr; return *this; } 00018 template<typename U> 00019 inline sp& operator=(const sp<U>& other) { T* otherPtr(other.m_ptr); if (otherPtr) otherPtr->incStrong(this); if (m_ptr) m_ptr->decStrong(this); m_ptr = otherPtr; return *this; } 00020 inline T& operator*() const { return *m_ptr; } 00021 inline T* operator->() const { return m_ptr; } 00022 inline T* get() const { return m_ptr; } 00023 inline operator T*() const { return m_ptr; } 00024 private: 00025 template<typename Y> friend class wp; 00026 T* m_ptr; 00027 }; 00028 00029 #else 00030 template <typename T> class sp { 00031 public: 00032 inline sp() { m_ptr = 0; } 00033 inline sp(T* other) { m_ptr = other; } 00034 inline ~sp() { m_ptr = 0; } 00035 inline sp& operator=(T* other) { m_ptr = other; } 00036 inline sp& operator=(const sp<T>& other) { m_ptr = other; } 00037 template<typename U> 00038 inline sp& operator=(const sp<U>& other) { m_ptr = other; } 00039 inline T& operator*() const { return *m_ptr; } 00040 inline T* operator->() const { return m_ptr; } 00041 inline T* get() const { return m_ptr; } 00042 inline operator T*() const { return m_ptr; } 00043 private: 00044 template<typename Y> friend class wp; 00045 T* m_ptr; 00046 }; 00047 #endif 00048 00049 } //end of namespace android 00050 00051 #endif //end of lib 00052 00054 #ifndef _LIBS_UTILS_SYNC_H 00055 #define _LIBS_UTILS_SYNC_H 00056 00057 #include <stdint.h> 00058 #include <sys/types.h> 00059 #include <pthread.h> 00060 00061 namespace android { 00062 00063 class Condition; 00064 00065 class Mutex { 00066 public: 00067 inline Mutex() {pthread_mutex_init(&mMutex, NULL);} 00068 inline ~Mutex() {pthread_mutex_destroy(&mMutex);} 00069 inline status_t lock() {return -pthread_mutex_lock(&mMutex);} 00070 inline void unlock() {pthread_mutex_unlock(&mMutex);} 00071 inline status_t tryLock() {return -pthread_mutex_trylock(&mMutex);} 00072 class Autolock { 00073 public: 00074 inline Autolock(Mutex& mutex) : mLock(mutex) { mLock.lock(); } 00075 inline Autolock(Mutex* mutex) : mLock(*mutex) { mLock.lock(); } 00076 inline ~Autolock() { mLock.unlock(); } 00077 private: 00078 Mutex& mLock; 00079 }; 00080 private: 00081 friend class Condition; 00082 Mutex(const Mutex&); 00083 Mutex& operator = (const Mutex&); 00084 pthread_mutex_t mMutex; 00085 }; 00086 typedef Mutex::Autolock AutoMutex; 00087 00088 class Condition { 00089 public: 00090 inline Condition() {pthread_cond_init(&mCond, NULL);} 00091 inline ~Condition() {pthread_cond_destroy(&mCond);} 00092 inline status_t wait(Mutex& mutex) {return -pthread_cond_wait(&mCond, &mutex.mMutex);} 00093 inline status_t waitAbsMono(Mutex& mutex, const struct timespec *abstime) { return -pthread_cond_timedwait_monotonic_np(&mCond, &mutex.mMutex, abstime);} 00094 inline void signal() {pthread_cond_signal(&mCond);} 00095 inline void broadcast() {pthread_cond_broadcast(&mCond);} 00096 private: 00097 pthread_cond_t mCond; 00098 }; 00099 00100 }; // namespace android 00101 00102 #endif // _LIBS_UTILS_SYNC_H