blob: 2d302172ed484f096be2d9b0c01a8d2a9a4292c4 [file] [log] [blame]
Eric Laurent17a58b22016-08-17 13:53:12 +02001/*
2 * Copyright (C) 2016 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
18#ifndef LOCK_WATCH_H
19#define LOCK_WATCH_H
20
21#include <utils/String8.h>
22#include <utils/Thread.h>
23
24namespace android {
25
26// periodically checks if a mutex can be acquired and kill process otherwise
27class LockWatch : public Thread {
28
29public:
30 static const uint32_t DEFAULT_PERIOD_MS = 10000; // 10 seconds default check period
31 static const uint32_t DEFAULT_TIMEOUT_MS = 3000; // 3 seconds default lock timeout
32
33 LockWatch(Mutex& lock, const String8& tag = String8(""),
34 uint32_t periodMs = DEFAULT_PERIOD_MS, uint32_t timeoutMs = DEFAULT_TIMEOUT_MS)
35 : Thread(false /*canCallJava*/),
36 mLock(lock), mTag(tag), mPeriodMs(periodMs), mTimeOutMs(timeoutMs) {}
37
38 virtual ~LockWatch() { }
39
40 // RefBase
41 virtual void onFirstRef();
42
43private:
44 // Thread
45 virtual bool threadLoop();
46
47 Mutex& mLock; // monitored mutex
48 String8 mTag; // tag
49 uint32_t mPeriodMs; // check period in milliseconds
50 uint32_t mTimeOutMs; // mutex lock timeout in milliseconds
51};
52
53} // namespace android
54
55#endif // LOCK_WATCH_H