xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/xcore/xcam_mutex.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 #include <xcam_log.h>
28 
29 namespace XCam {
30 
31 class Mutex {
32     friend class Cond;
33 private:
34     XCAM_DEAD_COPY (Mutex);
35 
36 public:
_dynamic(dynamic)37     Mutex (bool dynamic = true) : _dynamic(dynamic) {
38         if (!dynamic) {
39             _mutex = PTHREAD_MUTEX_INITIALIZER;
40             return;
41         }
42         int error_num = pthread_mutex_init (&_mutex, NULL);
43         if (error_num != 0) {
44             XCAM_LOG_WARNING ("Mutex init failed %d: %s", error_num, strerror(error_num));
45         }
46     }
~Mutex()47     virtual ~Mutex () {
48         if (!_dynamic)
49             return;
50         int error_num = pthread_mutex_destroy (&_mutex);
51         if (error_num != 0) {
52             XCAM_LOG_WARNING ("Mutex destroy failed %d: %s", error_num, strerror(error_num));
53         }
54     }
55 
lock()56     void lock() {
57         int error_num = pthread_mutex_lock (&_mutex);
58         if (error_num != 0) {
59             XCAM_LOG_WARNING ("Mutex lock failed %d: %s", error_num, strerror(error_num));
60         }
61     }
trylock()62     int trylock() {
63         int error_num = pthread_mutex_trylock (&_mutex);
64         return error_num;
65     }
unlock()66     void unlock() {
67         int error_num = pthread_mutex_unlock (&_mutex);
68         if (error_num != 0) {
69             XCAM_LOG_WARNING ("Mutex unlock failed %d: %s", error_num, strerror(error_num));
70         }
71     }
72 
73 private:
74     pthread_mutex_t _mutex;
75     bool _dynamic;
76 };
77 
78 class Cond {
79 private:
80     XCAM_DEAD_COPY (Cond);
81 
82 public:
_dynamic(dynamic)83     Cond (bool dynamic = true) : _dynamic(dynamic) {
84         if (!dynamic)
85             _cond = PTHREAD_COND_INITIALIZER;
86         else
87             pthread_cond_init (&_cond, NULL);
88     }
~Cond()89     ~Cond () {
90         if (_dynamic)
91             pthread_cond_destroy (&_cond);
92     }
93 
wait(Mutex & mutex)94     int wait (Mutex &mutex) {
95         return pthread_cond_wait (&_cond, &mutex._mutex);
96     }
timedwait(Mutex & mutex,uint32_t time_in_us)97     int timedwait (Mutex &mutex, uint32_t time_in_us) {
98         struct timeval now;
99         struct timespec abstime;
100 
101         gettimeofday (&now, NULL);
102         now.tv_usec += time_in_us;
103         xcam_mem_clear (abstime);
104         abstime.tv_sec += now.tv_sec + now.tv_usec / 1000000;
105         abstime.tv_nsec = (now.tv_usec % 1000000) * 1000;
106 
107         return pthread_cond_timedwait (&_cond, &mutex._mutex, &abstime);
108     }
109 
signal()110     int signal() {
111         return pthread_cond_signal (&_cond);
112     }
broadcast()113     int broadcast() {
114         return pthread_cond_broadcast (&_cond);
115     }
116 private:
117     pthread_cond_t _cond;
118     bool _dynamic;
119 };
120 
121 class SmartLock {
122 private:
123     XCAM_DEAD_COPY (SmartLock);
124 
125 public:
SmartLock(XCam::Mutex & mutex)126     SmartLock (XCam::Mutex &mutex): _mutex(mutex) {
127         _mutex.lock();
128     }
~SmartLock()129     virtual ~SmartLock () {
130         _mutex.unlock();
131     }
132 private:
133     XCam::Mutex &_mutex;
134 };
135 }
136 #endif //XCAM_MUTEX_H
137 
138