OLD | NEW |
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 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 return munmap(base, size) == 0; | 365 return munmap(base, size) == 0; |
366 } | 366 } |
367 | 367 |
368 | 368 |
369 bool VirtualMemory::HasLazyCommits() { | 369 bool VirtualMemory::HasLazyCommits() { |
370 // TODO(alph): implement for the platform. | 370 // TODO(alph): implement for the platform. |
371 return false; | 371 return false; |
372 } | 372 } |
373 | 373 |
374 | 374 |
375 class FreeBSDSemaphore : public Semaphore { | |
376 public: | |
377 explicit FreeBSDSemaphore(int count) { sem_init(&sem_, 0, count); } | |
378 virtual ~FreeBSDSemaphore() { sem_destroy(&sem_); } | |
379 | |
380 virtual void Wait(); | |
381 virtual bool Wait(int timeout); | |
382 virtual void Signal() { sem_post(&sem_); } | |
383 private: | |
384 sem_t sem_; | |
385 }; | |
386 | |
387 | |
388 void FreeBSDSemaphore::Wait() { | |
389 while (true) { | |
390 int result = sem_wait(&sem_); | |
391 if (result == 0) return; // Successfully got semaphore. | |
392 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. | |
393 } | |
394 } | |
395 | |
396 | |
397 bool FreeBSDSemaphore::Wait(int timeout) { | |
398 const long kOneSecondMicros = 1000000; // NOLINT | |
399 | |
400 // Split timeout into second and nanosecond parts. | |
401 struct timeval delta; | |
402 delta.tv_usec = timeout % kOneSecondMicros; | |
403 delta.tv_sec = timeout / kOneSecondMicros; | |
404 | |
405 struct timeval current_time; | |
406 // Get the current time. | |
407 if (gettimeofday(¤t_time, NULL) == -1) { | |
408 return false; | |
409 } | |
410 | |
411 // Calculate time for end of timeout. | |
412 struct timeval end_time; | |
413 timeradd(¤t_time, &delta, &end_time); | |
414 | |
415 struct timespec ts; | |
416 TIMEVAL_TO_TIMESPEC(&end_time, &ts); | |
417 while (true) { | |
418 int result = sem_timedwait(&sem_, &ts); | |
419 if (result == 0) return true; // Successfully got semaphore. | |
420 if (result == -1 && errno == ETIMEDOUT) return false; // Timeout. | |
421 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. | |
422 } | |
423 } | |
424 | |
425 | |
426 Semaphore* OS::CreateSemaphore(int count) { | |
427 return new FreeBSDSemaphore(count); | |
428 } | |
429 | |
430 | |
431 void OS::SetUp() { | 375 void OS::SetUp() { |
432 // Seed the random number generator. | 376 // Seed the random number generator. |
433 // Convert the current time to a 64-bit integer first, before converting it | 377 // Convert the current time to a 64-bit integer first, before converting it |
434 // to an unsigned. Going directly can cause an overflow and the seed to be | 378 // to an unsigned. Going directly can cause an overflow and the seed to be |
435 // set to all ones. The seed will be identical for different instances that | 379 // set to all ones. The seed will be identical for different instances that |
436 // call this setup code within the same millisecond. | 380 // call this setup code within the same millisecond. |
437 uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis()); | 381 uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis()); |
438 srandom(static_cast<unsigned int>(seed)); | 382 srandom(static_cast<unsigned int>(seed)); |
439 limit_mutex = new Mutex(); | 383 limit_mutex = new Mutex(); |
440 } | 384 } |
441 | 385 |
442 | 386 |
443 void OS::TearDown() { | 387 void OS::TearDown() { |
444 delete limit_mutex; | 388 delete limit_mutex; |
445 } | 389 } |
446 | 390 |
447 | 391 |
448 } } // namespace v8::internal | 392 } } // namespace v8::internal |
OLD | NEW |