 Chromium Code Reviews
 Chromium Code Reviews Issue 4721001:
  Add multi_process_lock to chrome/common  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 4721001:
  Add multi_process_lock to chrome/common  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| OLD | NEW | 
|---|---|
| (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/mac/scoped_cftyperef.h" | |
| 9 #include "base/sys_string_conversions.h" | |
| 10 | |
| 11 class MultiProcessLockMac : public MultiProcessLock { | |
| 12 public: | |
| 13 explicit MultiProcessLockMac(const std::string& name) | |
| 14 : name_(name), count_(0) { } | |
| 
Mark Mentovai
2010/11/11 23:45:24
Will this be accessed from multiple threads?
If s
 
dmac
2010/11/12 00:36:26
Good point regarding multi threads. I got rid of t
 | |
| 15 | |
| 
Mark Mentovai
2010/11/11 23:45:24
OK, I just looked at your implementation on other
 
dmac
2010/11/12 00:36:26
Done.
 | |
| 16 virtual ~MultiProcessLockMac() { | |
| 17 if (count_ > 0) { | |
| 18 Unlock(); | |
| 19 } | |
| 20 } | |
| 21 | |
| 22 virtual bool TryLock() { | |
| 23 if (count_ > 0) { | |
| 24 count_ += 1; | |
| 25 return true; | |
| 26 } | |
| 27 CFStringRef cf_name(base::SysUTF8ToCFStringRef(name_)); | |
| 28 base::mac::ScopedCFTypeRef<CFStringRef> scoped_cf_name(cf_name); | |
| 29 port_.reset(CFMessagePortCreateLocal(NULL, cf_name, NULL, NULL, NULL)); | |
| 30 if (port_ != NULL) { | |
| 31 count_ = 1; | |
| 32 return true; | |
| 33 } else { | |
| 34 return false; | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 virtual void Unlock() { | |
| 39 if (count_ == 0) { | |
| 40 DLOG(ERROR) << "Over unlocked MultiProcessLock " << name_; | |
| 
Mark Mentovai
2010/11/11 23:45:24
I’d hyphenate this dude.
 
dmac
2010/11/12 00:36:26
Done.
 | |
| 41 return; | |
| 42 } | |
| 43 count_ -= 1; | |
| 44 if (count_ == 0) { | |
| 45 port_.reset(); | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 private: | |
| 50 std::string name_; | |
| 51 int count_; | |
| 52 base::mac::ScopedCFTypeRef<CFMessagePortRef> port_; | |
| 
Mark Mentovai
2010/11/11 23:45:24
Improve the struct packing. Put port_ before count
 
dmac
2010/11/12 00:36:26
Done.
 | |
| 53 DISALLOW_COPY_AND_ASSIGN(MultiProcessLockMac); | |
| 54 }; | |
| 55 | |
| 56 MultiProcessLock* MultiProcessLock::Create(const std::string &name) { | |
| 57 return new MultiProcessLockMac(name); | |
| 58 } | |
| OLD | NEW |