1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/common/multi_process_lock.h"
7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/win/scoped_handle.h"
11 class MultiProcessLockWin
: public MultiProcessLock
{
13 explicit MultiProcessLockWin(const std::string
& name
) : name_(name
) { }
15 virtual ~MultiProcessLockWin() {
16 if (event_
.Get() != NULL
) {
21 virtual bool TryLock() {
22 if (event_
.Get() != NULL
) {
23 DLOG(ERROR
) << "MultiProcessLock is already locked - " << name_
;
27 if (name_
.length() >= MAX_PATH
) {
28 LOG(ERROR
) << "Socket name too long (" << name_
.length()
29 << " >= " << MAX_PATH
<< ") - " << name_
;
33 base::string16 wname
= UTF8ToUTF16(name_
);
34 event_
.Set(CreateEvent(NULL
, FALSE
, FALSE
, wname
.c_str()));
35 if (event_
.Get() && GetLastError() != ERROR_ALREADY_EXISTS
) {
43 virtual void Unlock() {
44 if (event_
.Get() == NULL
) {
45 DLOG(ERROR
) << "Over-unlocked MultiProcessLock - " << name_
;
53 base::win::ScopedHandle event_
;
54 DISALLOW_COPY_AND_ASSIGN(MultiProcessLockWin
);
57 MultiProcessLock
* MultiProcessLock::Create(const std::string
&name
) {
58 return new MultiProcessLockWin(name
);