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 |
(...skipping 14 matching lines...) Expand all Loading... |
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 FreeBSD goes here | 28 // Platform specific code for FreeBSD goes here |
29 | 29 |
30 #include <pthread.h> | 30 #include <pthread.h> |
31 #include <semaphore.h> | 31 #include <semaphore.h> |
32 #include <signal.h> | 32 #include <signal.h> |
33 #include <sys/time.h> | 33 #include <sys/time.h> |
34 #include <sys/resource.h> | 34 #include <sys/resource.h> |
| 35 #include <sys/socket.h> |
| 36 #include <sys/types.h> |
35 #include <sys/ucontext.h> | 37 #include <sys/ucontext.h> |
36 #include <stdlib.h> | 38 #include <stdlib.h> |
37 | 39 |
38 #include <sys/types.h> // mmap & munmap | 40 #include <sys/types.h> // mmap & munmap |
39 #include <sys/mman.h> // mmap & munmap | 41 #include <sys/mman.h> // mmap & munmap |
40 #include <sys/stat.h> // open | 42 #include <sys/stat.h> // open |
41 #include <sys/fcntl.h> // open | 43 #include <sys/fcntl.h> // open |
42 #include <unistd.h> // getpagesize | 44 #include <unistd.h> // getpagesize |
43 #include <execinfo.h> // backtrace, backtrace_symbols | 45 #include <execinfo.h> // backtrace, backtrace_symbols |
44 #include <strings.h> // index | 46 #include <strings.h> // index |
| 47 #include <arpa/inet.h> |
| 48 #include <netinet/in.h> |
| 49 #include <netdb.h> |
45 #include <errno.h> | 50 #include <errno.h> |
46 #include <stdarg.h> | 51 #include <stdarg.h> |
47 #include <limits.h> | 52 #include <limits.h> |
48 | 53 |
49 #undef MAP_TYPE | 54 #undef MAP_TYPE |
50 | 55 |
51 #include "v8.h" | 56 #include "v8.h" |
52 | 57 |
53 #include "platform.h" | 58 #include "platform.h" |
54 | 59 |
(...skipping 558 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
613 int result = sem_wait(&sem_); | 618 int result = sem_wait(&sem_); |
614 if (result == 0) return; // Successfully got semaphore. | 619 if (result == 0) return; // Successfully got semaphore. |
615 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. | 620 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. |
616 } | 621 } |
617 } | 622 } |
618 | 623 |
619 Semaphore* OS::CreateSemaphore(int count) { | 624 Semaphore* OS::CreateSemaphore(int count) { |
620 return new FreeBSDSemaphore(count); | 625 return new FreeBSDSemaphore(count); |
621 } | 626 } |
622 | 627 |
| 628 |
| 629 // ---------------------------------------------------------------------------- |
| 630 // FreeBSD socket support. |
| 631 // |
| 632 |
| 633 class FreeBSDSocket : public Socket { |
| 634 public: |
| 635 explicit FreeBSDSocket() { |
| 636 // Create the socket. |
| 637 socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 638 } |
| 639 explicit FreeBSDSocket(int socket): socket_(socket) { } |
| 640 |
| 641 |
| 642 virtual ~FreeBSDSocket() { |
| 643 if (IsValid()) { |
| 644 // Close socket. |
| 645 close(socket_); |
| 646 } |
| 647 } |
| 648 |
| 649 // Server initialization. |
| 650 bool Bind (const int port); |
| 651 bool Listen(int backlog) const; |
| 652 Socket* Accept () const; |
| 653 |
| 654 // Client initialization. |
| 655 bool Connect(const char* host, const char* port); |
| 656 |
| 657 // Data Transimission |
| 658 int Send(const char* data, int len) const; |
| 659 bool SendAll(const char* data, int len) const; |
| 660 int Receive(char* data, int len) const; |
| 661 |
| 662 bool IsValid() const { return socket_ != -1; } |
| 663 |
| 664 private: |
| 665 int socket_; |
| 666 }; |
| 667 |
| 668 |
| 669 bool FreeBSDSocket::Bind(const int port) { |
| 670 if (!IsValid()) { |
| 671 return false; |
| 672 } |
| 673 |
| 674 sockaddr_in addr; |
| 675 memset(&addr, 0, sizeof(addr)); |
| 676 addr.sin_family = AF_INET; |
| 677 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 678 addr.sin_port = htons(port); |
| 679 int status = bind(socket_, |
| 680 reinterpret_cast<struct sockaddr *>(&addr), |
| 681 sizeof (addr)); |
| 682 return status == 0; |
| 683 } |
| 684 |
| 685 |
| 686 bool FreeBSDSocket::Listen(int backlog) const { |
| 687 if (!IsValid()) { |
| 688 return false; |
| 689 } |
| 690 |
| 691 int status = listen(socket_, backlog); |
| 692 return status == 0; |
| 693 } |
| 694 |
| 695 |
| 696 Socket* FreeBSDSocket::Accept() const { |
| 697 if (!IsValid()) { |
| 698 return NULL; |
| 699 } |
| 700 |
| 701 int socket = accept(socket_, NULL, NULL); |
| 702 if (socket == -1) { |
| 703 return NULL; |
| 704 } else { |
| 705 return new FreeBSDSocket(socket); |
| 706 } |
| 707 } |
| 708 |
| 709 |
| 710 bool FreeBSDSocket::Connect(const char* host, const char* port) { |
| 711 if (!IsValid()) { |
| 712 return false; |
| 713 } |
| 714 |
| 715 // Lookup host and port. |
| 716 struct addrinfo *result = NULL; |
| 717 struct addrinfo hints; |
| 718 memset(&hints, 0, sizeof(addrinfo)); |
| 719 hints.ai_family = AF_INET; |
| 720 hints.ai_socktype = SOCK_STREAM; |
| 721 hints.ai_protocol = IPPROTO_TCP; |
| 722 int status = getaddrinfo(host, port, &hints, &result); |
| 723 if (status != 0) { |
| 724 return false; |
| 725 } |
| 726 |
| 727 // Connect. |
| 728 status = connect(socket_, result->ai_addr, result->ai_addrlen); |
| 729 return status == 0; |
| 730 } |
| 731 |
| 732 |
| 733 int FreeBSDSocket::Send(const char* data, int len) const { |
| 734 int status = send(socket_, data, len, 0); |
| 735 return status; |
| 736 } |
| 737 |
| 738 |
| 739 bool FreeBSDSocket::SendAll(const char* data, int len) const { |
| 740 int sent_len = 0; |
| 741 while (sent_len < len) { |
| 742 int status = Send(data, len); |
| 743 if (status <= 0) { |
| 744 return false; |
| 745 } |
| 746 sent_len += status; |
| 747 } |
| 748 return true; |
| 749 } |
| 750 |
| 751 |
| 752 int FreeBSDSocket::Receive(char* data, int len) const { |
| 753 int status = recv(socket_, data, len, 0); |
| 754 return status; |
| 755 } |
| 756 |
| 757 |
| 758 bool Socket::Setup() { |
| 759 // Nothing to do on FreeBSD. |
| 760 return true; |
| 761 } |
| 762 |
| 763 |
| 764 int Socket::LastError() { |
| 765 return errno; |
| 766 } |
| 767 |
| 768 |
| 769 uint16_t Socket::HToN(uint16_t value) { |
| 770 return htons(value); |
| 771 } |
| 772 |
| 773 |
| 774 uint16_t Socket::NToH(uint16_t value) { |
| 775 return ntohs(value); |
| 776 } |
| 777 |
| 778 |
| 779 uint32_t Socket::HToN(uint32_t value) { |
| 780 return htonl(value); |
| 781 } |
| 782 |
| 783 |
| 784 uint32_t Socket::NToH(uint32_t value) { |
| 785 return ntohl(value); |
| 786 } |
| 787 |
| 788 |
| 789 Socket* OS::CreateSocket() { |
| 790 return new FreeBSDSocket(); |
| 791 } |
| 792 |
| 793 |
623 #ifdef ENABLE_LOGGING_AND_PROFILING | 794 #ifdef ENABLE_LOGGING_AND_PROFILING |
624 | 795 |
625 static Sampler* active_sampler_ = NULL; | 796 static Sampler* active_sampler_ = NULL; |
626 | 797 |
627 static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { | 798 static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { |
628 USE(info); | 799 USE(info); |
629 if (signal != SIGPROF) return; | 800 if (signal != SIGPROF) return; |
630 if (active_sampler_ == NULL) return; | 801 if (active_sampler_ == NULL) return; |
631 | 802 |
632 TickSample sample; | 803 TickSample sample; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
713 } | 884 } |
714 | 885 |
715 // This sampler is no longer the active sampler. | 886 // This sampler is no longer the active sampler. |
716 active_sampler_ = NULL; | 887 active_sampler_ = NULL; |
717 active_ = false; | 888 active_ = false; |
718 } | 889 } |
719 | 890 |
720 #endif // ENABLE_LOGGING_AND_PROFILING | 891 #endif // ENABLE_LOGGING_AND_PROFILING |
721 | 892 |
722 } } // namespace v8::internal | 893 } } // namespace v8::internal |
OLD | NEW |