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

Side by Side Diff: src/platform-openbsd.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-macos.cc ('k') | src/platform-solaris.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 411 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 return munmap(base, size) == 0; 422 return munmap(base, size) == 0;
423 } 423 }
424 424
425 425
426 bool VirtualMemory::HasLazyCommits() { 426 bool VirtualMemory::HasLazyCommits() {
427 // TODO(alph): implement for the platform. 427 // TODO(alph): implement for the platform.
428 return false; 428 return false;
429 } 429 }
430 430
431 431
432 class OpenBSDSemaphore : public Semaphore {
433 public:
434 explicit OpenBSDSemaphore(int count) { sem_init(&sem_, 0, count); }
435 virtual ~OpenBSDSemaphore() { sem_destroy(&sem_); }
436
437 virtual void Wait();
438 virtual bool Wait(int timeout);
439 virtual void Signal() { sem_post(&sem_); }
440 private:
441 sem_t sem_;
442 };
443
444
445 void OpenBSDSemaphore::Wait() {
446 while (true) {
447 int result = sem_wait(&sem_);
448 if (result == 0) return; // Successfully got semaphore.
449 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
450 }
451 }
452
453
454 #ifndef TIMEVAL_TO_TIMESPEC
455 #define TIMEVAL_TO_TIMESPEC(tv, ts) do { \
456 (ts)->tv_sec = (tv)->tv_sec; \
457 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
458 } while (false)
459 #endif
460
461
462 bool OpenBSDSemaphore::Wait(int timeout) {
463 const long kOneSecondMicros = 1000000; // NOLINT
464
465 // Split timeout into second and nanosecond parts.
466 struct timeval delta;
467 delta.tv_usec = timeout % kOneSecondMicros;
468 delta.tv_sec = timeout / kOneSecondMicros;
469
470 struct timeval current_time;
471 // Get the current time.
472 if (gettimeofday(&current_time, NULL) == -1) {
473 return false;
474 }
475
476 // Calculate time for end of timeout.
477 struct timeval end_time;
478 timeradd(&current_time, &delta, &end_time);
479
480 struct timespec ts;
481 TIMEVAL_TO_TIMESPEC(&end_time, &ts);
482
483 int to = ts.tv_sec;
484
485 while (true) {
486 int result = sem_trywait(&sem_);
487 if (result == 0) return true; // Successfully got semaphore.
488 if (!to) return false; // Timeout.
489 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
490 usleep(ts.tv_nsec / 1000);
491 to--;
492 }
493 }
494
495
496 Semaphore* OS::CreateSemaphore(int count) {
497 return new OpenBSDSemaphore(count);
498 }
499
500
501 void OS::SetUp() { 432 void OS::SetUp() {
502 // Seed the random number generator. We preserve microsecond resolution. 433 // Seed the random number generator. We preserve microsecond resolution.
503 uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis()) ^ (getpid() << 16); 434 uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis()) ^ (getpid() << 16);
504 srandom(static_cast<unsigned int>(seed)); 435 srandom(static_cast<unsigned int>(seed));
505 limit_mutex = new Mutex(); 436 limit_mutex = new Mutex();
506 } 437 }
507 438
508 439
509 void OS::TearDown() { 440 void OS::TearDown() {
510 delete limit_mutex; 441 delete limit_mutex;
511 } 442 }
512 443
513 444
514 } } // namespace v8::internal 445 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/platform-macos.cc ('k') | src/platform-solaris.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698