1d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath/*
2d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath * Copyright (C) 2011 The Android Open Source Project
3d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath *
4d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath * Licensed under the Apache License, Version 2.0 (the "License");
5d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath * you may not use this file except in compliance with the License.
6d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath * You may obtain a copy of the License at
7d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath *
8d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath *      http://www.apache.org/licenses/LICENSE-2.0
9d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath *
10d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath * Unless required by applicable law or agreed to in writing, software
11d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath * distributed under the License is distributed on an "AS IS" BASIS,
12d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath * See the License for the specific language governing permissions and
14d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath * limitations under the License.
15d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath */
16d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath
17d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath#ifndef ART_RUNTIME_BASE_SCOPED_FLOCK_H_
18d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath#define ART_RUNTIME_BASE_SCOPED_FLOCK_H_
19d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath
20d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath#include <memory>
21d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath#include <string>
22d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath
23d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath#include "base/macros.h"
24d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath#include "os.h"
25d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath
26d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamathnamespace art {
27d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath
28d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamathclass ScopedFlock {
29d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath public:
30d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath  ScopedFlock();
31d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath
32d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath  // Attempts to acquire an exclusive file lock (see flock(2)) on the file
33d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath  // at filename, and blocks until it can do so.
34d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath  //
35877fd963548a3175665bfef25b0d24bc0e5a0135Calin Juravle  // Returns true if the lock could be acquired, or false if an error occurred.
36877fd963548a3175665bfef25b0d24bc0e5a0135Calin Juravle  // It is an error if its inode changed (usually due to a new file being
37877fd963548a3175665bfef25b0d24bc0e5a0135Calin Juravle  // created at the same path) between attempts to lock it. In blocking mode,
38877fd963548a3175665bfef25b0d24bc0e5a0135Calin Juravle  // locking will be retried if the file changed. In non-blocking mode, false
39877fd963548a3175665bfef25b0d24bc0e5a0135Calin Juravle  // is returned and no attempt is made to re-acquire the lock.
40877fd963548a3175665bfef25b0d24bc0e5a0135Calin Juravle  //
41877fd963548a3175665bfef25b0d24bc0e5a0135Calin Juravle  // The file is opened with the provided flags.
42877fd963548a3175665bfef25b0d24bc0e5a0135Calin Juravle  bool Init(const char* filename, int flags, bool block, std::string* error_msg);
43877fd963548a3175665bfef25b0d24bc0e5a0135Calin Juravle  // Calls Init(filename, O_CREAT | O_RDWR, true, errror_msg)
44d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath  bool Init(const char* filename, std::string* error_msg);
45a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  // Attempt to acquire an exclusive file lock (see flock(2)) on 'file'.
46a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  // Returns true if the lock could be acquired or false if an error
47a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  // occured.
48a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  bool Init(File* file, std::string* error_msg);
49d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath
50d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath  // Returns the (locked) file associated with this instance.
51877fd963548a3175665bfef25b0d24bc0e5a0135Calin Juravle  File* GetFile() const;
52833a48501d560c9fa7fc78ef619888138c2d374fAndreas Gampe
53833a48501d560c9fa7fc78ef619888138c2d374fAndreas Gampe  // Returns whether a file is held.
54833a48501d560c9fa7fc78ef619888138c2d374fAndreas Gampe  bool HasFile();
55833a48501d560c9fa7fc78ef619888138c2d374fAndreas Gampe
56d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath  ~ScopedFlock();
57a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
58d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath private:
59d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath  std::unique_ptr<File> file_;
60d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath  DISALLOW_COPY_AND_ASSIGN(ScopedFlock);
61d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath};
62d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath
63d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath}  // namespace art
64d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath
65d1c606f280797be81e2592c483869a6ec836a9f3Narayan Kamath#endif  // ART_RUNTIME_BASE_SCOPED_FLOCK_H_
66