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