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

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

Powered by Google App Engine
This is Rietveld 408576698