The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2005 The Android Open Source Project |
| 3 | // |
| 4 | // Inter-process semaphore. |
| 5 | // |
| 6 | // These are meant for IPC, not thread management. The Mutex and Condition |
| 7 | // classes are much lighter weight. |
| 8 | // |
| 9 | #ifndef __LIBS_SEMAPHORE_H |
| 10 | #define __LIBS_SEMAPHORE_H |
| 11 | |
| 12 | #ifdef HAVE_ANDROID_OS |
| 13 | #error DO NOT USE THIS FILE IN THE DEVICE BUILD |
| 14 | #endif |
| 15 | |
| 16 | namespace android { |
| 17 | |
| 18 | /* |
| 19 | * Platform-independent semaphore class. |
| 20 | * |
| 21 | * Each object holds a single semaphore. |
| 22 | * |
| 23 | * The "key" is usually the process ID of the process that created the |
| 24 | * semaphore (following POSIX semantics). See the comments in shmem.h. |
| 25 | */ |
| 26 | class Semaphore { |
| 27 | public: |
| 28 | Semaphore(void); |
| 29 | virtual ~Semaphore(void); |
| 30 | |
| 31 | /* |
| 32 | * Create a new semaphore, with the specified initial value. The |
| 33 | * value indicates the number of resources available. |
| 34 | */ |
| 35 | bool create(int key, int initialValue, bool deleteExisting); |
| 36 | |
| 37 | /* |
| 38 | * Attach to an existing semaphore. |
| 39 | */ |
| 40 | bool attach(int key); |
| 41 | |
| 42 | /* |
| 43 | * Acquire or release the semaphore. |
| 44 | */ |
| 45 | void acquire(void); |
| 46 | void release(void); |
| 47 | bool tryAcquire(void); // take a timeout? |
| 48 | |
| 49 | private: |
| 50 | bool adjust(int adj, bool wait); |
| 51 | |
| 52 | unsigned long mHandle; // semid(int) or HANDLE |
| 53 | bool mCreator; |
| 54 | int mKey; |
| 55 | }; |
| 56 | |
| 57 | }; // namespace android |
| 58 | |
| 59 | #endif // __LIBS_SEMAPHORE_H |