1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef _TS_LIST_
17 #define _TS_LIST_
18 #include <list>
19 #include <shared_mutex>
20 
21 namespace ffrt {
22 template <typename T>
23 class TSList {
24 public:
25     TSList() = default;
26     ~TSList() = default;
27 
emplace_back(T && val)28     T* emplace_back(T&& val)
29     {
30         std::unique_lock<std::shared_mutex> lck(mtx_);
31         list_.emplace_back(std::move(val));
32         return &list_.back();
33     }
34 
push_back(T val)35     void push_back(T val)
36     {
37         std::unique_lock<std::shared_mutex> lck(mtx_);
38         list_.push_back(val);
39     }
40 
get_all()41     std::list<T>& get_all()
42     {
43         std::shared_lock<std::shared_mutex> lck(mtx_);
44         return list_;
45     }
46 
claim()47     std::list<T> claim()
48     {
49         std::unique_lock<std::shared_mutex> lck(mtx_);
50         std::list<T> copy = list_;
51         list_.clear();
52         return copy;
53     }
54 
55 private:
56     std::list<T> list_;
57     std::shared_mutex mtx_;
58 };
59 } // namespace ffrt
60 
61 #endif