• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 #ifndef ANDROID_HARDWARE_BROADCASTRADIO_VTS_POINTER_UTILS
17 #define ANDROID_HARDWARE_BROADCASTRADIO_VTS_POINTER_UTILS
18 
19 #include <chrono>
20 #include <thread>
21 
22 namespace android {
23 namespace hardware {
24 namespace broadcastradio {
25 namespace vts {
26 
27 /**
28  * Clears strong pointer and waits until the object gets destroyed.
29  *
30  * @param ptr The pointer to get cleared.
31  * @param timeout Time to wait for other references.
32  */
33 template <typename T>
clearAndWait(sp<T> & ptr,std::chrono::milliseconds timeout)34 static void clearAndWait(sp<T>& ptr, std::chrono::milliseconds timeout) {
35     using std::chrono::steady_clock;
36 
37     constexpr auto step = 10ms;
38 
39     wp<T> wptr = ptr;
40     ptr.clear();
41 
42     auto limit = steady_clock::now() + timeout;
43     while (wptr.promote() != nullptr) {
44         if (steady_clock::now() + step > limit) {
45             FAIL() << "Pointer was not released within timeout";
46             break;
47         }
48         std::this_thread::sleep_for(step);
49     }
50 }
51 
52 }  // namespace vts
53 }  // namespace broadcastradio
54 }  // namespace hardware
55 }  // namespace android
56 
57 #endif  // ANDROID_HARDWARE_BROADCASTRADIO_VTS_POINTER_UTILS
58