• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * xcam_mutex.h - Lock
3  *
4  *  Copyright (c) 2014 Intel Corporation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Author: Wind Yuan <feng.yuan@intel.com>
19  */
20 
21 #ifndef XCAM_MUTEX_H
22 #define XCAM_MUTEX_H
23 
24 #include <xcam_std.h>
25 #include <pthread.h>
26 #include <sys/time.h>
27 
28 namespace XCam {
29 
30 class Mutex {
31     friend class Cond;
32 private:
33     XCAM_DEAD_COPY (Mutex);
34 
35 public:
Mutex()36     Mutex () {
37         int error_num = pthread_mutex_init (&_mutex, NULL);
38         if (error_num != 0) {
39             XCAM_LOG_WARNING ("Mutex init failed %d: %s", error_num, strerror(error_num));
40         }
41     }
~Mutex()42     virtual ~Mutex () {
43         int error_num = pthread_mutex_destroy (&_mutex);
44         if (error_num != 0) {
45             XCAM_LOG_WARNING ("Mutex destroy failed %d: %s", error_num, strerror(error_num));
46         }
47     }
48 
lock()49     void lock() {
50         int error_num = pthread_mutex_lock (&_mutex);
51         if (error_num != 0) {
52             XCAM_LOG_WARNING ("Mutex lock failed %d: %s", error_num, strerror(error_num));
53         }
54     }
unlock()55     void unlock() {
56         int error_num = pthread_mutex_unlock (&_mutex);
57         if (error_num != 0) {
58             XCAM_LOG_WARNING ("Mutex unlock failed %d: %s", error_num, strerror(error_num));
59         }
60     }
61 
62 private:
63     pthread_mutex_t _mutex;
64 };
65 
66 class Cond {
67 private:
68     XCAM_DEAD_COPY (Cond);
69 
70 public:
Cond()71     Cond () {
72         pthread_cond_init (&_cond, NULL);
73     }
~Cond()74     ~Cond () {
75         pthread_cond_destroy (&_cond);
76     }
77 
wait(Mutex & mutex)78     int wait (Mutex &mutex) {
79         return pthread_cond_wait (&_cond, &mutex._mutex);
80     }
timedwait(Mutex & mutex,uint32_t time_in_us)81     int timedwait (Mutex &mutex, uint32_t time_in_us) {
82         struct timeval now;
83         struct timespec abstime;
84 
85         gettimeofday (&now, NULL);
86         now.tv_usec += time_in_us;
87         xcam_mem_clear (abstime);
88         abstime.tv_sec += now.tv_sec + now.tv_usec / 1000000;
89         abstime.tv_nsec = (now.tv_usec % 1000000) * 1000;
90 
91         return pthread_cond_timedwait (&_cond, &mutex._mutex, &abstime);
92     }
93 
signal()94     int signal() {
95         return pthread_cond_signal (&_cond);
96     }
broadcast()97     int broadcast() {
98         return pthread_cond_broadcast (&_cond);
99     }
100 private:
101     pthread_cond_t _cond;
102 };
103 
104 class SmartLock {
105 private:
106     XCAM_DEAD_COPY (SmartLock);
107 
108 public:
SmartLock(XCam::Mutex & mutex)109     SmartLock (XCam::Mutex &mutex): _mutex(mutex) {
110         _mutex.lock();
111     }
~SmartLock()112     virtual ~SmartLock () {
113         _mutex.unlock();
114     }
115 private:
116     XCam::Mutex &_mutex;
117 };
118 };
119 #endif //XCAM_MUTEX_H
120 
121