Index: util/synchronization/semaphore_posix.cc |
diff --git a/util/misc/clock_posix.cc b/util/synchronization/semaphore_posix.cc |
similarity index 50% |
copy from util/misc/clock_posix.cc |
copy to util/synchronization/semaphore_posix.cc |
index 24171091db1d41db1c34b420eb197b8291220214..973f0a5d503df575e3f0b24f3397abc3e6a5168e 100644 |
--- a/util/misc/clock_posix.cc |
+++ b/util/synchronization/semaphore_posix.cc |
@@ -12,40 +12,46 @@ |
// See the License for the specific language governing permissions and |
// limitations under the License. |
-#include "util/misc/clock.h" |
Robert Sesek
2015/02/10 21:57:07
The find-copies here sucks.
Mark Mentovai
2015/02/10 22:01:45
Robert Sesek wrote:
|
+#include "util/synchronization/semaphore.h" |
-#include <time.h> |
+#include <errno.h> |
+ |
+#include <cmath> |
#include "base/logging.h" |
#include "base/posix/eintr_wrapper.h" |
-#include "build/build_config.h" |
- |
-namespace { |
- |
-const uint64_t kNanosecondsPerSecond = 1E9; |
- |
-} // namespace |
namespace crashpad { |
#if !defined(OS_MACOSX) |
-uint64_t ClockMonotonicNanoseconds() { |
- timespec now; |
- int rv = clock_gettime(CLOCK_MONOTONIC, &now); |
- DPCHECK(rv == 0) << "clock_gettime"; |
+Semaphore::Semaphore(int value) { |
+ PCHECK(sem_init(&semaphore_, 0, value) == 0) << "sem_init"; |
+} |
- return now.tv_sec * kNanosecondsPerSecond + now.tv_nsec; |
+Semaphore::~Semaphore() { |
+ PCHECK(sem_destroy(&semaphore_) == 0) << "sem_destroy"; |
} |
-#endif |
+void Semaphore::Wait() { |
+ PCHECK(HANDLE_EINTR(sem_wait(&semaphore_)) == 0) << "sem_wait"; |
+} |
+ |
+bool Semaphore::TimedWait(double seconds) { |
+ DCHECK_GE(seconds, 0.0); |
+ timespec timeout; |
+ timeout.tv_sec = seconds; |
+ timeout.tv_nsec = (seconds - trunc(seconds)) * 1E9; |
+ |
+ int rv = HANDLE_EINTR(sem_timedwait(&semaphore_, &timeout)); |
+ PCHECK(rv == 0 || errno == ETIMEDOUT) << "sem_timedwait"; |
+ return rv == 0; |
+} |
-void SleepNanoseconds(uint64_t nanoseconds) { |
- timespec sleep_time; |
- sleep_time.tv_sec = nanoseconds / kNanosecondsPerSecond; |
- sleep_time.tv_nsec = nanoseconds % kNanosecondsPerSecond; |
- int rv = HANDLE_EINTR(nanosleep(&sleep_time, &sleep_time)); |
- DPCHECK(rv == 0) << "nanosleep"; |
+void Semaphore::Signal() { |
+ PCHECK(sem_post(&semaphore_) == 0) << "sem_post"; |
} |
+#endif |
+ |
} // namespace crashpad |