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

Side by Side Diff: src/platform-linux.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-freebsd.cc ('k') | src/platform-macos.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 574 matching lines...) Expand 10 before | Expand all | Expand 10 after
585 return new LinuxMutex(); 585 return new LinuxMutex();
586 } 586 }
587 587
588 588
589 class LinuxSemaphore : public Semaphore { 589 class LinuxSemaphore : public Semaphore {
590 public: 590 public:
591 explicit LinuxSemaphore(int count) { sem_init(&sem_, 0, count); } 591 explicit LinuxSemaphore(int count) { sem_init(&sem_, 0, count); }
592 virtual ~LinuxSemaphore() { sem_destroy(&sem_); } 592 virtual ~LinuxSemaphore() { sem_destroy(&sem_); }
593 593
594 virtual void Wait(); 594 virtual void Wait();
595 virtual bool Wait(int timeout);
595 virtual void Signal() { sem_post(&sem_); } 596 virtual void Signal() { sem_post(&sem_); }
596 private: 597 private:
597 sem_t sem_; 598 sem_t sem_;
598 }; 599 };
599 600
601
600 void LinuxSemaphore::Wait() { 602 void LinuxSemaphore::Wait() {
601 while (true) { 603 while (true) {
602 int result = sem_wait(&sem_); 604 int result = sem_wait(&sem_);
603 if (result == 0) return; // Successfully got semaphore. 605 if (result == 0) return; // Successfully got semaphore.
604 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. 606 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
605 } 607 }
606 } 608 }
607 609
610
611 bool LinuxSemaphore::Wait(int timeout) {
612 const long kOneSecondMicros = 1000000; // NOLINT
613 const long kOneSecondNanos = 1000000000; // NOLINT
614
615 // Split timeout into second and nanosecond parts.
616 long nanos = (timeout % kOneSecondMicros) * 1000; // NOLINT
617 time_t secs = timeout / kOneSecondMicros;
618
619 // Get the current realtime clock.
620 struct timespec ts;
621 if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
622 return false;
623 }
624
625 // Calculate real time for end of timeout.
626 ts.tv_nsec += nanos;
627 if (ts.tv_nsec >= kOneSecondNanos) {
628 ts.tv_nsec -= kOneSecondNanos;
629 ts.tv_nsec++;
630 }
631 ts.tv_sec += secs;
632
633 // Wait for semaphore signalled or timeout.
634 while (true) {
635 int result = sem_timedwait(&sem_, &ts);
636 if (result == 0) return true; // Successfully got semaphore.
637 if (result == -1 && errno == ETIMEDOUT) return false; // Timeout.
638 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
639 }
640 }
641
642
608 Semaphore* OS::CreateSemaphore(int count) { 643 Semaphore* OS::CreateSemaphore(int count) {
609 return new LinuxSemaphore(count); 644 return new LinuxSemaphore(count);
610 } 645 }
611 646
612 647
613 // ---------------------------------------------------------------------------- 648 // ----------------------------------------------------------------------------
614 // Linux socket support. 649 // Linux socket support.
615 // 650 //
616 651
617 class LinuxSocket : public Socket { 652 class LinuxSocket : public Socket {
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
854 } 889 }
855 890
856 // This sampler is no longer the active sampler. 891 // This sampler is no longer the active sampler.
857 active_sampler_ = NULL; 892 active_sampler_ = NULL;
858 active_ = false; 893 active_ = false;
859 } 894 }
860 895
861 #endif // ENABLE_LOGGING_AND_PROFILING 896 #endif // ENABLE_LOGGING_AND_PROFILING
862 897
863 } } // namespace v8::internal 898 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/platform-freebsd.cc ('k') | src/platform-macos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698