| 1 | /* |
| 2 | * Copyright (C) 2006-2016 Apple Inc. All rights reserved. |
| 3 | * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com |
| 4 | * Copyright (C) 2015 Igalia S.L. |
| 5 | * Copyright (C) 2018 Sony Interactive Entertainment. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 20 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 21 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 22 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 23 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 24 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 27 | */ |
| 28 | |
| 29 | #include "config.h" |
| 30 | #include "MainThreadSharedTimer.h" |
| 31 | |
| 32 | #include <wtf/NeverDestroyed.h> |
| 33 | |
| 34 | #if USE(GLIB) |
| 35 | #include <wtf/glib/RunLoopSourcePriority.h> |
| 36 | #endif |
| 37 | |
| 38 | namespace WebCore { |
| 39 | |
| 40 | MainThreadSharedTimer& MainThreadSharedTimer::singleton() |
| 41 | { |
| 42 | static NeverDestroyed<MainThreadSharedTimer> instance; |
| 43 | return instance; |
| 44 | } |
| 45 | |
| 46 | #if USE(CF) || OS(WINDOWS) |
| 47 | MainThreadSharedTimer::MainThreadSharedTimer() = default; |
| 48 | #else |
| 49 | MainThreadSharedTimer::MainThreadSharedTimer() |
| 50 | : m_timer(RunLoop::main(), this, &MainThreadSharedTimer::fired) |
| 51 | { |
| 52 | #if USE(GLIB) |
| 53 | m_timer.setPriority(RunLoopSourcePriority::MainThreadSharedTimer); |
| 54 | m_timer.setName("[WebKit] MainThreadSharedTimer" ); |
| 55 | #endif |
| 56 | } |
| 57 | |
| 58 | void MainThreadSharedTimer::setFireInterval(Seconds interval) |
| 59 | { |
| 60 | ASSERT(m_firedFunction); |
| 61 | m_timer.startOneShot(interval); |
| 62 | } |
| 63 | |
| 64 | void MainThreadSharedTimer::stop() |
| 65 | { |
| 66 | m_timer.stop(); |
| 67 | } |
| 68 | |
| 69 | void MainThreadSharedTimer::invalidate() |
| 70 | { |
| 71 | } |
| 72 | #endif |
| 73 | |
| 74 | void MainThreadSharedTimer::setFiredFunction(WTF::Function<void()>&& firedFunction) |
| 75 | { |
| 76 | RELEASE_ASSERT(!m_firedFunction || !firedFunction); |
| 77 | m_firedFunction = WTFMove(firedFunction); |
| 78 | } |
| 79 | |
| 80 | void MainThreadSharedTimer::fired() |
| 81 | { |
| 82 | ASSERT(m_firedFunction); |
| 83 | m_firedFunction(); |
| 84 | } |
| 85 | |
| 86 | } // namespace WebCore |
| 87 | |