Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 26 | namespace art { |
| 27 | |
| 28 | class 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 | // |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 35 | // Returns true if the lock could be acquired, or false if an error occurred. |
| 36 | // It is an error if its inode changed (usually due to a new file being |
| 37 | // created at the same path) between attempts to lock it. In blocking mode, |
| 38 | // locking will be retried if the file changed. In non-blocking mode, false |
| 39 | // is returned and no attempt is made to re-acquire the lock. |
| 40 | // |
| 41 | // The file is opened with the provided flags. |
| 42 | bool Init(const char* filename, int flags, bool block, std::string* error_msg); |
| 43 | // Calls Init(filename, O_CREAT | O_RDWR, true, errror_msg) |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 44 | bool Init(const char* filename, std::string* error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 45 | // Attempt to acquire an exclusive file lock (see flock(2)) on 'file'. |
| 46 | // Returns true if the lock could be acquired or false if an error |
| 47 | // occured. |
| 48 | bool Init(File* file, std::string* error_msg); |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 49 | |
| 50 | // Returns the (locked) file associated with this instance. |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 51 | File* GetFile() const; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 52 | |
| 53 | // Returns whether a file is held. |
| 54 | bool HasFile(); |
| 55 | |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 56 | ~ScopedFlock(); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 57 | |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 58 | private: |
| 59 | std::unique_ptr<File> file_; |
| 60 | DISALLOW_COPY_AND_ASSIGN(ScopedFlock); |
| 61 | }; |
| 62 | |
| 63 | } // namespace art |
| 64 | |
| 65 | #endif // ART_RUNTIME_BASE_SCOPED_FLOCK_H_ |