Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include "util/synchronization/semaphore.h" | 15 #include "util/file/file_io.h" |
| 16 | 16 |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/posix/eintr_wrapper.h" | 18 #include "base/numerics/safe_conversions.h" |
| 19 | 19 |
| 20 namespace crashpad { | 20 namespace crashpad { |
| 21 | 21 |
| 22 #if defined(OS_MACOSX) | 22 // TODO(scottmg): Handle > DWORD sized writes if necessary. |
| 23 | 23 |
| 24 Semaphore::Semaphore(int value) | 24 ssize_t ReadFile(HANDLE file, void* buffer, size_t size) { |
|
Mark Mentovai
2014/12/16 22:40:25
FileHandle, not HANDLE. Same on lines 33 and 44.
scottmg
2014/12/17 00:22:58
Done.
| |
| 25 : semaphore_(dispatch_semaphore_create(value)) { | 25 DWORD size_dword = base::checked_cast<DWORD>(size); |
| 26 CHECK(semaphore_) << "dispatch_semaphore_create"; | 26 DWORD bytes_read; |
| 27 BOOL rv = ::ReadFile(file, buffer, size_dword, &bytes_read, NULL); | |
|
Mark Mentovai
2014/12/16 22:40:25
Crashpad is 100% nullptr now. Same on line 38.
Mark Mentovai
2014/12/16 22:40:25
Do ReadFile() and WriteFile() guarantee not to do
scottmg
2014/12/17 00:22:58
Done.
scottmg
2014/12/17 00:22:58
I think you're right. From my reading of the docs,
| |
| 28 if (!rv) | |
| 29 return -1; | |
| 30 return bytes_read; | |
| 27 } | 31 } |
| 28 | 32 |
| 29 Semaphore::~Semaphore() { | 33 ssize_t WriteFile(HANDLE file, const void* buffer, size_t size) { |
| 30 dispatch_release(semaphore_); | 34 // TODO(scottmg): This might need to handle the limit for pipes across a |
| 35 // network in the future. | |
| 36 DWORD size_dword = base::checked_cast<DWORD>(size); | |
| 37 DWORD bytes_written; | |
| 38 BOOL rv = ::WriteFile(file, buffer, size_dword, &bytes_written, NULL); | |
| 39 if (!rv) | |
| 40 return -1; | |
| 41 return bytes_written; | |
| 31 } | 42 } |
| 32 | 43 |
| 33 void Semaphore::Wait() { | 44 bool LoggingCloseFile(HANDLE file) { |
| 34 CHECK_EQ(dispatch_semaphore_wait(semaphore_, DISPATCH_TIME_FOREVER), 0); | 45 BOOL rv = ::CloseHandle(file); |
|
Mark Mentovai
2014/12/16 22:40:25
The :: aren’t necessary on this one.
scottmg
2014/12/17 00:22:58
Done.
Habit to imply "this is a Win32 function",
| |
| 46 PLOG_IF(ERROR, !rv) << "CloseHandle"; | |
| 47 return rv; | |
| 35 } | 48 } |
| 36 | 49 |
| 37 void Semaphore::Signal() { | |
| 38 dispatch_semaphore_signal(semaphore_); | |
| 39 } | |
| 40 | |
| 41 #else | |
| 42 | |
| 43 Semaphore::Semaphore(int value) { | |
| 44 PCHECK(sem_init(&semaphore_, 0, value) == 0) << "sem_init"; | |
| 45 } | |
| 46 | |
| 47 Semaphore::~Semaphore() { | |
| 48 PCHECK(sem_destroy(&semaphore_)) << "sem_destroy"; | |
| 49 } | |
| 50 | |
| 51 void Semaphore::Wait() { | |
| 52 PCHECK(HANDLE_EINTR(sem_wait(&semaphore_))) << "sem_wait"; | |
| 53 } | |
| 54 | |
| 55 void Semaphore::Signal() { | |
| 56 PCHECK(sem_post(&semaphore_)) << "sem_post"; | |
| 57 } | |
| 58 | |
| 59 #endif | |
| 60 | |
| 61 } // namespace crashpad | 50 } // namespace crashpad |
| OLD | NEW |