1 /*
2 * safe_list.h - safe list template
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_SAFE_LIST_EX_H
22 #define XCAM_SAFE_LIST_EX_H
23
24 #include <base/xcam_defs.h>
25 #include <base/xcam_common.h>
26 #include <errno.h>
27 #include <list>
28 #include <xcam_mutex.h>
29
30 namespace XCam {
31
32 template<class OBj>
33 class SafeListEx {
34 public:
35 typedef OBj ObjPtr;
36 typedef std::list<ObjPtr> ObjList;
37 typedef typename std::list<typename SafeListEx<OBj>::ObjPtr>::iterator ObjIter;
38
SafeListEx()39 SafeListEx ()
40 : _pop_paused (false)
41 {}
~SafeListEx()42 ~SafeListEx () {
43 }
44
45 /*
46 * timeout, -1, wait until wakeup
47 * >=0, wait for @timeout microsseconds
48 */
49 //inline ObjPtr pop (int32_t timeout = -1);
50 inline XCamReturn pop (ObjPtr& obj_out, int32_t timeout = -1);
51 inline bool push (const ObjPtr &obj);
52 inline bool erase (const ObjPtr &obj);
53 inline ObjPtr front ();
size()54 uint32_t size () {
55 SmartLock lock(_mutex);
56 return _obj_list.size();
57 }
is_empty()58 bool is_empty () {
59 SmartLock lock(_mutex);
60 return _obj_list.empty();
61 }
wakeup()62 void wakeup () {
63 _new_obj_cond.broadcast ();
64 }
pause_pop()65 void pause_pop () {
66 SmartLock lock(_mutex);
67 _pop_paused = true;
68 wakeup ();
69 }
resume_pop()70 void resume_pop () {
71 SmartLock lock(_mutex);
72 _pop_paused = false;
73 }
74 inline void clear ();
75
76 protected:
77 ObjList _obj_list;
78 Mutex _mutex;
79 XCam::Cond _new_obj_cond;
80 volatile bool _pop_paused;
81 };
82
83
84 template<class OBj>
85 XCamReturn
pop(ObjPtr & obj_out,int32_t timeout)86 SafeListEx<OBj>::pop (ObjPtr& obj_out, int32_t timeout)
87 {
88 SmartLock lock (_mutex);
89 int code = 0;
90
91 while (!_pop_paused && _obj_list.empty() && code == 0) {
92 if (timeout < 0)
93 code = _new_obj_cond.wait(_mutex);
94 else
95 code = _new_obj_cond.timedwait(_mutex, timeout);
96 }
97
98 if (_pop_paused)
99 return XCAM_RETURN_ERROR_FAILED;
100
101 if (_obj_list.empty()) {
102 if (code == ETIMEDOUT) {
103 XCAM_LOG_DEBUG ("safe list pop timeout");
104 } else {
105 XCAM_LOG_ERROR ("safe list pop failed, code:%d", code);
106 }
107 return XCAM_RETURN_ERROR_FAILED;
108 }
109
110 obj_out = *_obj_list.begin ();
111 _obj_list.erase (_obj_list.begin ());
112 return XCAM_RETURN_NO_ERROR;
113 }
114
115 template<class OBj>
116 bool
push(const SafeListEx<OBj>::ObjPtr & obj)117 SafeListEx<OBj>::push (const SafeListEx<OBj>::ObjPtr &obj)
118 {
119 SmartLock lock (_mutex);
120 _obj_list.push_back (obj);
121 _new_obj_cond.signal ();
122 return true;
123 }
124
125 template<class OBj>
126 bool
erase(const SafeListEx<OBj>::ObjPtr & obj)127 SafeListEx<OBj>::erase (const SafeListEx<OBj>::ObjPtr &obj)
128 {
129 LOGE("not suport erase now");
130 return false;
131 }
132
133 template<class OBj>
134 typename SafeListEx<OBj>::ObjPtr
front()135 SafeListEx<OBj>::front ()
136 {
137 SmartLock lock (_mutex);
138 SafeListEx<OBj>::ObjIter i = _obj_list.begin ();
139 if (i == _obj_list.end ())
140 return NULL;
141 return *i;
142 }
143
144 template<class OBj>
clear()145 void SafeListEx<OBj>::clear ()
146 {
147 SmartLock lock (_mutex);
148 _obj_list.clear();
149 }
150
151 }
152 #endif //XCAM_SAFE_LIST_EX_H
153