1 : // Copyright 2014 Google Inc. All Rights Reserved.
2 : //
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 : // Utility functions for spending a fixed amount of time trying to acquire
16 : // a base::Lock (or any lock with the same API).
17 :
18 : #ifndef SYZYGY_AGENT_ASAN_TIMED_TRY_H_
19 : #define SYZYGY_AGENT_ASAN_TIMED_TRY_H_
20 :
21 : #include "base/time/time.h"
22 :
23 m : namespace agent {
24 m : namespace asan {
25 :
26 : // Spends at most |delta| time trying to acquire the given |lock|. The time
27 : // limit is a guideline and not precise. This function tries to grab the lock
28 : // by repeatedly calling 'LockType::Try' via LockTryFunctor.
29 : // @tparam LockType The type of the lock to be acquired.
30 : // @param delta The maximum amount of time to spend trying to acquire the lock.
31 : // @param lock The lock to be acquired.
32 : // @returns true if the lock has been acquired, false otherwise.
33 m : template<typename LockType>
34 m : bool TimedTry(base::TimeDelta delta, LockType* lock);
35 :
36 : // A scoped timed try lock.
37 : // @tparam LockType The type of the lock to be acquired.
38 m : template<typename LockType>
39 m : class AutoTimedTry {
40 m : public:
41 m : AutoTimedTry(base::TimeDelta delta, LockType* lock);
42 m : ~AutoTimedTry();
43 m : bool is_acquired() const;
44 :
45 m : private:
46 m : LockType* lock_;
47 m : bool is_acquired_;
48 :
49 m : DISALLOW_COPY_AND_ASSIGN(AutoTimedTry);
50 m : };
51 :
52 m : } // namespace asan
53 m : } // namespace agent
54 :
55 : #include "syzygy/agent/asan/timed_try_impl.h"
56 :
57 : #endif // SYZYGY_AGENT_ASAN_TIMED_TRY_H_
|