| 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 29 matching lines...) Expand all Loading... |
| 40 // slower. It would require factory methods for selecting the right | 40 // slower. It would require factory methods for selecting the right |
| 41 // implementation and the overhead of virtual methods for performance | 41 // implementation and the overhead of virtual methods for performance |
| 42 // sensitive like mutex locking/unlocking. | 42 // sensitive like mutex locking/unlocking. |
| 43 | 43 |
| 44 #ifndef V8_PLATFORM_H_ | 44 #ifndef V8_PLATFORM_H_ |
| 45 #define V8_PLATFORM_H_ | 45 #define V8_PLATFORM_H_ |
| 46 | 46 |
| 47 #include <cstdarg> | 47 #include <cstdarg> |
| 48 | 48 |
| 49 #include "platform/mutex.h" | 49 #include "platform/mutex.h" |
| 50 #include "platform/semaphore.h" |
| 50 #include "utils.h" | 51 #include "utils.h" |
| 51 #include "v8globals.h" | 52 #include "v8globals.h" |
| 52 | 53 |
| 53 #ifdef __sun | 54 #ifdef __sun |
| 54 # ifndef signbit | 55 # ifndef signbit |
| 55 namespace std { | 56 namespace std { |
| 56 int signbit(double x); | 57 int signbit(double x); |
| 57 } | 58 } |
| 58 # endif | 59 # endif |
| 59 #endif | 60 #endif |
| (...skipping 26 matching lines...) Expand all Loading... |
| 86 #endif // V8_CC_MSVC | 87 #endif // V8_CC_MSVC |
| 87 | 88 |
| 88 // Random is missing on both Visual Studio and MinGW. | 89 // Random is missing on both Visual Studio and MinGW. |
| 89 #if V8_CC_MSVC || V8_CC_MINGW | 90 #if V8_CC_MSVC || V8_CC_MINGW |
| 90 int random(); | 91 int random(); |
| 91 #endif // V8_CC_MSVC || V8_CC_MINGW | 92 #endif // V8_CC_MSVC || V8_CC_MINGW |
| 92 | 93 |
| 93 namespace v8 { | 94 namespace v8 { |
| 94 namespace internal { | 95 namespace internal { |
| 95 | 96 |
| 96 class Semaphore; | |
| 97 | |
| 98 double ceiling(double x); | 97 double ceiling(double x); |
| 99 double modulo(double x, double y); | 98 double modulo(double x, double y); |
| 100 | 99 |
| 101 // Custom implementation of math functions. | 100 // Custom implementation of math functions. |
| 102 double fast_sin(double input); | 101 double fast_sin(double input); |
| 103 double fast_cos(double input); | 102 double fast_cos(double input); |
| 104 double fast_tan(double input); | 103 double fast_tan(double input); |
| 105 double fast_log(double input); | 104 double fast_log(double input); |
| 106 double fast_exp(double input); | 105 double fast_exp(double input); |
| 107 double fast_sqrt(double input); | 106 double fast_sqrt(double input); |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 static const int kStackWalkError = -1; | 280 static const int kStackWalkError = -1; |
| 282 static const int kStackWalkMaxNameLen = 256; | 281 static const int kStackWalkMaxNameLen = 256; |
| 283 static const int kStackWalkMaxTextLen = 256; | 282 static const int kStackWalkMaxTextLen = 256; |
| 284 struct StackFrame { | 283 struct StackFrame { |
| 285 void* address; | 284 void* address; |
| 286 char text[kStackWalkMaxTextLen]; | 285 char text[kStackWalkMaxTextLen]; |
| 287 }; | 286 }; |
| 288 | 287 |
| 289 static int StackWalk(Vector<StackFrame> frames); | 288 static int StackWalk(Vector<StackFrame> frames); |
| 290 | 289 |
| 291 // Factory method for creating platform dependent Semaphore. | |
| 292 // Please use delete to reclaim the storage for the returned Semaphore. | |
| 293 static Semaphore* CreateSemaphore(int count); | |
| 294 | |
| 295 // Factory method for creating platform dependent Socket. | 290 // Factory method for creating platform dependent Socket. |
| 296 // Please use delete to reclaim the storage for the returned Socket. | 291 // Please use delete to reclaim the storage for the returned Socket. |
| 297 static Socket* CreateSocket(); | 292 static Socket* CreateSocket(); |
| 298 | 293 |
| 299 class MemoryMappedFile { | 294 class MemoryMappedFile { |
| 300 public: | 295 public: |
| 301 static MemoryMappedFile* open(const char* name); | 296 static MemoryMappedFile* open(const char* name); |
| 302 static MemoryMappedFile* create(const char* name, int size, void* initial); | 297 static MemoryMappedFile* create(const char* name, int size, void* initial); |
| 303 virtual ~MemoryMappedFile() { } | 298 virtual ~MemoryMappedFile() { } |
| 304 virtual void* memory() = 0; | 299 virtual void* memory() = 0; |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 // Otherwise returns false. | 498 // Otherwise returns false. |
| 504 static bool HasLazyCommits(); | 499 static bool HasLazyCommits(); |
| 505 | 500 |
| 506 private: | 501 private: |
| 507 void* address_; // Start address of the virtual memory. | 502 void* address_; // Start address of the virtual memory. |
| 508 size_t size_; // Size of the virtual memory. | 503 size_t size_; // Size of the virtual memory. |
| 509 }; | 504 }; |
| 510 | 505 |
| 511 | 506 |
| 512 // ---------------------------------------------------------------------------- | 507 // ---------------------------------------------------------------------------- |
| 513 // Semaphore | |
| 514 // | |
| 515 // A semaphore object is a synchronization object that maintains a count. The | |
| 516 // count is decremented each time a thread completes a wait for the semaphore | |
| 517 // object and incremented each time a thread signals the semaphore. When the | |
| 518 // count reaches zero, threads waiting for the semaphore blocks until the | |
| 519 // count becomes non-zero. | |
| 520 | |
| 521 class Semaphore { | |
| 522 public: | |
| 523 virtual ~Semaphore() {} | |
| 524 | |
| 525 // Suspends the calling thread until the semaphore counter is non zero | |
| 526 // and then decrements the semaphore counter. | |
| 527 virtual void Wait() = 0; | |
| 528 | |
| 529 // Suspends the calling thread until the counter is non zero or the timeout | |
| 530 // time has passed. If timeout happens the return value is false and the | |
| 531 // counter is unchanged. Otherwise the semaphore counter is decremented and | |
| 532 // true is returned. The timeout value is specified in microseconds. | |
| 533 virtual bool Wait(int timeout) = 0; | |
| 534 | |
| 535 // Increments the semaphore counter. | |
| 536 virtual void Signal() = 0; | |
| 537 }; | |
| 538 | |
| 539 template <int InitialValue> | |
| 540 struct CreateSemaphoreTrait { | |
| 541 static Semaphore* Create() { | |
| 542 return OS::CreateSemaphore(InitialValue); | |
| 543 } | |
| 544 }; | |
| 545 | |
| 546 // POD Semaphore initialized lazily (i.e. the first time Pointer() is called). | |
| 547 // Usage: | |
| 548 // // The following semaphore starts at 0. | |
| 549 // static LazySemaphore<0>::type my_semaphore = LAZY_SEMAPHORE_INITIALIZER; | |
| 550 // | |
| 551 // void my_function() { | |
| 552 // // Do something with my_semaphore.Pointer(). | |
| 553 // } | |
| 554 // | |
| 555 template <int InitialValue> | |
| 556 struct LazySemaphore { | |
| 557 typedef typename LazyDynamicInstance< | |
| 558 Semaphore, CreateSemaphoreTrait<InitialValue>, | |
| 559 ThreadSafeInitOnceTrait>::type type; | |
| 560 }; | |
| 561 | |
| 562 #define LAZY_SEMAPHORE_INITIALIZER LAZY_DYNAMIC_INSTANCE_INITIALIZER | |
| 563 | |
| 564 | |
| 565 // ---------------------------------------------------------------------------- | |
| 566 // Thread | 508 // Thread |
| 567 // | 509 // |
| 568 // Thread objects are used for creating and running threads. When the start() | 510 // Thread objects are used for creating and running threads. When the start() |
| 569 // method is called the new thread starts running the run() method in the new | 511 // method is called the new thread starts running the run() method in the new |
| 570 // thread. The Thread object should not be deallocated before the thread has | 512 // thread. The Thread object should not be deallocated before the thread has |
| 571 // terminated. | 513 // terminated. |
| 572 | 514 |
| 573 class Thread { | 515 class Thread { |
| 574 public: | 516 public: |
| 575 // Opaque data type for thread-local storage keys. | 517 // Opaque data type for thread-local storage keys. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 597 | 539 |
| 598 // Create new thread. | 540 // Create new thread. |
| 599 explicit Thread(const Options& options); | 541 explicit Thread(const Options& options); |
| 600 virtual ~Thread(); | 542 virtual ~Thread(); |
| 601 | 543 |
| 602 // Start new thread by calling the Run() method on the new thread. | 544 // Start new thread by calling the Run() method on the new thread. |
| 603 void Start(); | 545 void Start(); |
| 604 | 546 |
| 605 // Start new thread and wait until Run() method is called on the new thread. | 547 // Start new thread and wait until Run() method is called on the new thread. |
| 606 void StartSynchronously() { | 548 void StartSynchronously() { |
| 607 start_semaphore_ = OS::CreateSemaphore(0); | 549 start_semaphore_ = new Semaphore(0); |
| 608 Start(); | 550 Start(); |
| 609 start_semaphore_->Wait(); | 551 start_semaphore_->Wait(); |
| 610 delete start_semaphore_; | 552 delete start_semaphore_; |
| 611 start_semaphore_ = NULL; | 553 start_semaphore_ = NULL; |
| 612 } | 554 } |
| 613 | 555 |
| 614 // Wait until thread terminates. | 556 // Wait until thread terminates. |
| 615 void Join(); | 557 void Join(); |
| 616 | 558 |
| 617 inline const char* name() const { | 559 inline const char* name() const { |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 714 static uint16_t HToN(uint16_t value); | 656 static uint16_t HToN(uint16_t value); |
| 715 static uint16_t NToH(uint16_t value); | 657 static uint16_t NToH(uint16_t value); |
| 716 static uint32_t HToN(uint32_t value); | 658 static uint32_t HToN(uint32_t value); |
| 717 static uint32_t NToH(uint32_t value); | 659 static uint32_t NToH(uint32_t value); |
| 718 }; | 660 }; |
| 719 | 661 |
| 720 | 662 |
| 721 } } // namespace v8::internal | 663 } } // namespace v8::internal |
| 722 | 664 |
| 723 #endif // V8_PLATFORM_H_ | 665 #endif // V8_PLATFORM_H_ |
| OLD | NEW |