blob: f8ed805bea4e4f85dd3dce09479f203b4125bf19 [file] [log] [blame]
Narayan Kamathd1c606f2014-06-09 16:50:19 +01001/*
2 * Copyright (C) 2011 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 ART_RUNTIME_BASE_SCOPED_FLOCK_H_
18#define ART_RUNTIME_BASE_SCOPED_FLOCK_H_
19
20#include <memory>
21#include <string>
22
23#include "base/macros.h"
24#include "os.h"
25
26namespace art {
27
28class ScopedFlock {
29 public:
30 ScopedFlock();
31
32 // Attempts to acquire an exclusive file lock (see flock(2)) on the file
33 // at filename, and blocks until it can do so.
34 //
35 // Returns true if the lock could be acquired, or false if an error
36 // occurred. It is an error if the file does not exist, or if its inode
37 // changed (usually due to a new file being created at the same path)
38 // between attempts to lock it.
39 bool Init(const char* filename, std::string* error_msg);
40
41 // Returns the (locked) file associated with this instance.
42 File* GetFile();
Andreas Gampe833a4852014-05-21 18:46:59 -070043
44 // Returns whether a file is held.
45 bool HasFile();
46
Narayan Kamathd1c606f2014-06-09 16:50:19 +010047 ~ScopedFlock();
48 private:
49 std::unique_ptr<File> file_;
50 DISALLOW_COPY_AND_ASSIGN(ScopedFlock);
51};
52
53} // namespace art
54
55#endif // ART_RUNTIME_BASE_SCOPED_FLOCK_H_