1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17 #ifndef ANDROID_FRAMEWORKS_BUFFERHUB_V1_0_ID_GENERATOR_H 18 #define ANDROID_FRAMEWORKS_BUFFERHUB_V1_0_ID_GENERATOR_H 19 20 #include <mutex> 21 #include <set> 22 23 #include <utils/Mutex.h> 24 25 namespace android { 26 namespace frameworks { 27 namespace bufferhub { 28 namespace V1_0 { 29 namespace implementation { 30 31 // A thread-safe, non-negative, incremental, int id generator. 32 class BufferHubIdGenerator { 33 public: 34 // Get the singleton instance of this class 35 static BufferHubIdGenerator& getInstance(); 36 37 // Gets next available id. If next id is greater than std::numeric_limits<int32_t>::max(), it 38 // will try to get an id start from 0 again. 39 int getId(); 40 41 // Free a specific id. 42 void freeId(int id); 43 44 private: 45 BufferHubIdGenerator() = default; 46 ~BufferHubIdGenerator() = default; 47 48 // Start from -1 so all valid ids will be >= 0 49 int mLastId = -1; 50 51 std::mutex mIdsInUseMutex; 52 std::set<int> mIdsInUse GUARDED_BY(mIdsInUseMutex); 53 }; 54 55 } // namespace implementation 56 } // namespace V1_0 57 } // namespace bufferhub 58 } // namespace frameworks 59 } // namespace android 60 61 #endif // ANDROID_FRAMEWORKS_BUFFERHUB_V1_0_ID_GENERATOR_H 62