Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(157)

Side by Side Diff: src/platform-freebsd.cc

Issue 48123: Added a wait with timeout to the platform semaphore class (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 11 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/platform.h ('k') | src/platform-linux.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 590 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 return new FreeBSDMutex(); 601 return new FreeBSDMutex();
602 } 602 }
603 603
604 604
605 class FreeBSDSemaphore : public Semaphore { 605 class FreeBSDSemaphore : public Semaphore {
606 public: 606 public:
607 explicit FreeBSDSemaphore(int count) { sem_init(&sem_, 0, count); } 607 explicit FreeBSDSemaphore(int count) { sem_init(&sem_, 0, count); }
608 virtual ~FreeBSDSemaphore() { sem_destroy(&sem_); } 608 virtual ~FreeBSDSemaphore() { sem_destroy(&sem_); }
609 609
610 virtual void Wait(); 610 virtual void Wait();
611 virtual bool Wait(int timeout);
611 virtual void Signal() { sem_post(&sem_); } 612 virtual void Signal() { sem_post(&sem_); }
612 private: 613 private:
613 sem_t sem_; 614 sem_t sem_;
614 }; 615 };
615 616
617
616 void FreeBSDSemaphore::Wait() { 618 void FreeBSDSemaphore::Wait() {
617 while (true) { 619 while (true) {
618 int result = sem_wait(&sem_); 620 int result = sem_wait(&sem_);
619 if (result == 0) return; // Successfully got semaphore. 621 if (result == 0) return; // Successfully got semaphore.
620 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. 622 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
621 } 623 }
622 } 624 }
623 625
626
627 bool FreeBSDSemaphore::Wait(int timeout) {
628 const long kOneSecondMicros = 1000000; // NOLINT
629 const long kOneSecondNanos = 1000000000; // NOLINT
630
631 // Split timeout into second and nanosecond parts.
632 long nanos = (timeout % kOneSecondMicros) * 1000; // NOLINT
633 time_t secs = timeout / kOneSecondMicros;
634
635 // Get the current real time clock.
636 struct timespec ts;
637 if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
638 return false;
639 }
640
641 // Calculate realtime for end of timeout.
642 ts.tv_nsec += nanos;
643 if (ts.tv_nsec >= kOneSecondNanos) {
644 ts.tv_nsec -= kOneSecondNanos;
645 ts.tv_nsec++;
646 }
647 ts.tv_sec += secs;
648
649 // Wait for semaphore signalled or timeout.
650 while (true) {
651 int result = sem_timedwait(&sem_, &ts);
652 if (result == 0) return true; // Successfully got semaphore.
653 if (result == -1 && errno == ETIMEDOUT) return false; // Timeout.
654 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
655 }
656 }
657
658
624 Semaphore* OS::CreateSemaphore(int count) { 659 Semaphore* OS::CreateSemaphore(int count) {
625 return new FreeBSDSemaphore(count); 660 return new FreeBSDSemaphore(count);
626 } 661 }
627 662
628 663
629 // ---------------------------------------------------------------------------- 664 // ----------------------------------------------------------------------------
630 // FreeBSD socket support. 665 // FreeBSD socket support.
631 // 666 //
632 667
633 class FreeBSDSocket : public Socket { 668 class FreeBSDSocket : public Socket {
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
870 } 905 }
871 906
872 // This sampler is no longer the active sampler. 907 // This sampler is no longer the active sampler.
873 active_sampler_ = NULL; 908 active_sampler_ = NULL;
874 active_ = false; 909 active_ = false;
875 } 910 }
876 911
877 #endif // ENABLE_LOGGING_AND_PROFILING 912 #endif // ENABLE_LOGGING_AND_PROFILING
878 913
879 } } // namespace v8::internal 914 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/platform.h ('k') | src/platform-linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698