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

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

Issue 23748003: Cleanup Semaphore class. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Build fix for Mac OS X. Created 7 years, 3 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 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 479 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 bool VirtualMemory::ReleaseRegion(void* base, size_t size) { 490 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
491 return munmap(base, size) == 0; 491 return munmap(base, size) == 0;
492 } 492 }
493 493
494 494
495 bool VirtualMemory::HasLazyCommits() { 495 bool VirtualMemory::HasLazyCommits() {
496 return true; 496 return true;
497 } 497 }
498 498
499 499
500 class LinuxSemaphore : public Semaphore {
501 public:
502 explicit LinuxSemaphore(int count) { sem_init(&sem_, 0, count); }
503 virtual ~LinuxSemaphore() { sem_destroy(&sem_); }
504
505 virtual void Wait();
506 virtual bool Wait(int timeout);
507 virtual void Signal() { sem_post(&sem_); }
508 private:
509 sem_t sem_;
510 };
511
512
513 void LinuxSemaphore::Wait() {
514 while (true) {
515 int result = sem_wait(&sem_);
516 if (result == 0) return; // Successfully got semaphore.
517 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
518 }
519 }
520
521
522 #ifndef TIMEVAL_TO_TIMESPEC
523 #define TIMEVAL_TO_TIMESPEC(tv, ts) do { \
524 (ts)->tv_sec = (tv)->tv_sec; \
525 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
526 } while (false)
527 #endif
528
529
530 bool LinuxSemaphore::Wait(int timeout) {
531 const long kOneSecondMicros = 1000000; // NOLINT
532
533 // Split timeout into second and nanosecond parts.
534 struct timeval delta;
535 delta.tv_usec = timeout % kOneSecondMicros;
536 delta.tv_sec = timeout / kOneSecondMicros;
537
538 struct timeval current_time;
539 // Get the current time.
540 if (gettimeofday(&current_time, NULL) == -1) {
541 return false;
542 }
543
544 // Calculate time for end of timeout.
545 struct timeval end_time;
546 timeradd(&current_time, &delta, &end_time);
547
548 struct timespec ts;
549 TIMEVAL_TO_TIMESPEC(&end_time, &ts);
550 // Wait for semaphore signalled or timeout.
551 while (true) {
552 int result = sem_timedwait(&sem_, &ts);
553 if (result == 0) return true; // Successfully got semaphore.
554 if (result > 0) {
555 // For glibc prior to 2.3.4 sem_timedwait returns the error instead of -1.
556 errno = result;
557 result = -1;
558 }
559 if (result == -1 && errno == ETIMEDOUT) return false; // Timeout.
560 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
561 }
562 }
563
564
565 Semaphore* OS::CreateSemaphore(int count) {
566 return new LinuxSemaphore(count);
567 }
568
569
570 void OS::SetUp() { 500 void OS::SetUp() {
571 // Seed the random number generator. We preserve microsecond resolution. 501 // Seed the random number generator. We preserve microsecond resolution.
572 uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis()) ^ (getpid() << 16); 502 uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis()) ^ (getpid() << 16);
573 srandom(static_cast<unsigned int>(seed)); 503 srandom(static_cast<unsigned int>(seed));
574 limit_mutex = new Mutex(); 504 limit_mutex = new Mutex();
575 } 505 }
576 506
577 507
578 void OS::TearDown() { 508 void OS::TearDown() {
579 delete limit_mutex; 509 delete limit_mutex;
580 } 510 }
581 511
582 512
583 } } // namespace v8::internal 513 } } // 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