| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 // Platform specific code for MacOS goes here | 28 // Platform specific code for MacOS goes here. For the POSIX comaptible parts |
| 29 // the implementation is in platform-posix.cc. |
| 29 | 30 |
| 30 #include <ucontext.h> | 31 #include <ucontext.h> |
| 31 #include <unistd.h> | 32 #include <unistd.h> |
| 32 #include <sys/mman.h> | 33 #include <sys/mman.h> |
| 33 #include <mach/mach_init.h> | 34 #include <mach/mach_init.h> |
| 34 | 35 |
| 35 #include <AvailabilityMacros.h> | 36 #include <AvailabilityMacros.h> |
| 36 | 37 |
| 37 #ifdef MAC_OS_X_VERSION_10_5 | 38 #ifdef MAC_OS_X_VERSION_10_5 |
| 38 # include <execinfo.h> // backtrace, backtrace_symbols | 39 # include <execinfo.h> // backtrace, backtrace_symbols |
| 39 #endif | 40 #endif |
| 40 | 41 |
| 41 #include <pthread.h> | 42 #include <pthread.h> |
| 42 #include <semaphore.h> | 43 #include <semaphore.h> |
| 43 #include <signal.h> | 44 #include <signal.h> |
| 44 #include <mach/semaphore.h> | 45 #include <mach/semaphore.h> |
| 45 #include <mach/task.h> | 46 #include <mach/task.h> |
| 46 #include <sys/time.h> | 47 #include <sys/time.h> |
| 47 #include <sys/resource.h> | 48 #include <sys/resource.h> |
| 48 #include <sys/socket.h> | |
| 49 #include <sys/types.h> | 49 #include <sys/types.h> |
| 50 #include <stdarg.h> | 50 #include <stdarg.h> |
| 51 #include <stdlib.h> | 51 #include <stdlib.h> |
| 52 | 52 |
| 53 #include <arpa/inet.h> | |
| 54 #include <netinet/in.h> | |
| 55 #include <netdb.h> | |
| 56 #include <errno.h> | 53 #include <errno.h> |
| 57 | 54 |
| 58 #undef MAP_TYPE | 55 #undef MAP_TYPE |
| 59 | 56 |
| 60 #include "v8.h" | 57 #include "v8.h" |
| 61 | 58 |
| 62 #include "platform.h" | 59 #include "platform.h" |
| 63 | 60 |
| 64 namespace v8 { namespace internal { | 61 namespace v8 { namespace internal { |
| 65 | 62 |
| (...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 558 ts.tv_nsec = (timeout % 1000000) * 1000; | 555 ts.tv_nsec = (timeout % 1000000) * 1000; |
| 559 return semaphore_timedwait(semaphore_, ts) != KERN_OPERATION_TIMED_OUT; | 556 return semaphore_timedwait(semaphore_, ts) != KERN_OPERATION_TIMED_OUT; |
| 560 } | 557 } |
| 561 | 558 |
| 562 | 559 |
| 563 Semaphore* OS::CreateSemaphore(int count) { | 560 Semaphore* OS::CreateSemaphore(int count) { |
| 564 return new MacOSSemaphore(count); | 561 return new MacOSSemaphore(count); |
| 565 } | 562 } |
| 566 | 563 |
| 567 | 564 |
| 568 // ---------------------------------------------------------------------------- | |
| 569 // MacOS socket support. | |
| 570 // | |
| 571 | |
| 572 class MacOSSocket : public Socket { | |
| 573 public: | |
| 574 explicit MacOSSocket() { | |
| 575 // Create the socket. | |
| 576 socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | |
| 577 } | |
| 578 explicit MacOSSocket(int socket): socket_(socket) { } | |
| 579 virtual ~MacOSSocket() { Shutdown(); } | |
| 580 | |
| 581 // Server initialization. | |
| 582 bool Bind(const int port); | |
| 583 bool Listen(int backlog) const; | |
| 584 Socket* Accept() const; | |
| 585 | |
| 586 // Client initialization. | |
| 587 bool Connect(const char* host, const char* port); | |
| 588 | |
| 589 // Shutdown socket for both read and write. | |
| 590 bool Shutdown(); | |
| 591 | |
| 592 // Data Transimission | |
| 593 int Send(const char* data, int len) const; | |
| 594 int Receive(char* data, int len) const; | |
| 595 | |
| 596 bool SetReuseAddress(bool reuse_address); | |
| 597 | |
| 598 bool IsValid() const { return socket_ != -1; } | |
| 599 | |
| 600 private: | |
| 601 int socket_; | |
| 602 }; | |
| 603 | |
| 604 | |
| 605 bool MacOSSocket::Bind(const int port) { | |
| 606 if (!IsValid()) { | |
| 607 return false; | |
| 608 } | |
| 609 | |
| 610 int on = 1; | |
| 611 int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); | |
| 612 if (status != 0) { | |
| 613 return false; | |
| 614 } | |
| 615 | |
| 616 sockaddr_in addr; | |
| 617 memset(&addr, 0, sizeof(addr)); | |
| 618 addr.sin_family = AF_INET; | |
| 619 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
| 620 addr.sin_port = htons(port); | |
| 621 status = bind(socket_, | |
| 622 reinterpret_cast<struct sockaddr *>(&addr), | |
| 623 sizeof(addr)); | |
| 624 return status == 0; | |
| 625 } | |
| 626 | |
| 627 | |
| 628 bool MacOSSocket::Listen(int backlog) const { | |
| 629 if (!IsValid()) { | |
| 630 return false; | |
| 631 } | |
| 632 | |
| 633 int status = listen(socket_, backlog); | |
| 634 return status == 0; | |
| 635 } | |
| 636 | |
| 637 | |
| 638 Socket* MacOSSocket::Accept() const { | |
| 639 if (!IsValid()) { | |
| 640 return NULL; | |
| 641 } | |
| 642 | |
| 643 int socket = accept(socket_, NULL, NULL); | |
| 644 if (socket == -1) { | |
| 645 return NULL; | |
| 646 } else { | |
| 647 return new MacOSSocket(socket); | |
| 648 } | |
| 649 } | |
| 650 | |
| 651 | |
| 652 bool MacOSSocket::Connect(const char* host, const char* port) { | |
| 653 if (!IsValid()) { | |
| 654 return false; | |
| 655 } | |
| 656 | |
| 657 // Lookup host and port. | |
| 658 struct addrinfo *result = NULL; | |
| 659 struct addrinfo hints; | |
| 660 memset(&hints, 0, sizeof(addrinfo)); | |
| 661 hints.ai_family = AF_INET; | |
| 662 hints.ai_socktype = SOCK_STREAM; | |
| 663 hints.ai_protocol = IPPROTO_TCP; | |
| 664 int status = getaddrinfo(host, port, &hints, &result); | |
| 665 if (status != 0) { | |
| 666 return false; | |
| 667 } | |
| 668 | |
| 669 // Connect. | |
| 670 status = connect(socket_, result->ai_addr, result->ai_addrlen); | |
| 671 freeaddrinfo(result); | |
| 672 return status == 0; | |
| 673 } | |
| 674 | |
| 675 | |
| 676 bool MacOSSocket::Shutdown() { | |
| 677 if (IsValid()) { | |
| 678 // Shutdown socket for both read and write. | |
| 679 int status = shutdown(socket_, SHUT_RDWR); | |
| 680 close(socket_); | |
| 681 socket_ = -1; | |
| 682 return status == 0; | |
| 683 } | |
| 684 return true; | |
| 685 } | |
| 686 | |
| 687 | |
| 688 int MacOSSocket::Send(const char* data, int len) const { | |
| 689 int status = send(socket_, data, len, 0); | |
| 690 return status; | |
| 691 } | |
| 692 | |
| 693 | |
| 694 int MacOSSocket::Receive(char* data, int len) const { | |
| 695 int status = recv(socket_, data, len, 0); | |
| 696 return status; | |
| 697 } | |
| 698 | |
| 699 | |
| 700 bool MacOSSocket::SetReuseAddress(bool reuse_address) { | |
| 701 int on = reuse_address ? 1 : 0; | |
| 702 int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); | |
| 703 return status == 0; | |
| 704 } | |
| 705 | |
| 706 | |
| 707 bool Socket::Setup() { | |
| 708 // Nothing to do on MacOS. | |
| 709 return true; | |
| 710 } | |
| 711 | |
| 712 | |
| 713 int Socket::LastError() { | |
| 714 return errno; | |
| 715 } | |
| 716 | |
| 717 | |
| 718 uint16_t Socket::HToN(uint16_t value) { | |
| 719 return htons(value); | |
| 720 } | |
| 721 | |
| 722 | |
| 723 uint16_t Socket::NToH(uint16_t value) { | |
| 724 return ntohs(value); | |
| 725 } | |
| 726 | |
| 727 | |
| 728 uint32_t Socket::HToN(uint32_t value) { | |
| 729 return htonl(value); | |
| 730 } | |
| 731 | |
| 732 | |
| 733 uint32_t Socket::NToH(uint32_t value) { | |
| 734 return ntohl(value); | |
| 735 } | |
| 736 | |
| 737 | |
| 738 Socket* OS::CreateSocket() { | |
| 739 return new MacOSSocket(); | |
| 740 } | |
| 741 | |
| 742 | |
| 743 #ifdef ENABLE_LOGGING_AND_PROFILING | 565 #ifdef ENABLE_LOGGING_AND_PROFILING |
| 744 | 566 |
| 745 static Sampler* active_sampler_ = NULL; | 567 static Sampler* active_sampler_ = NULL; |
| 746 | 568 |
| 747 static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { | 569 static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { |
| 748 USE(info); | 570 USE(info); |
| 749 if (signal != SIGPROF) return; | 571 if (signal != SIGPROF) return; |
| 750 if (active_sampler_ == NULL) return; | 572 if (active_sampler_ == NULL) return; |
| 751 | 573 |
| 752 TickSample sample; | 574 TickSample sample; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 833 } | 655 } |
| 834 | 656 |
| 835 // This sampler is no longer the active sampler. | 657 // This sampler is no longer the active sampler. |
| 836 active_sampler_ = NULL; | 658 active_sampler_ = NULL; |
| 837 active_ = false; | 659 active_ = false; |
| 838 } | 660 } |
| 839 | 661 |
| 840 #endif // ENABLE_LOGGING_AND_PROFILING | 662 #endif // ENABLE_LOGGING_AND_PROFILING |
| 841 | 663 |
| 842 } } // namespace v8::internal | 664 } } // namespace v8::internal |
| OLD | NEW |