Chromium Code Reviews| 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 "base/shared_mutex.h" | |
| 6 | |
| 7 #include <sys/socket.h> | |
| 8 #include <sys/un.h> | |
| 9 #include <unistd.h> | |
| 10 | |
| 11 #include "base/logging.h" | |
|
agl
2010/11/08 23:57:49
include "base/eintr_wrapper.h"
dmac
2010/11/11 21:47:59
Done.
| |
| 12 | |
| 13 namespace base { | |
| 14 | |
| 15 SharedMutex::SharedMutex(const std::string& name) : fd_(-1), name_(name) { | |
| 16 } | |
|
Mark Mentovai
2010/11/09 17:43:14
Blank line before, and |} // namespace base|.
| |
| 17 | |
| 18 bool SharedMutex::TryLock() { | |
| 19 struct sockaddr_un address; | |
| 20 address.sun_family = AF_UNIX; | |
| 21 // Add 1 for the \0 that goes at the front to make it an abstract name port. | |
| 22 size_t address_length = sizeof(address.sun_family) + name_.length() + 1; | |
| 23 size_t max_length = sizeof(address.sun_path); | |
|
agl
2010/11/08 23:57:49
I think this bit gets simpler if you just do
if (n
dmac
2010/11/11 21:47:59
Done.
| |
| 24 if (snprintf(address.sun_path, max_length, "#%s", name_.c_str()) | |
|
agl
2010/11/08 23:57:49
IMPORTANT: do this:
memset(address.sun_path, 0, si
dmac
2010/11/11 21:47:59
Done.
| |
| 25 >= static_cast<int>(max_length)) { | |
|
Mark Mentovai
2010/11/09 17:43:14
I think it’s way more usual for the >= to be at th
dmac
2010/11/11 21:47:59
Done.
| |
| 26 NOTREACHED() << "Socket name to long"; | |
|
Mark Mentovai
2010/11/09 17:43:14
What, now? to?
dmac
2010/11/11 21:47:59
Done.
| |
| 27 return false; | |
| 28 } | |
| 29 // Set up the abstract name port. | |
| 30 address.sun_path[0] = 0; | |
|
agl
2010/11/08 23:57:49
drop this line
dmac
2010/11/11 21:47:59
Done.
| |
| 31 int socket_fd = socket(PF_UNIX, SOCK_STREAM, 0); | |
|
agl
2010/11/08 23:57:49
AF_UNIX, not PF_UNIX
dmac
2010/11/11 21:47:59
Done.
| |
| 32 if (socket_fd < 0) { | |
| 33 NOTREACHED() << "Couldn't create socket"; | |
|
Mark Mentovai
2010/11/09 17:43:14
For failling sysaclls, you can use [D]PLOG to be s
dmac
2010/11/11 21:47:59
Done.
| |
| 34 return false; | |
| 35 } | |
| 36 if (bind(socket_fd, | |
| 37 reinterpret_cast<sockaddr *>(&address), | |
| 38 address_length) == 0) { | |
|
agl
2010/11/08 23:57:49
sizeof(address), not address_length
dmac
2010/11/11 21:47:59
Done.
| |
| 39 fd_ = socket_fd; | |
| 40 } | |
|
agl
2010/11/08 23:57:49
All the other conditionals are failure tests, then
dmac
2010/11/11 21:47:59
Done.
| |
| 41 return fd_ != -1; | |
| 42 } | |
| 43 | |
| 44 void SharedMutex::Unlock() { | |
| 45 close(fd_); | |
|
agl
2010/11/08 23:57:49
HANDLE_EINTR(close(fd_));
Mark Mentovai
2010/11/09 17:43:14
This should be wrapped in if (fd_ != -1) to avoid
dmac
2010/11/11 21:47:59
Done.
dmac
2010/11/11 21:47:59
Done.
| |
| 46 fd_ = -1; | |
| 47 } | |
| 48 | |
| 49 } // namespace base | |
| OLD | NEW |