1/* 2 * Copyright (C) 2010 Apple Inc. All rights reserved. 3 * Portions Copyright (c) 2010 Motorola Mobility, Inc. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 24 * THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#ifndef WorkQueue_h 28#define WorkQueue_h 29 30#if OS(DARWIN) 31#include <dispatch/dispatch.h> 32#endif 33 34#include <chrono> 35#include <functional> 36#include <wtf/Forward.h> 37#include <wtf/Functional.h> 38#include <wtf/HashMap.h> 39#include <wtf/RefCounted.h> 40#include <wtf/Threading.h> 41#include <wtf/Vector.h> 42 43#if PLATFORM(GTK) || PLATFORM(EFL) 44#include "PlatformProcessIdentifier.h" 45#endif 46 47#if PLATFORM(GTK) 48#include <wtf/gobject/GMainLoopSource.h> 49#include <wtf/gobject/GRefPtr.h> 50#elif PLATFORM(EFL) 51#include <DispatchQueueEfl.h> 52#endif 53 54class WorkQueue : public ThreadSafeRefCounted<WorkQueue> { 55public: 56 enum class QOS { 57 UserInteractive, 58 UserInitiated, 59 Default, 60 Utility, 61 Background 62 }; 63 64 static PassRefPtr<WorkQueue> create(const char* name, QOS = QOS::Default); 65 ~WorkQueue(); 66 67 void dispatch(std::function<void ()>); 68 void dispatchAfter(std::chrono::nanoseconds, std::function<void ()>); 69 70#if OS(DARWIN) 71 dispatch_queue_t dispatchQueue() const { return m_dispatchQueue; } 72#elif PLATFORM(GTK) 73 void registerSocketEventHandler(int, std::function<void ()>, std::function<void ()>); 74 void unregisterSocketEventHandler(int); 75#elif PLATFORM(EFL) 76 void registerSocketEventHandler(int, std::function<void ()>); 77 void unregisterSocketEventHandler(int); 78#endif 79 80private: 81 explicit WorkQueue(const char* name, QOS); 82 83 void platformInitialize(const char* name, QOS); 84 void platformInvalidate(); 85 86#if OS(DARWIN) 87 static void executeFunction(void*); 88 dispatch_queue_t m_dispatchQueue; 89#elif PLATFORM(GTK) 90 static void startWorkQueueThread(WorkQueue*); 91 void workQueueThreadBody(); 92 93 ThreadIdentifier m_workQueueThread; 94 GRefPtr<GMainContext> m_eventContext; 95 Mutex m_eventLoopLock; 96 GRefPtr<GMainLoop> m_eventLoop; 97 GMainLoopSource m_socketEventSource; 98#elif PLATFORM(EFL) 99 RefPtr<DispatchQueue> m_dispatchQueue; 100#endif 101}; 102 103#endif // WorkQueue_h 104