2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef CACHE_FLAG_HH_
18 #define CACHE_FLAG_HH_
21 #include <shared_mutex>
23 namespace pkgmgr_server {
26 #define EXPORT_API __attribute__((visibility("default")))
29 class EXPORT_API CacheFlag {
31 enum class EXPORT_API Status {
37 static bool SetPreparing() {
39 std::unique_lock<std::mutex> u(status_lock_);
40 if (status_ == Status::UNPREPARED) {
41 status_ = Status::PREPARING;
47 static void SetStatus(Status status) {
48 std::unique_lock<std::mutex> u(status_lock_);
52 static Status GetStatus() {
53 std::unique_lock<std::mutex> u(status_lock_);
57 static std::unique_lock<std::shared_timed_mutex> GetWriterLock() {
58 return std::unique_lock<std::shared_timed_mutex>(lock_);
61 static void WriterUnLock() {
65 static std::shared_lock<std::shared_timed_mutex> GetReaderLock() {
66 return std::shared_lock<std::shared_timed_mutex>(lock_, std::defer_lock);
69 static void ReaderUnLock() {
70 lock_.unlock_shared();
74 static inline Status status_;
75 static inline std::mutex status_lock_;
76 static inline std::shared_timed_mutex lock_;
79 } // namespace pkgmgr_server
81 #endif // CACHE_FLAG_HH_