Dileep Marchya | f9ba485 | 2014-10-24 19:56:57 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2014, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without modification, are permitted |
| 5 | * provided that the following conditions are met: |
| 6 | * * Redistributions of source code must retain the above copyright notice, this list of |
| 7 | * conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above copyright notice, this list of |
| 9 | * conditions and the following disclaimer in the documentation and/or other materials provided |
| 10 | * with the distribution. |
| 11 | * * Neither the name of The Linux Foundation nor the names of its contributors may be used to |
| 12 | * endorse or promote products derived from this software without specific prior written |
| 13 | * permission. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 16 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 17 | * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 19 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 20 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 21 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 22 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | */ |
| 24 | |
| 25 | #ifndef __LOCKER_H__ |
| 26 | #define __LOCKER_H__ |
| 27 | |
| 28 | #include <stdint.h> |
| 29 | #include <pthread.h> |
| 30 | |
| 31 | #define SCOPE_LOCK(locker) Locker::ScopeLock scopeLock(locker) |
| 32 | |
| 33 | namespace sde { |
| 34 | |
| 35 | class Locker { |
| 36 | public: |
| 37 | class ScopeLock { |
| 38 | public: |
| 39 | explicit ScopeLock(Locker& locker) : locker_(locker) { |
| 40 | locker_.Lock(); |
| 41 | } |
| 42 | |
| 43 | ~ScopeLock() { |
| 44 | locker_.Unlock(); |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | Locker &locker_; |
| 49 | }; |
| 50 | |
| 51 | Locker() { |
| 52 | pthread_mutex_init(&mutex_, 0); |
| 53 | pthread_cond_init(&condition_, 0); |
| 54 | } |
| 55 | |
| 56 | ~Locker() { |
| 57 | pthread_mutex_destroy(&mutex_); |
| 58 | pthread_cond_destroy(&condition_); |
| 59 | } |
| 60 | |
| 61 | void Lock() { pthread_mutex_lock(&mutex_); } |
| 62 | void Unlock() { pthread_mutex_unlock(&mutex_); } |
| 63 | void Signal() { pthread_cond_signal(&condition_); } |
| 64 | void Broadcast() { pthread_cond_broadcast(&condition_); } |
| 65 | void Wait() { pthread_cond_wait(&condition_, &mutex_); } |
| 66 | int WaitFinite(long int ms) { |
| 67 | struct timespec ts; |
| 68 | struct timeval tv; |
| 69 | gettimeofday(&tv, NULL); |
| 70 | ts.tv_sec = tv.tv_sec + ms/1000; |
| 71 | ts.tv_nsec = tv.tv_usec*1000 + (ms%1000)*1000000; |
| 72 | ts.tv_sec += ts.tv_nsec/1000000000L; |
| 73 | ts.tv_nsec += ts.tv_nsec%1000000000L; |
| 74 | return pthread_cond_timedwait(&condition_, &mutex_, &ts); |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | pthread_mutex_t mutex_; |
| 79 | pthread_cond_t condition_; |
| 80 | }; |
| 81 | |
| 82 | } // namespace sde |
| 83 | |
| 84 | #endif // __LOCKER_H__ |
| 85 | |