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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * safe_list.h - safe list template
 *
 *  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_SAFE_LIST_H
#define XCAM_SAFE_LIST_H
 
#include <base/xcam_defs.h>
#include <base/xcam_common.h>
#include <errno.h>
#include <list>
#include <xcam_mutex.h>
 
namespace XCam {
 
template<class OBj>
class SafeList {
public:
    typedef SmartPtr<OBj> ObjPtr;
    typedef std::list<ObjPtr> ObjList;
    typedef typename std::list<typename SafeList<OBj>::ObjPtr>::iterator ObjIter;
 
    SafeList ()
        : _pop_paused (false)
    {}
    ~SafeList () {
    }
 
    /*
     * timeout, -1,  wait until wakeup
     *         >=0,  wait for @timeout microsseconds
    */
    inline ObjPtr pop (int32_t timeout = -1);
    inline bool push (const ObjPtr &obj);
    inline bool erase (const ObjPtr &obj);
    inline ObjPtr front ();
    uint32_t size () {
        SmartLock lock(_mutex);
        return _obj_list.size();
    }
    bool is_empty () {
        SmartLock lock(_mutex);
        return _obj_list.empty();
    }
    void wakeup () {
        _new_obj_cond.broadcast ();
    }
    void pause_pop () {
        SmartLock lock(_mutex);
        _pop_paused = true;
        wakeup ();
    }
    void resume_pop () {
        SmartLock lock(_mutex);
        _pop_paused = false;
    }
    inline void clear ();
 
protected:
    ObjList           _obj_list;
    Mutex             _mutex;
    XCam::Cond        _new_obj_cond;
    volatile bool              _pop_paused;
};
 
 
template<class OBj>
typename SafeList<OBj>::ObjPtr
SafeList<OBj>::pop (int32_t timeout)
{
    SmartLock lock (_mutex);
    int code = 0;
 
    while (!_pop_paused && _obj_list.empty() && code == 0) {
        if (timeout < 0)
            code = _new_obj_cond.wait(_mutex);
        else
            code = _new_obj_cond.timedwait(_mutex, timeout);
    }
 
    if (_pop_paused)
        return NULL;
 
    if (_obj_list.empty()) {
        if (code == ETIMEDOUT) {
            XCAM_LOG_DEBUG ("safe list pop timeout");
        } else {
            XCAM_LOG_ERROR ("safe list pop failed, code:%d", code);
        }
        return NULL;
    }
 
    SafeList<OBj>::ObjPtr obj = *_obj_list.begin ();
    _obj_list.erase (_obj_list.begin ());
    return obj;
}
 
template<class OBj>
bool
SafeList<OBj>::push (const SafeList<OBj>::ObjPtr &obj)
{
    SmartLock lock (_mutex);
    _obj_list.push_back (obj);
    _new_obj_cond.signal ();
    return true;
}
 
template<class OBj>
bool
SafeList<OBj>::erase (const SafeList<OBj>::ObjPtr &obj)
{
    XCAM_ASSERT (obj.ptr ());
    SmartLock lock (_mutex);
    for (SafeList<OBj>::ObjIter i_obj = _obj_list.begin ();
            i_obj != _obj_list.end (); ++i_obj) {
        if ((*i_obj).ptr () == obj.ptr ()) {
            _obj_list.erase (i_obj);
            return true;
        }
    }
    return false;
}
 
template<class OBj>
typename SafeList<OBj>::ObjPtr
SafeList<OBj>::front ()
{
    SmartLock lock (_mutex);
    SafeList<OBj>::ObjIter i = _obj_list.begin ();
    if (i == _obj_list.end ())
        return NULL;
    return *i;
}
 
template<class OBj>
void SafeList<OBj>::clear ()
{
    SmartLock lock (_mutex);
    SafeList<OBj>::ObjIter i_obj = _obj_list.begin ();
    while (i_obj != _obj_list.end ()) {
        _obj_list.erase (i_obj++);
    }
}
 
};
#endif //XCAM_SAFE_LIST_H