Chromium Code Reviews

Side by Side Diff: chrome/common/multi_process_lock_win.cc

Issue 4721001: Add multi_process_lock to chrome/common (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more clean up for mento Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2010 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.
4
5 #include "chrome/common/multi_process_lock.h"
6
7 #include "base/logging.h"
8 #include "base/utf_string_conversions.h"
9 #include "base/win/scoped_handle.h"
10
11 class MultiProcessLockWin : public MultiProcessLock {
12 public:
13 explicit MultiProcessLockWin(const std::string& name) : name_(name) { }
14
15 virtual ~MultiProcessLockWin() {
16 if (event_.Get() != NULL) {
17 Unlock();
18 }
19 }
20
21 virtual bool TryLock() {
22 if (event_.Get() != NULL) {
23 DLOG(ERROR) << "MultiProcessLock is already locked - " << name_;
24 return true;
25 }
26
27 if (name_.length() > MULTI_PROCESS_LOCK_NAME_MAX_LEN) {
28 LOG(ERROR) << "Socket name too long - " << name_;
29 return false;
30 }
31
32 string16 wname = UTF8ToUTF16(name_);
33 event_.Set(CreateEvent(NULL, FALSE, FALSE, wname.c_str()));
34 if (event_.Get() && GetLastError() != ERROR_ALREADY_EXISTS) {
35 return true;
36 } else {
37 event_.Set(NULL);
38 return false;
39 }
40 }
41
42 virtual void Unlock() {
43 if (event_.Get() == NULL) {
44 DLOG(ERROR) << "Over-unlocked MultiProcessLock - " << name_;
45 return;
46 }
47 event_.Set(NULL);
48 }
49
50 private:
51 std::string name_;
52 base::win::ScopedHandle event_;
53 DISALLOW_COPY_AND_ASSIGN(MultiProcessLockWin);
54 };
55
56 MultiProcessLock* MultiProcessLock::Create(const std::string &name) {
57 return new MultiProcessLockWin(name);
58 }
OLDNEW
« chrome/common/multi_process_lock_unittest.cc ('K') | « chrome/common/multi_process_lock_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine