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

Side by Side Diff: src/platform-solaris.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-openbsd.cc ('k') | src/platform-win32.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 375 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 return munmap(base, size) == 0; 386 return munmap(base, size) == 0;
387 } 387 }
388 388
389 389
390 bool VirtualMemory::HasLazyCommits() { 390 bool VirtualMemory::HasLazyCommits() {
391 // TODO(alph): implement for the platform. 391 // TODO(alph): implement for the platform.
392 return false; 392 return false;
393 } 393 }
394 394
395 395
396 class SolarisSemaphore : public Semaphore {
397 public:
398 explicit SolarisSemaphore(int count) { sem_init(&sem_, 0, count); }
399 virtual ~SolarisSemaphore() { sem_destroy(&sem_); }
400
401 virtual void Wait();
402 virtual bool Wait(int timeout);
403 virtual void Signal() { sem_post(&sem_); }
404 private:
405 sem_t sem_;
406 };
407
408
409 void SolarisSemaphore::Wait() {
410 while (true) {
411 int result = sem_wait(&sem_);
412 if (result == 0) return; // Successfully got semaphore.
413 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
414 }
415 }
416
417
418 #ifndef TIMEVAL_TO_TIMESPEC
419 #define TIMEVAL_TO_TIMESPEC(tv, ts) do { \
420 (ts)->tv_sec = (tv)->tv_sec; \
421 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
422 } while (false)
423 #endif
424
425
426 #ifndef timeradd
427 #define timeradd(a, b, result) \
428 do { \
429 (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
430 (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
431 if ((result)->tv_usec >= 1000000) { \
432 ++(result)->tv_sec; \
433 (result)->tv_usec -= 1000000; \
434 } \
435 } while (0)
436 #endif
437
438
439 bool SolarisSemaphore::Wait(int timeout) {
440 const long kOneSecondMicros = 1000000; // NOLINT
441
442 // Split timeout into second and nanosecond parts.
443 struct timeval delta;
444 delta.tv_usec = timeout % kOneSecondMicros;
445 delta.tv_sec = timeout / kOneSecondMicros;
446
447 struct timeval current_time;
448 // Get the current time.
449 if (gettimeofday(&current_time, NULL) == -1) {
450 return false;
451 }
452
453 // Calculate time for end of timeout.
454 struct timeval end_time;
455 timeradd(&current_time, &delta, &end_time);
456
457 struct timespec ts;
458 TIMEVAL_TO_TIMESPEC(&end_time, &ts);
459 // Wait for semaphore signalled or timeout.
460 while (true) {
461 int result = sem_timedwait(&sem_, &ts);
462 if (result == 0) return true; // Successfully got semaphore.
463 if (result == -1 && errno == ETIMEDOUT) return false; // Timeout.
464 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
465 }
466 }
467
468
469 Semaphore* OS::CreateSemaphore(int count) {
470 return new SolarisSemaphore(count);
471 }
472
473
474 void OS::SetUp() { 396 void OS::SetUp() {
475 // Seed the random number generator. 397 // Seed the random number generator.
476 // Convert the current time to a 64-bit integer first, before converting it 398 // Convert the current time to a 64-bit integer first, before converting it
477 // to an unsigned. Going directly will cause an overflow and the seed to be 399 // to an unsigned. Going directly will cause an overflow and the seed to be
478 // set to all ones. The seed will be identical for different instances that 400 // set to all ones. The seed will be identical for different instances that
479 // call this setup code within the same millisecond. 401 // call this setup code within the same millisecond.
480 uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis()); 402 uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis());
481 srandom(static_cast<unsigned int>(seed)); 403 srandom(static_cast<unsigned int>(seed));
482 limit_mutex = new Mutex(); 404 limit_mutex = new Mutex();
483 } 405 }
484 406
485 407
486 void OS::TearDown() { 408 void OS::TearDown() {
487 delete limit_mutex; 409 delete limit_mutex;
488 } 410 }
489 411
490 412
491 } } // namespace v8::internal 413 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/platform-openbsd.cc ('k') | src/platform-win32.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698