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

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

Issue 23698002: Revert "Add Chromium-style TimeDelta, Time and TimeTicks classes, and a new ElapsedTimer class." (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 3 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-posix.cc ('k') | src/profile-generator.h » ('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 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 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 init_fast_sqrt_function(); 239 init_fast_sqrt_function();
240 } 240 }
241 241
242 242
243 // ---------------------------------------------------------------------------- 243 // ----------------------------------------------------------------------------
244 // The Time class represents time on win32. A timestamp is represented as 244 // The Time class represents time on win32. A timestamp is represented as
245 // a 64-bit integer in 100 nanoseconds since January 1, 1601 (UTC). JavaScript 245 // a 64-bit integer in 100 nanoseconds since January 1, 1601 (UTC). JavaScript
246 // timestamps are represented as a doubles in milliseconds since 00:00:00 UTC, 246 // timestamps are represented as a doubles in milliseconds since 00:00:00 UTC,
247 // January 1, 1970. 247 // January 1, 1970.
248 248
249 class Win32Time { 249 class Time {
250 public: 250 public:
251 // Constructors. 251 // Constructors.
252 explicit Win32Time(double jstime); 252 Time();
253 Win32Time(int year, int mon, int day, int hour, int min, int sec); 253 explicit Time(double jstime);
254 Time(int year, int mon, int day, int hour, int min, int sec);
254 255
255 // Convert timestamp to JavaScript representation. 256 // Convert timestamp to JavaScript representation.
256 double ToJSTime(); 257 double ToJSTime();
257 258
259 // Set timestamp to current time.
260 void SetToCurrentTime();
261
258 // Returns the local timezone offset in milliseconds east of UTC. This is 262 // Returns the local timezone offset in milliseconds east of UTC. This is
259 // the number of milliseconds you must add to UTC to get local time, i.e. 263 // the number of milliseconds you must add to UTC to get local time, i.e.
260 // LocalOffset(CET) = 3600000 and LocalOffset(PST) = -28800000. This 264 // LocalOffset(CET) = 3600000 and LocalOffset(PST) = -28800000. This
261 // routine also takes into account whether daylight saving is effect 265 // routine also takes into account whether daylight saving is effect
262 // at the time. 266 // at the time.
263 int64_t LocalOffset(); 267 int64_t LocalOffset();
264 268
265 // Returns the daylight savings time offset for the time in milliseconds. 269 // Returns the daylight savings time offset for the time in milliseconds.
266 int64_t DaylightSavingsOffset(); 270 int64_t DaylightSavingsOffset();
267 271
(...skipping 21 matching lines...) Expand all
289 293
290 // Initialize the timezone information (if not already done). 294 // Initialize the timezone information (if not already done).
291 static void TzSet(); 295 static void TzSet();
292 296
293 // Guess the name of the timezone from the bias. 297 // Guess the name of the timezone from the bias.
294 static const char* GuessTimezoneNameFromBias(int bias); 298 static const char* GuessTimezoneNameFromBias(int bias);
295 299
296 // Return whether or not daylight savings time is in effect at this time. 300 // Return whether or not daylight savings time is in effect at this time.
297 bool InDST(); 301 bool InDST();
298 302
303 // Return the difference (in milliseconds) between this timestamp and
304 // another timestamp.
305 int64_t Diff(Time* other);
306
299 // Accessor for FILETIME representation. 307 // Accessor for FILETIME representation.
300 FILETIME& ft() { return time_.ft_; } 308 FILETIME& ft() { return time_.ft_; }
301 309
302 // Accessor for integer representation. 310 // Accessor for integer representation.
303 int64_t& t() { return time_.t_; } 311 int64_t& t() { return time_.t_; }
304 312
305 // Although win32 uses 64-bit integers for representing timestamps, 313 // Although win32 uses 64-bit integers for representing timestamps,
306 // these are packed into a FILETIME structure. The FILETIME structure 314 // these are packed into a FILETIME structure. The FILETIME structure
307 // is just a struct representing a 64-bit integer. The TimeStamp union 315 // is just a struct representing a 64-bit integer. The TimeStamp union
308 // allows access to both a FILETIME and an integer representation of 316 // allows access to both a FILETIME and an integer representation of
309 // the timestamp. 317 // the timestamp.
310 union TimeStamp { 318 union TimeStamp {
311 FILETIME ft_; 319 FILETIME ft_;
312 int64_t t_; 320 int64_t t_;
313 }; 321 };
314 322
315 TimeStamp time_; 323 TimeStamp time_;
316 }; 324 };
317 325
318 326
319 // Static variables. 327 // Static variables.
320 bool Win32Time::tz_initialized_ = false; 328 bool Time::tz_initialized_ = false;
321 TIME_ZONE_INFORMATION Win32Time::tzinfo_; 329 TIME_ZONE_INFORMATION Time::tzinfo_;
322 char Win32Time::std_tz_name_[kTzNameSize]; 330 char Time::std_tz_name_[kTzNameSize];
323 char Win32Time::dst_tz_name_[kTzNameSize]; 331 char Time::dst_tz_name_[kTzNameSize];
332
333
334 // Initialize timestamp to start of epoc.
335 Time::Time() {
336 t() = 0;
337 }
324 338
325 339
326 // Initialize timestamp from a JavaScript timestamp. 340 // Initialize timestamp from a JavaScript timestamp.
327 Win32Time::Win32Time(double jstime) { 341 Time::Time(double jstime) {
328 t() = static_cast<int64_t>(jstime) * kTimeScaler + kTimeEpoc; 342 t() = static_cast<int64_t>(jstime) * kTimeScaler + kTimeEpoc;
329 } 343 }
330 344
331 345
332 // Initialize timestamp from date/time components. 346 // Initialize timestamp from date/time components.
333 Win32Time::Win32Time(int year, int mon, int day, int hour, int min, int sec) { 347 Time::Time(int year, int mon, int day, int hour, int min, int sec) {
334 SYSTEMTIME st; 348 SYSTEMTIME st;
335 st.wYear = year; 349 st.wYear = year;
336 st.wMonth = mon; 350 st.wMonth = mon;
337 st.wDay = day; 351 st.wDay = day;
338 st.wHour = hour; 352 st.wHour = hour;
339 st.wMinute = min; 353 st.wMinute = min;
340 st.wSecond = sec; 354 st.wSecond = sec;
341 st.wMilliseconds = 0; 355 st.wMilliseconds = 0;
342 SystemTimeToFileTime(&st, &ft()); 356 SystemTimeToFileTime(&st, &ft());
343 } 357 }
344 358
345 359
346 // Convert timestamp to JavaScript timestamp. 360 // Convert timestamp to JavaScript timestamp.
347 double Win32Time::ToJSTime() { 361 double Time::ToJSTime() {
348 return static_cast<double>((t() - kTimeEpoc) / kTimeScaler); 362 return static_cast<double>((t() - kTimeEpoc) / kTimeScaler);
349 } 363 }
350 364
351 365
352 // Guess the name of the timezone from the bias. 366 // Guess the name of the timezone from the bias.
353 // The guess is very biased towards the northern hemisphere. 367 // The guess is very biased towards the northern hemisphere.
354 const char* Win32Time::GuessTimezoneNameFromBias(int bias) { 368 const char* Time::GuessTimezoneNameFromBias(int bias) {
355 static const int kHour = 60; 369 static const int kHour = 60;
356 switch (-bias) { 370 switch (-bias) {
357 case -9*kHour: return "Alaska"; 371 case -9*kHour: return "Alaska";
358 case -8*kHour: return "Pacific"; 372 case -8*kHour: return "Pacific";
359 case -7*kHour: return "Mountain"; 373 case -7*kHour: return "Mountain";
360 case -6*kHour: return "Central"; 374 case -6*kHour: return "Central";
361 case -5*kHour: return "Eastern"; 375 case -5*kHour: return "Eastern";
362 case -4*kHour: return "Atlantic"; 376 case -4*kHour: return "Atlantic";
363 case 0*kHour: return "GMT"; 377 case 0*kHour: return "GMT";
364 case +1*kHour: return "Central Europe"; 378 case +1*kHour: return "Central Europe";
365 case +2*kHour: return "Eastern Europe"; 379 case +2*kHour: return "Eastern Europe";
366 case +3*kHour: return "Russia"; 380 case +3*kHour: return "Russia";
367 case +5*kHour + 30: return "India"; 381 case +5*kHour + 30: return "India";
368 case +8*kHour: return "China"; 382 case +8*kHour: return "China";
369 case +9*kHour: return "Japan"; 383 case +9*kHour: return "Japan";
370 case +12*kHour: return "New Zealand"; 384 case +12*kHour: return "New Zealand";
371 default: return "Local"; 385 default: return "Local";
372 } 386 }
373 } 387 }
374 388
375 389
376 // Initialize timezone information. The timezone information is obtained from 390 // Initialize timezone information. The timezone information is obtained from
377 // windows. If we cannot get the timezone information we fall back to CET. 391 // windows. If we cannot get the timezone information we fall back to CET.
378 // Please notice that this code is not thread-safe. 392 // Please notice that this code is not thread-safe.
379 void Win32Time::TzSet() { 393 void Time::TzSet() {
380 // Just return if timezone information has already been initialized. 394 // Just return if timezone information has already been initialized.
381 if (tz_initialized_) return; 395 if (tz_initialized_) return;
382 396
383 // Initialize POSIX time zone data. 397 // Initialize POSIX time zone data.
384 _tzset(); 398 _tzset();
385 // Obtain timezone information from operating system. 399 // Obtain timezone information from operating system.
386 memset(&tzinfo_, 0, sizeof(tzinfo_)); 400 memset(&tzinfo_, 0, sizeof(tzinfo_));
387 if (GetTimeZoneInformation(&tzinfo_) == TIME_ZONE_ID_INVALID) { 401 if (GetTimeZoneInformation(&tzinfo_) == TIME_ZONE_ID_INVALID) {
388 // If we cannot get timezone information we fall back to CET. 402 // If we cannot get timezone information we fall back to CET.
389 tzinfo_.Bias = -60; 403 tzinfo_.Bias = -60;
(...skipping 28 matching lines...) Expand all
418 OS::SNPrintF(Vector<char>(dst_tz_name_, kTzNameSize - 1), 432 OS::SNPrintF(Vector<char>(dst_tz_name_, kTzNameSize - 1),
419 "%s Daylight Time", 433 "%s Daylight Time",
420 GuessTimezoneNameFromBias(tzinfo_.Bias)); 434 GuessTimezoneNameFromBias(tzinfo_.Bias));
421 } 435 }
422 436
423 // Timezone information initialized. 437 // Timezone information initialized.
424 tz_initialized_ = true; 438 tz_initialized_ = true;
425 } 439 }
426 440
427 441
442 // Return the difference in milliseconds between this and another timestamp.
443 int64_t Time::Diff(Time* other) {
444 return (t() - other->t()) / kTimeScaler;
445 }
446
447
448 // Set timestamp to current time.
449 void Time::SetToCurrentTime() {
450 // The default GetSystemTimeAsFileTime has a ~15.5ms resolution.
451 // Because we're fast, we like fast timers which have at least a
452 // 1ms resolution.
453 //
454 // timeGetTime() provides 1ms granularity when combined with
455 // timeBeginPeriod(). If the host application for v8 wants fast
456 // timers, it can use timeBeginPeriod to increase the resolution.
457 //
458 // Using timeGetTime() has a drawback because it is a 32bit value
459 // and hence rolls-over every ~49days.
460 //
461 // To use the clock, we use GetSystemTimeAsFileTime as our base;
462 // and then use timeGetTime to extrapolate current time from the
463 // start time. To deal with rollovers, we resync the clock
464 // any time when more than kMaxClockElapsedTime has passed or
465 // whenever timeGetTime creates a rollover.
466
467 static bool initialized = false;
468 static TimeStamp init_time;
469 static DWORD init_ticks;
470 static const int64_t kHundredNanosecondsPerSecond = 10000000;
471 static const int64_t kMaxClockElapsedTime =
472 60*kHundredNanosecondsPerSecond; // 1 minute
473
474 // If we are uninitialized, we need to resync the clock.
475 bool needs_resync = !initialized;
476
477 // Get the current time.
478 TimeStamp time_now;
479 GetSystemTimeAsFileTime(&time_now.ft_);
480 DWORD ticks_now = timeGetTime();
481
482 // Check if we need to resync due to clock rollover.
483 needs_resync |= ticks_now < init_ticks;
484
485 // Check if we need to resync due to elapsed time.
486 needs_resync |= (time_now.t_ - init_time.t_) > kMaxClockElapsedTime;
487
488 // Check if we need to resync due to backwards time change.
489 needs_resync |= time_now.t_ < init_time.t_;
490
491 // Resync the clock if necessary.
492 if (needs_resync) {
493 GetSystemTimeAsFileTime(&init_time.ft_);
494 init_ticks = ticks_now = timeGetTime();
495 initialized = true;
496 }
497
498 // Finally, compute the actual time. Why is this so hard.
499 DWORD elapsed = ticks_now - init_ticks;
500 this->time_.t_ = init_time.t_ + (static_cast<int64_t>(elapsed) * 10000);
501 }
502
503
428 // Return the local timezone offset in milliseconds east of UTC. This 504 // Return the local timezone offset in milliseconds east of UTC. This
429 // takes into account whether daylight saving is in effect at the time. 505 // takes into account whether daylight saving is in effect at the time.
430 // Only times in the 32-bit Unix range may be passed to this function. 506 // Only times in the 32-bit Unix range may be passed to this function.
431 // Also, adding the time-zone offset to the input must not overflow. 507 // Also, adding the time-zone offset to the input must not overflow.
432 // The function EquivalentTime() in date.js guarantees this. 508 // The function EquivalentTime() in date.js guarantees this.
433 int64_t Win32Time::LocalOffset() { 509 int64_t Time::LocalOffset() {
434 // Initialize timezone information, if needed. 510 // Initialize timezone information, if needed.
435 TzSet(); 511 TzSet();
436 512
437 Win32Time rounded_to_second(*this); 513 Time rounded_to_second(*this);
438 rounded_to_second.t() = rounded_to_second.t() / 1000 / kTimeScaler * 514 rounded_to_second.t() = rounded_to_second.t() / 1000 / kTimeScaler *
439 1000 * kTimeScaler; 515 1000 * kTimeScaler;
440 // Convert to local time using POSIX localtime function. 516 // Convert to local time using POSIX localtime function.
441 // Windows XP Service Pack 3 made SystemTimeToTzSpecificLocalTime() 517 // Windows XP Service Pack 3 made SystemTimeToTzSpecificLocalTime()
442 // very slow. Other browsers use localtime(). 518 // very slow. Other browsers use localtime().
443 519
444 // Convert from JavaScript milliseconds past 1/1/1970 0:00:00 to 520 // Convert from JavaScript milliseconds past 1/1/1970 0:00:00 to
445 // POSIX seconds past 1/1/1970 0:00:00. 521 // POSIX seconds past 1/1/1970 0:00:00.
446 double unchecked_posix_time = rounded_to_second.ToJSTime() / 1000; 522 double unchecked_posix_time = rounded_to_second.ToJSTime() / 1000;
447 if (unchecked_posix_time > INT_MAX || unchecked_posix_time < 0) { 523 if (unchecked_posix_time > INT_MAX || unchecked_posix_time < 0) {
(...skipping 10 matching lines...) Expand all
458 return (tzinfo_.Bias + tzinfo_.DaylightBias) * -kMsPerMinute; 534 return (tzinfo_.Bias + tzinfo_.DaylightBias) * -kMsPerMinute;
459 } else if (posix_local_time_struct.tm_isdst == 0) { 535 } else if (posix_local_time_struct.tm_isdst == 0) {
460 return (tzinfo_.Bias + tzinfo_.StandardBias) * -kMsPerMinute; 536 return (tzinfo_.Bias + tzinfo_.StandardBias) * -kMsPerMinute;
461 } else { 537 } else {
462 return tzinfo_.Bias * -kMsPerMinute; 538 return tzinfo_.Bias * -kMsPerMinute;
463 } 539 }
464 } 540 }
465 541
466 542
467 // Return whether or not daylight savings time is in effect at this time. 543 // Return whether or not daylight savings time is in effect at this time.
468 bool Win32Time::InDST() { 544 bool Time::InDST() {
469 // Initialize timezone information, if needed. 545 // Initialize timezone information, if needed.
470 TzSet(); 546 TzSet();
471 547
472 // Determine if DST is in effect at the specified time. 548 // Determine if DST is in effect at the specified time.
473 bool in_dst = false; 549 bool in_dst = false;
474 if (tzinfo_.StandardDate.wMonth != 0 || tzinfo_.DaylightDate.wMonth != 0) { 550 if (tzinfo_.StandardDate.wMonth != 0 || tzinfo_.DaylightDate.wMonth != 0) {
475 // Get the local timezone offset for the timestamp in milliseconds. 551 // Get the local timezone offset for the timestamp in milliseconds.
476 int64_t offset = LocalOffset(); 552 int64_t offset = LocalOffset();
477 553
478 // Compute the offset for DST. The bias parameters in the timezone info 554 // Compute the offset for DST. The bias parameters in the timezone info
479 // are specified in minutes. These must be converted to milliseconds. 555 // are specified in minutes. These must be converted to milliseconds.
480 int64_t dstofs = -(tzinfo_.Bias + tzinfo_.DaylightBias) * kMsPerMinute; 556 int64_t dstofs = -(tzinfo_.Bias + tzinfo_.DaylightBias) * kMsPerMinute;
481 557
482 // If the local time offset equals the timezone bias plus the daylight 558 // If the local time offset equals the timezone bias plus the daylight
483 // bias then DST is in effect. 559 // bias then DST is in effect.
484 in_dst = offset == dstofs; 560 in_dst = offset == dstofs;
485 } 561 }
486 562
487 return in_dst; 563 return in_dst;
488 } 564 }
489 565
490 566
491 // Return the daylight savings time offset for this time. 567 // Return the daylight savings time offset for this time.
492 int64_t Win32Time::DaylightSavingsOffset() { 568 int64_t Time::DaylightSavingsOffset() {
493 return InDST() ? 60 * kMsPerMinute : 0; 569 return InDST() ? 60 * kMsPerMinute : 0;
494 } 570 }
495 571
496 572
497 // Returns a string identifying the current timezone for the 573 // Returns a string identifying the current timezone for the
498 // timestamp taking into account daylight saving. 574 // timestamp taking into account daylight saving.
499 char* Win32Time::LocalTimezone() { 575 char* Time::LocalTimezone() {
500 // Return the standard or DST time zone name based on whether daylight 576 // Return the standard or DST time zone name based on whether daylight
501 // saving is in effect at the given time. 577 // saving is in effect at the given time.
502 return InDST() ? dst_tz_name_ : std_tz_name_; 578 return InDST() ? dst_tz_name_ : std_tz_name_;
503 } 579 }
504 580
505 581
506 void OS::PostSetUp() { 582 void OS::PostSetUp() {
507 // Math functions depend on CPU features therefore they are initialized after 583 // Math functions depend on CPU features therefore they are initialized after
508 // CPU. 584 // CPU.
509 MathSetup(); 585 MathSetup();
(...skipping 21 matching lines...) Expand all
531 // Convert to seconds and microseconds 607 // Convert to seconds and microseconds
532 *secs = static_cast<uint32_t>(usertime / 1000000); 608 *secs = static_cast<uint32_t>(usertime / 1000000);
533 *usecs = static_cast<uint32_t>(usertime % 1000000); 609 *usecs = static_cast<uint32_t>(usertime % 1000000);
534 return 0; 610 return 0;
535 } 611 }
536 612
537 613
538 // Returns current time as the number of milliseconds since 614 // Returns current time as the number of milliseconds since
539 // 00:00:00 UTC, January 1, 1970. 615 // 00:00:00 UTC, January 1, 1970.
540 double OS::TimeCurrentMillis() { 616 double OS::TimeCurrentMillis() {
541 return Time::Now().ToJsTime(); 617 Time t;
618 t.SetToCurrentTime();
619 return t.ToJSTime();
620 }
621
622
623 // Returns the tickcounter based on timeGetTime.
624 int64_t OS::Ticks() {
625 return timeGetTime() * 1000; // Convert to microseconds.
542 } 626 }
543 627
544 628
545 // Returns a string identifying the current timezone taking into 629 // Returns a string identifying the current timezone taking into
546 // account daylight saving. 630 // account daylight saving.
547 const char* OS::LocalTimezone(double time) { 631 const char* OS::LocalTimezone(double time) {
548 return Win32Time(time).LocalTimezone(); 632 return Time(time).LocalTimezone();
549 } 633 }
550 634
551 635
552 // Returns the local time offset in milliseconds east of UTC without 636 // Returns the local time offset in milliseconds east of UTC without
553 // taking daylight savings time into account. 637 // taking daylight savings time into account.
554 double OS::LocalTimeOffset() { 638 double OS::LocalTimeOffset() {
555 // Use current time, rounded to the millisecond. 639 // Use current time, rounded to the millisecond.
556 Win32Time t(TimeCurrentMillis()); 640 Time t(TimeCurrentMillis());
557 // Time::LocalOffset inlcudes any daylight savings offset, so subtract it. 641 // Time::LocalOffset inlcudes any daylight savings offset, so subtract it.
558 return static_cast<double>(t.LocalOffset() - t.DaylightSavingsOffset()); 642 return static_cast<double>(t.LocalOffset() - t.DaylightSavingsOffset());
559 } 643 }
560 644
561 645
562 // Returns the daylight savings offset in milliseconds for the given 646 // Returns the daylight savings offset in milliseconds for the given
563 // time. 647 // time.
564 double OS::DaylightSavingsOffset(double time) { 648 double OS::DaylightSavingsOffset(double time) {
565 int64_t offset = Win32Time(time).DaylightSavingsOffset(); 649 int64_t offset = Time(time).DaylightSavingsOffset();
566 return static_cast<double>(offset); 650 return static_cast<double>(offset);
567 } 651 }
568 652
569 653
570 int OS::GetLastError() { 654 int OS::GetLastError() {
571 return ::GetLastError(); 655 return ::GetLastError();
572 } 656 }
573 657
574 658
575 int OS::GetCurrentProcessId() { 659 int OS::GetCurrentProcessId() {
(...skipping 1333 matching lines...) Expand 10 before | Expand all | Expand 10 after
1909 limit_mutex = CreateMutex(); 1993 limit_mutex = CreateMutex();
1910 } 1994 }
1911 1995
1912 1996
1913 void OS::TearDown() { 1997 void OS::TearDown() {
1914 delete limit_mutex; 1998 delete limit_mutex;
1915 } 1999 }
1916 2000
1917 2001
1918 } } // namespace v8::internal 2002 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/platform-posix.cc ('k') | src/profile-generator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698