Changeset 167263 in webkit


Ignore:
Timestamp:
Apr 14, 2014 1:31:45 PM (10 years ago)
Author:
mark.lam@apple.com
Message:

monotonicallyIncreasingTime() should only initialize its static timebaseInfo once.
<https://webkit.org/b/131630>

Reviewed by Filip Pizlo.

The current initialization of the static field is not thread safe.

  • wtf/CurrentTime.cpp:

(WTF::monotonicallyIncreasingTime):

Location:
trunk/Source/WTF
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r167220 r167263  
     12014-04-14  Mark Lam  <mark.lam@apple.com>
     2
     3        monotonicallyIncreasingTime() should only initialize its static timebaseInfo once.
     4        <https://webkit.org/b/131630>
     5
     6        Reviewed by Filip Pizlo.
     7
     8        The current initialization of the static field is not thread safe.
     9
     10        * wtf/CurrentTime.cpp:
     11        (WTF::monotonicallyIncreasingTime):
     12
    1132014-04-14  Benjamin Poulain  <benjamin@webkit.org>
    214
  • trunk/Source/WTF/wtf/CurrentTime.cpp

    r167101 r167263  
    3838#include <mach/mach.h>
    3939#include <mach/mach_time.h>
     40#include <mutex>
    4041#include <sys/time.h>
    4142#elif OS(WINDOWS)
     
    272273double monotonicallyIncreasingTime()
    273274{
    274     // Based on listing #2 from Apple QA 1398.
     275    // Based on listing #2 from Apple QA 1398, but modified to be thread-safe.
    275276    static mach_timebase_info_data_t timebaseInfo;
    276     if (!timebaseInfo.denom) {
     277    static std::once_flag initializeTimerOnceFlag;
     278    std::call_once(initializeTimerOnceFlag, [] {
    277279        kern_return_t kr = mach_timebase_info(&timebaseInfo);
    278280        ASSERT_UNUSED(kr, kr == KERN_SUCCESS);
    279     }
     281        ASSERT(timebaseInfo.denom);
     282    });
     283
    280284    return (mach_absolute_time() * timebaseInfo.numer) / (1.0e9 * timebaseInfo.denom);
    281285}
Note: See TracChangeset for help on using the changeset viewer.