OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 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 13 matching lines...) Expand all Loading... |
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 POSIX goes here. This is not a platform on its | 28 // Platform specific code for POSIX goes here. This is not a platform on its |
29 // own but contains the parts which are the same across POSIX platforms Linux, | 29 // own but contains the parts which are the same across POSIX platforms Linux, |
30 // Mac OS and FreeBSD. | 30 // Mac OS and FreeBSD. |
31 | 31 |
32 #include <unistd.h> | 32 #include <unistd.h> |
33 #include <errno.h> | 33 #include <errno.h> |
| 34 #include <time.h> |
34 | 35 |
35 #include <sys/socket.h> | 36 #include <sys/socket.h> |
| 37 #include <sys/resource.h> |
| 38 #include <sys/time.h> |
36 #include <sys/types.h> | 39 #include <sys/types.h> |
37 | 40 |
38 #include <arpa/inet.h> | 41 #include <arpa/inet.h> |
39 #include <netinet/in.h> | 42 #include <netinet/in.h> |
40 #include <netdb.h> | 43 #include <netdb.h> |
41 | 44 |
42 #include "v8.h" | 45 #include "v8.h" |
43 | 46 |
44 #include "platform.h" | 47 #include "platform.h" |
45 | 48 |
46 | 49 |
47 namespace v8 { namespace internal { | 50 namespace v8 { namespace internal { |
48 | 51 |
49 | 52 |
50 // ---------------------------------------------------------------------------- | 53 // ---------------------------------------------------------------------------- |
| 54 // POSIX date/time support. |
| 55 // |
| 56 |
| 57 int OS::GetUserTime(uint32_t* secs, uint32_t* usecs) { |
| 58 struct rusage usage; |
| 59 |
| 60 if (getrusage(RUSAGE_SELF, &usage) < 0) return -1; |
| 61 *secs = usage.ru_utime.tv_sec; |
| 62 *usecs = usage.ru_utime.tv_usec; |
| 63 return 0; |
| 64 } |
| 65 |
| 66 |
| 67 double OS::TimeCurrentMillis() { |
| 68 struct timeval tv; |
| 69 if (gettimeofday(&tv, NULL) < 0) return 0.0; |
| 70 return (static_cast<double>(tv.tv_sec) * 1000) + |
| 71 (static_cast<double>(tv.tv_usec) / 1000); |
| 72 } |
| 73 |
| 74 |
| 75 int64_t OS::Ticks() { |
| 76 // gettimeofday has microsecond resolution. |
| 77 struct timeval tv; |
| 78 if (gettimeofday(&tv, NULL) < 0) |
| 79 return 0; |
| 80 return (static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec; |
| 81 } |
| 82 |
| 83 |
| 84 char* OS::LocalTimezone(double time) { |
| 85 time_t tv = static_cast<time_t>(floor(time/msPerSecond)); |
| 86 struct tm* t = localtime(&tv); |
| 87 return const_cast<char*>(t->tm_zone); |
| 88 } |
| 89 |
| 90 |
| 91 double OS::DaylightSavingsOffset(double time) { |
| 92 time_t tv = static_cast<time_t>(floor(time/msPerSecond)); |
| 93 struct tm* t = localtime(&tv); |
| 94 return t->tm_isdst > 0 ? 3600 * msPerSecond : 0; |
| 95 } |
| 96 |
| 97 |
| 98 double OS::LocalTimeOffset() { |
| 99 time_t tv = time(NULL); |
| 100 struct tm* t = localtime(&tv); |
| 101 // tm_gmtoff includes any daylight savings offset, so subtract it. |
| 102 return static_cast<double>(t->tm_gmtoff * msPerSecond - |
| 103 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); |
| 104 } |
| 105 |
| 106 |
| 107 // ---------------------------------------------------------------------------- |
51 // POSIX socket support. | 108 // POSIX socket support. |
52 // | 109 // |
53 | 110 |
54 class POSIXSocket : public Socket { | 111 class POSIXSocket : public Socket { |
55 public: | 112 public: |
56 explicit POSIXSocket() { | 113 explicit POSIXSocket() { |
57 // Create the socket. | 114 // Create the socket. |
58 socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | 115 socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
59 } | 116 } |
60 explicit POSIXSocket(int socket): socket_(socket) { } | 117 explicit POSIXSocket(int socket): socket_(socket) { } |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 return ntohl(value); | 267 return ntohl(value); |
211 } | 268 } |
212 | 269 |
213 | 270 |
214 Socket* OS::CreateSocket() { | 271 Socket* OS::CreateSocket() { |
215 return new POSIXSocket(); | 272 return new POSIXSocket(); |
216 } | 273 } |
217 | 274 |
218 | 275 |
219 } } // namespace v8::internal | 276 } } // namespace v8::internal |
OLD | NEW |