14f94c520f8d699a5973956a1716272146be17128Zonr Chang/*
24f94c520f8d699a5973956a1716272146be17128Zonr Chang * Copyright 2012, The Android Open Source Project
34f94c520f8d699a5973956a1716272146be17128Zonr Chang *
44f94c520f8d699a5973956a1716272146be17128Zonr Chang * Licensed under the Apache License, Version 2.0 (the "License");
54f94c520f8d699a5973956a1716272146be17128Zonr Chang * you may not use this file except in compliance with the License.
64f94c520f8d699a5973956a1716272146be17128Zonr Chang * You may obtain a copy of the License at
74f94c520f8d699a5973956a1716272146be17128Zonr Chang *
84f94c520f8d699a5973956a1716272146be17128Zonr Chang *     http://www.apache.org/licenses/LICENSE-2.0
94f94c520f8d699a5973956a1716272146be17128Zonr Chang *
104f94c520f8d699a5973956a1716272146be17128Zonr Chang * Unless required by applicable law or agreed to in writing, software
114f94c520f8d699a5973956a1716272146be17128Zonr Chang * distributed under the License is distributed on an "AS IS" BASIS,
124f94c520f8d699a5973956a1716272146be17128Zonr Chang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134f94c520f8d699a5973956a1716272146be17128Zonr Chang * See the License for the specific language governing permissions and
144f94c520f8d699a5973956a1716272146be17128Zonr Chang * limitations under the License.
154f94c520f8d699a5973956a1716272146be17128Zonr Chang */
164f94c520f8d699a5973956a1716272146be17128Zonr Chang
17c72c4ddfcd79c74f70713da91a69569451b5c19eZonr Chang#ifndef BCC_SUPPORT_FILE_MUTEX_H
18c72c4ddfcd79c74f70713da91a69569451b5c19eZonr Chang#define BCC_SUPPORT_FILE_MUTEX_H
194f94c520f8d699a5973956a1716272146be17128Zonr Chang
204f94c520f8d699a5973956a1716272146be17128Zonr Chang#include <string>
214f94c520f8d699a5973956a1716272146be17128Zonr Chang
22c72c4ddfcd79c74f70713da91a69569451b5c19eZonr Chang#include "bcc/Support/FileBase.h"
234f94c520f8d699a5973956a1716272146be17128Zonr Chang
244f94c520f8d699a5973956a1716272146be17128Zonr Changnamespace bcc {
254f94c520f8d699a5973956a1716272146be17128Zonr Chang
264f94c520f8d699a5973956a1716272146be17128Zonr Changtemplate<enum FileBase::LockModeEnum LockMode>
274f94c520f8d699a5973956a1716272146be17128Zonr Changclass FileMutex : public FileBase {
284f94c520f8d699a5973956a1716272146be17128Zonr Changpublic:
294f94c520f8d699a5973956a1716272146be17128Zonr Chang  FileMutex(const std::string &pFileToLock)
304f94c520f8d699a5973956a1716272146be17128Zonr Chang    : FileBase(pFileToLock + ".lock", O_RDONLY | O_CREAT, 0) { }
314f94c520f8d699a5973956a1716272146be17128Zonr Chang
324f94c520f8d699a5973956a1716272146be17128Zonr Chang  // Provide a lock() interface filled with default configuration.
334f94c520f8d699a5973956a1716272146be17128Zonr Chang  inline bool lock(bool pNonblocking = true,
344f94c520f8d699a5973956a1716272146be17128Zonr Chang                   unsigned pMaxRetry = FileBase::kDefaultMaxRetryLock,
354f94c520f8d699a5973956a1716272146be17128Zonr Chang                   useconds_t pRetryInterval =
364f94c520f8d699a5973956a1716272146be17128Zonr Chang                       FileBase::kDefaultRetryLockInterval) {
374f94c520f8d699a5973956a1716272146be17128Zonr Chang    return FileBase::lock(LockMode, pNonblocking, pMaxRetry, pRetryInterval);
384f94c520f8d699a5973956a1716272146be17128Zonr Chang  }
394f94c520f8d699a5973956a1716272146be17128Zonr Chang};
404f94c520f8d699a5973956a1716272146be17128Zonr Chang
414f94c520f8d699a5973956a1716272146be17128Zonr Chang} // namespace bcc
424f94c520f8d699a5973956a1716272146be17128Zonr Chang
43c72c4ddfcd79c74f70713da91a69569451b5c19eZonr Chang#endif  // BCC_SUPPORT_FILE_MUTEX_H
44