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

Side by Side Diff: src/platform-linux.cc

Issue 51001: Add a new "pseudo" platform for POSIX (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 11 years, 9 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-freebsd.cc ('k') | src/platform-macos.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 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 Linux goes here 28 // Platform specific code for Linux goes here. For the POSIX comaptible parts
29 // the implementation is in platform-posix.cc.
29 30
30 #include <pthread.h> 31 #include <pthread.h>
31 #include <semaphore.h> 32 #include <semaphore.h>
32 #include <signal.h> 33 #include <signal.h>
33 #include <sys/time.h> 34 #include <sys/time.h>
34 #include <sys/resource.h> 35 #include <sys/resource.h>
35 #include <sys/socket.h>
36 #include <sys/types.h> 36 #include <sys/types.h>
37 #include <stdlib.h> 37 #include <stdlib.h>
38 38
39 // Ubuntu Dapper requires memory pages to be marked as 39 // Ubuntu Dapper requires memory pages to be marked as
40 // executable. Otherwise, OS raises an exception when executing code 40 // executable. Otherwise, OS raises an exception when executing code
41 // in that page. 41 // in that page.
42 #include <sys/types.h> // mmap & munmap 42 #include <sys/types.h> // mmap & munmap
43 #include <sys/mman.h> // mmap & munmap 43 #include <sys/mman.h> // mmap & munmap
44 #include <sys/stat.h> // open 44 #include <sys/stat.h> // open
45 #include <fcntl.h> // open 45 #include <fcntl.h> // open
46 #include <unistd.h> // sysconf 46 #include <unistd.h> // sysconf
47 #ifdef __GLIBC__ 47 #ifdef __GLIBC__
48 #include <execinfo.h> // backtrace, backtrace_symbols 48 #include <execinfo.h> // backtrace, backtrace_symbols
49 #endif // def __GLIBC__ 49 #endif // def __GLIBC__
50 #include <strings.h> // index 50 #include <strings.h> // index
51 #include <errno.h> 51 #include <errno.h>
52 #include <stdarg.h> 52 #include <stdarg.h>
53 53
54 #include <arpa/inet.h>
55 #include <netinet/in.h>
56 #include <netdb.h>
57
58 #undef MAP_TYPE 54 #undef MAP_TYPE
59 55
60 #include "v8.h" 56 #include "v8.h"
61 57
62 #include "platform.h" 58 #include "platform.h"
63 59
64 60
65 namespace v8 { namespace internal { 61 namespace v8 { namespace internal {
66 62
67 // 0 is never a valid thread id on Linux since tids and pids share a 63 // 0 is never a valid thread id on Linux since tids and pids share a
(...skipping 572 matching lines...) Expand 10 before | Expand all | Expand 10 after
640 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. 636 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
641 } 637 }
642 } 638 }
643 639
644 640
645 Semaphore* OS::CreateSemaphore(int count) { 641 Semaphore* OS::CreateSemaphore(int count) {
646 return new LinuxSemaphore(count); 642 return new LinuxSemaphore(count);
647 } 643 }
648 644
649 645
650 // ----------------------------------------------------------------------------
651 // Linux socket support.
652 //
653
654 class LinuxSocket : public Socket {
655 public:
656 explicit LinuxSocket() {
657 // Create the socket.
658 socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
659 }
660 explicit LinuxSocket(int socket): socket_(socket) { }
661 virtual ~LinuxSocket() { Shutdown(); }
662
663 // Server initialization.
664 bool Bind(const int port);
665 bool Listen(int backlog) const;
666 Socket* Accept() const;
667
668 // Client initialization.
669 bool Connect(const char* host, const char* port);
670
671 // Shutdown socket for both read and write.
672 bool Shutdown();
673
674 // Data Transimission
675 int Send(const char* data, int len) const;
676 int Receive(char* data, int len) const;
677
678 bool SetReuseAddress(bool reuse_address);
679
680 bool IsValid() const { return socket_ != -1; }
681
682 private:
683 int socket_;
684 };
685
686
687 bool LinuxSocket::Bind(const int port) {
688 if (!IsValid()) {
689 return false;
690 }
691
692 sockaddr_in addr;
693 memset(&addr, 0, sizeof(addr));
694 addr.sin_family = AF_INET;
695 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
696 addr.sin_port = htons(port);
697 int status = bind(socket_,
698 reinterpret_cast<struct sockaddr *>(&addr),
699 sizeof(addr));
700 return status == 0;
701 }
702
703
704 bool LinuxSocket::Listen(int backlog) const {
705 if (!IsValid()) {
706 return false;
707 }
708
709 int status = listen(socket_, backlog);
710 return status == 0;
711 }
712
713
714 Socket* LinuxSocket::Accept() const {
715 if (!IsValid()) {
716 return NULL;
717 }
718
719 int socket = accept(socket_, NULL, NULL);
720 if (socket == -1) {
721 return NULL;
722 } else {
723 return new LinuxSocket(socket);
724 }
725 }
726
727
728 bool LinuxSocket::Connect(const char* host, const char* port) {
729 if (!IsValid()) {
730 return false;
731 }
732
733 // Lookup host and port.
734 struct addrinfo *result = NULL;
735 struct addrinfo hints;
736 memset(&hints, 0, sizeof(addrinfo));
737 hints.ai_family = AF_INET;
738 hints.ai_socktype = SOCK_STREAM;
739 hints.ai_protocol = IPPROTO_TCP;
740 int status = getaddrinfo(host, port, &hints, &result);
741 if (status != 0) {
742 return false;
743 }
744
745 // Connect.
746 status = connect(socket_, result->ai_addr, result->ai_addrlen);
747 freeaddrinfo(result);
748 return status == 0;
749 }
750
751
752 bool LinuxSocket::Shutdown() {
753 if (IsValid()) {
754 // Shutdown socket for both read and write.
755 int status = shutdown(socket_, SHUT_RDWR);
756 close(socket_);
757 socket_ = -1;
758 return status == 0;
759 }
760 return true;
761 }
762
763
764 int LinuxSocket::Send(const char* data, int len) const {
765 int status = send(socket_, data, len, 0);
766 return status;
767 }
768
769
770 int LinuxSocket::Receive(char* data, int len) const {
771 int status = recv(socket_, data, len, 0);
772 return status;
773 }
774
775
776 bool LinuxSocket::SetReuseAddress(bool reuse_address) {
777 int on = reuse_address ? 1 : 0;
778 int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
779 return status == 0;
780 }
781
782
783 bool Socket::Setup() {
784 // Nothing to do on Linux.
785 return true;
786 }
787
788
789 int Socket::LastError() {
790 return errno;
791 }
792
793
794 uint16_t Socket::HToN(uint16_t value) {
795 return htons(value);
796 }
797
798
799 uint16_t Socket::NToH(uint16_t value) {
800 return ntohs(value);
801 }
802
803
804 uint32_t Socket::HToN(uint32_t value) {
805 return htonl(value);
806 }
807
808
809 uint32_t Socket::NToH(uint32_t value) {
810 return ntohl(value);
811 }
812
813
814 Socket* OS::CreateSocket() {
815 return new LinuxSocket();
816 }
817
818
819 #ifdef ENABLE_LOGGING_AND_PROFILING 646 #ifdef ENABLE_LOGGING_AND_PROFILING
820 647
821 static Sampler* active_sampler_ = NULL; 648 static Sampler* active_sampler_ = NULL;
822 649
823 static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { 650 static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) {
824 // Ucontext is a glibc extension - no profiling on Android at the moment. 651 // Ucontext is a glibc extension - no profiling on Android at the moment.
825 #ifdef __GLIBC__ 652 #ifdef __GLIBC__
826 USE(info); 653 USE(info);
827 if (signal != SIGPROF) return; 654 if (signal != SIGPROF) return;
828 if (active_sampler_ == NULL) return; 655 if (active_sampler_ == NULL) return;
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
912 } 739 }
913 740
914 // This sampler is no longer the active sampler. 741 // This sampler is no longer the active sampler.
915 active_sampler_ = NULL; 742 active_sampler_ = NULL;
916 active_ = false; 743 active_ = false;
917 } 744 }
918 745
919 #endif // ENABLE_LOGGING_AND_PROFILING 746 #endif // ENABLE_LOGGING_AND_PROFILING
920 747
921 } } // namespace v8::internal 748 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/platform-freebsd.cc ('k') | src/platform-macos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698