Changeset 31063 in webkit


Ignore:
Timestamp:
Mar 14, 2008 12:53:37 PM (16 years ago)
Author:
Adam Roben
Message:

Make most of callOnMainThread's implementation be cross-platform

I took the non-platform-specific parts of MainThreadWin.cpp and moved
them to a new MainThread.cpp. Each platform is now responsible for
implementing one function, scheduleDispatchFunctionsOnMainThread,
which is supposed to set things up so that
dispatchFunctionsFromMainThread gets called from the main thread in
the near future.

Reviewed by Alexey.

  • GNUmakefile.am: Added MainThread.cpp to the project.
  • WebCore.pro: Ditto.
  • WebCore.vcproj/WebCore.vcproj: Ditto.
  • WebCore.xcodeproj/project.pbxproj: Ditto.
  • WebCoreSources.bkl: Ditto.
  • platform/MainThread.cpp:
    • Copied from WebCore/platform/win/MainThreadWin.cpp.
    • Removed the Windows-specific parts.

(WebCore::callOnMainThread): Changed to call
scheduleDispatchFunctionsOnMainThread instead of PostMessage.

  • platform/gtk/MainThreadGtk.cpp: (WebCore::timeoutFired): Renamed from callFunctionOnMainThread. Now just calls dispatchFunctionsFromMainThread. (WebCore::scheduleDispatchFunctionsOnMainThread): Added. Calls through to g_timeout_add.
  • platform/mac/MainThreadMac.mm: Renamed WebCoreFunctionWrapper to WebCoreMainThreadCaller. (-[WebCoreMainThreadCaller call]): Calls through to dispatchFunctionsFromMainThread. (WebCore::scheduleDispatchFunctionsOnMainThread): Makes a new WebCoreMainThreadCaller and calls performSelectorOnMainThread on it.
  • platform/qt/MainThreadQt.cpp: Removed PerformFunctionEvent. (WebCore::MainThreadInvoker::event): Chagned to call through to dispatchFunctionsFromMainThread. (WebCore::scheduleDispatchFunctionsOnMainThread): Sends an empty event to the MainThreadInvoker.
  • platform/win/MainThreadWin.cpp:
    • Removed the non-Windows-specific parts.
    • Removed some unnecessary initialization of static variables to 0.

(WebCore::ThreadingWindowWndProc): Changed to call
dispatchFunctionsFromMainThread.
(WebCore::scheduleDispatchFunctionsOnMainThread): Calls through to
PostMessage.

  • platform/wx/MainThreadWx.cpp: (WebCore::scheduleDispatchFunctionsOnMainThread): Added.
Location:
trunk/WebCore
Files:
12 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r31060 r31063  
     12008-03-13  Adam Roben  <aroben@apple.com>
     2
     3        Make most of callOnMainThread's implementation be cross-platform
     4
     5        I took the non-platform-specific parts of MainThreadWin.cpp and moved
     6        them to a new MainThread.cpp. Each platform is now responsible for
     7        implementing one function, scheduleDispatchFunctionsOnMainThread,
     8        which is supposed to set things up so that
     9        dispatchFunctionsFromMainThread gets called from the main thread in
     10        the near future.
     11
     12        Reviewed by Alexey.
     13
     14        * GNUmakefile.am: Added MainThread.cpp to the project.
     15        * WebCore.pro: Ditto.
     16        * WebCore.vcproj/WebCore.vcproj: Ditto.
     17        * WebCore.xcodeproj/project.pbxproj: Ditto.
     18        * WebCoreSources.bkl: Ditto.
     19        * platform/MainThread.cpp:
     20          - Copied from WebCore/platform/win/MainThreadWin.cpp.
     21          - Removed the Windows-specific parts.
     22        (WebCore::callOnMainThread): Changed to call
     23        scheduleDispatchFunctionsOnMainThread instead of PostMessage.
     24        * platform/gtk/MainThreadGtk.cpp:
     25        (WebCore::timeoutFired): Renamed from callFunctionOnMainThread. Now
     26        just calls dispatchFunctionsFromMainThread.
     27        (WebCore::scheduleDispatchFunctionsOnMainThread): Added. Calls through
     28        to g_timeout_add.
     29        * platform/mac/MainThreadMac.mm: Renamed WebCoreFunctionWrapper to
     30        WebCoreMainThreadCaller.
     31        (-[WebCoreMainThreadCaller call]): Calls through to
     32        dispatchFunctionsFromMainThread.
     33        (WebCore::scheduleDispatchFunctionsOnMainThread): Makes a new
     34        WebCoreMainThreadCaller and calls performSelectorOnMainThread on it.
     35        * platform/qt/MainThreadQt.cpp: Removed PerformFunctionEvent.
     36        (WebCore::MainThreadInvoker::event): Chagned to call through to
     37        dispatchFunctionsFromMainThread.
     38        (WebCore::scheduleDispatchFunctionsOnMainThread): Sends an empty event
     39        to the MainThreadInvoker.
     40        * platform/win/MainThreadWin.cpp:
     41          - Removed the non-Windows-specific parts.
     42          - Removed some unnecessary initialization of static variables to 0.
     43        (WebCore::ThreadingWindowWndProc): Changed to call
     44        dispatchFunctionsFromMainThread.
     45        (WebCore::scheduleDispatchFunctionsOnMainThread): Calls through to
     46        PostMessage.
     47        * platform/wx/MainThreadWx.cpp:
     48        (WebCore::scheduleDispatchFunctionsOnMainThread): Added.
     49
    1502008-03-14  Beth Dakin  <bdakin@apple.com>
    251
  • trunk/WebCore/GNUmakefile.am

    r30989 r31063  
    856856        WebCore/platform/Logging.cpp \
    857857        WebCore/platform/MIMETypeRegistry.cpp \
     858        WebCore/platform/MainThread.cpp \
    858859        WebCore/platform/network/AuthenticationChallengeBase.cpp \
    859860        WebCore/platform/network/Credential.cpp \
  • trunk/WebCore/WebCore.pro

    r30960 r31063  
    793793    platform/Logging.cpp \
    794794    platform/MIMETypeRegistry.cpp \
     795    platform/MainThread.cpp \
    795796    platform/network/AuthenticationChallengeBase.cpp \
    796797    platform/network/Credential.cpp \
  • trunk/WebCore/WebCore.vcproj/WebCore.vcproj

    r31033 r31063  
    36613661                        </File>
    36623662                        <File
     3663                                RelativePath="..\platform\MainThread.cpp"
     3664                                >
     3665                        </File>
     3666                        <File
    36633667                                RelativePath="..\platform\MainThread.h"
    36643668                                >
  • trunk/WebCore/WebCore.xcodeproj/project.pbxproj

    r31051 r31063  
    333333                1CB4214D0AF2B2CA0085AD91 /* DOMRangeInternal.h in Copy Generated Headers */ = {isa = PBXBuildFile; fileRef = 8538F05A0AD722F1006A81D1 /* DOMRangeInternal.h */; };
    334334                1CE24F970D7CAF0E007E04C2 /* SchedulePairMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1CE24F960D7CAF0E007E04C2 /* SchedulePairMac.mm */; };
     335                1CE3F8B40D89CFDC00FB035F /* MainThread.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1CE3F8B30D89CFDC00FB035F /* MainThread.cpp */; };
    335336                1CE83AC30ADAFFD7009354F6 /* DeleteButtonController.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1C4C8EFF0AD85D87009475CE /* DeleteButtonController.cpp */; };
    336337                1CE83AC40ADAFFD8009354F6 /* DeleteButton.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1C4C8F630AD8655D009475CE /* DeleteButton.cpp */; };
     
    46754676                1CDD45E60BA9C84600F90147 /* Base.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = Base.xcconfig; sourceTree = "<group>"; };
    46764677                1CE24F960D7CAF0E007E04C2 /* SchedulePairMac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = SchedulePairMac.mm; sourceTree = "<group>"; };
     4678                1CE3F8B30D89CFDC00FB035F /* MainThread.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = MainThread.cpp; path = mac/MainThread.cpp; sourceTree = "<group>"; };
    46774679                1CEFC9B70D78DC8C007D2579 /* SchedulePair.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SchedulePair.h; sourceTree = "<group>"; };
    46784680                1CEFC9B80D78DC8C007D2579 /* SchedulePair.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SchedulePair.cpp; sourceTree = "<group>"; };
     
    1200912011                                A8239DFE09B3CF8A00B60641 /* Logging.cpp */,
    1201012012                                A8239DFF09B3CF8A00B60641 /* Logging.h */,
     12013                                1CE3F8B30D89CFDC00FB035F /* MainThread.cpp */,
    1201112014                                E1EE79360D6C97DE00FEA3BA /* MainThread.h */,
    1201212015                                BC772C4C0C4EB3040083285F /* MIMETypeRegistry.cpp */,
     
    1617916182                                A9D248060D757E7D00FDF959 /* JSMimeType.cpp in Sources */,
    1618016183                                A9D248080D757E7D00FDF959 /* JSMimeTypeArray.cpp in Sources */,
     16184                                1CE3F8B40D89CFDC00FB035F /* MainThread.cpp in Sources */,
    1618116185                        );
    1618216186                        runOnlyForDeploymentPostprocessing = 0;
  • trunk/WebCore/WebCoreSources.bkl

    r30924 r31063  
    611611        platform/Logging.cpp
    612612        platform/MIMETypeRegistry.cpp
     613        platform/MainThread.cpp
    613614        platform/text/RegularExpression.cpp
    614615        platform/ScrollBar.cpp
  • trunk/WebCore/platform/MainThread.cpp

    r31062 r31063  
    11/*
    22 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
    3  * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com)
    43 *
    54 * Redistribution and use in source and binary forms, with or without
     
    1211 *     notice, this list of conditions and the following disclaimer in the
    1312 *     documentation and/or other materials provided with the distribution.
    14  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
     13 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
    1514 *     its contributors may be used to endorse or promote products derived
    1615 *     from this software without specific prior written permission.
     
    3130#include "MainThread.h"
    3231
    33 #include <glib.h>
     32#include "Logging.h"
     33#include <wtf/Vector.h>
    3434
    3535namespace WebCore {
     
    3838    MainThreadFunction* function;
    3939    void* context;
     40    FunctionWithContext(MainThreadFunction* f = 0, void* c = 0) : function(f), context(c) { }
    4041};
    4142
    42 static gboolean callFunctionOnMainThread(gpointer data)
     43typedef Vector<FunctionWithContext> FunctionQueue;
     44
     45static Mutex& functionQueueMutex()
    4346{
    44     FunctionWithContext* functionWithContext = static_cast<FunctionWithContext*>(data);
    45     functionWithContext->function(functionWithContext->context);
    46     delete functionWithContext;
    47     return FALSE;
     47    static Mutex staticFunctionQueueMutex;
     48    return staticFunctionQueueMutex;
     49}
     50
     51static FunctionQueue& functionQueue()
     52{
     53    static FunctionQueue staticFunctionQueue;
     54    return staticFunctionQueue;
     55}
     56
     57void dispatchFunctionsFromMainThread()
     58{
     59    FunctionQueue queueCopy;
     60    {
     61        MutexLocker locker(functionQueueMutex());
     62        queueCopy.swap(functionQueue());
     63    }
     64
     65    LOG(Threading, "Calling %u functions on the main thread", queueCopy.size());
     66    for (unsigned i = 0; i < queueCopy.size(); ++i)
     67        queueCopy[i].function(queueCopy[i].context);
    4868}
    4969
     
    5171{
    5272    ASSERT(function);
    53     FunctionWithContext* functionWithContext = new FunctionWithContext;
    54     functionWithContext->function = function;
    55     functionWithContext->context = context;
    56     g_timeout_add(0, callFunctionOnMainThread, functionWithContext);
     73
     74    {
     75        MutexLocker locker(functionQueueMutex());
     76        functionQueue().append(FunctionWithContext(function, context));
     77    }
     78
     79    scheduleDispatchFunctionsOnMainThread();
    5780}
    5881
    59 
    60 }
     82} // namespace WebCore
  • trunk/WebCore/platform/MainThread.h

    r30522 r31063  
    4848#endif
    4949
     50// These functions are internal to the callOnMainThread implementation.
     51void dispatchFunctionsFromMainThread();
     52void scheduleDispatchFunctionsOnMainThread();
    5053
    5154} // namespace WebCore
  • trunk/WebCore/platform/gtk/MainThreadGtk.cpp

    r30522 r31063  
    3535namespace WebCore {
    3636
    37 struct FunctionWithContext {
    38     MainThreadFunction* function;
    39     void* context;
    40 };
    41 
    42 static gboolean callFunctionOnMainThread(gpointer data)
     37static gboolean timeoutFired(gpointer)
    4338{
    44     FunctionWithContext* functionWithContext = static_cast<FunctionWithContext*>(data);
    45     functionWithContext->function(functionWithContext->context);
    46     delete functionWithContext;
     39    dispatchFunctionsFromMainThread();
    4740    return FALSE;
    4841}
    4942
    50 void callOnMainThread(MainThreadFunction* function, void* context)
     43void scheduleDispatchFunctionsOnMainThread()
    5144{
    52     ASSERT(function);
    53     FunctionWithContext* functionWithContext = new FunctionWithContext;
    54     functionWithContext->function = function;
    55     functionWithContext->context = context;
    56     g_timeout_add(0, callFunctionOnMainThread, functionWithContext);
     45    g_timeout_add(0, timeoutFired, 0);
    5746}
    5847
  • trunk/WebCore/platform/mac/MainThreadMac.mm

    r30522 r31063  
    3030#import "MainThread.h"
    3131
    32 @interface WebCoreFunctionWrapper : NSObject {
    33     WebCore::MainThreadFunction* m_function;
    34     void* m_context;
     32@interface WebCoreMainThreadCaller : NSObject {
    3533}
    36 - (id)initWithFunction:(WebCore::MainThreadFunction*)function context:(void*)context;
    37 - (void)invoke;
     34- (void)call;
    3835@end
    3936
    40 @implementation WebCoreFunctionWrapper
     37@implementation WebCoreMainThreadCaller
    4138
    42 - (id)initWithFunction:(WebCore::MainThreadFunction*)function context:(void*)context;
     39- (void)call
    4340{
    44     [super init];
    45     m_function = function;
    46     m_context = context;
    47     return self;
     41    dispatchFunctionsFromMainThread();
    4842}
    4943
    50 - (void)invoke
    51 {
    52     m_function(m_context);
    53 }
    54 
    55 @end // implementation WebCoreFunctionWrapper
     44@end // implementation WebCoreMainThreadCaller
    5645
    5746namespace WebCore {
    5847
    59 void callOnMainThread(MainThreadFunction* function, void* context)
     48void scheduleDispatchFunctionsOnMainThread()
    6049{
    61     WebCoreFunctionWrapper *wrapper = [[WebCoreFunctionWrapper alloc] initWithFunction:function context:context];
    62     [wrapper performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:NO];
    63     [wrapper release];
     50    WebCoreMainThreadCaller *caller = [[WebCoreMainThreadCaller alloc] init];
     51    [caller performSelectorOnMainThread:@selector(call) withObject:nil waitUntilDone:NO];
     52    [caller release];
    6453}
    6554
  • trunk/WebCore/platform/qt/MainThreadQt.cpp

    r30522 r31063  
    3838namespace WebCore {
    3939
    40 class PerformFunctionEvent : public QEvent {
    41 public:
    42     static const int EventType = 723;
    43 
    44     PerformFunctionEvent(MainThreadFunction*, void* context);
    45 
    46     void invoke();
    47 
    48 private:
    49     MainThreadFunction* m_function;
    50     void* m_context;
    51 };
    52 
    5340class MainThreadInvoker : public QObject {
    5441    Q_OBJECT
     
    6047};
    6148
    62 PerformFunctionEvent::PerformFunctionEvent(MainThreadFunction* function, void* context)
    63     : QEvent(static_cast<QEvent::Type>(EventType))
    64     , m_function(function)
    65     , m_context(context)
    66 {
    67 }
    68 
    69 void PerformFunctionEvent::invoke()
    70 {
    71     m_function(m_context);
    72 }
    73 
    7449MainThreadInvoker::MainThreadInvoker()
    7550{
     
    7752}
    7853
    79 bool MainThreadInvoker::event(QEvent* event)
     54bool MainThreadInvoker::event(QEvent*)
    8055{
    81     if (event->type() == PerformFunctionEvent::EventType)
    82         static_cast<PerformFunctionEvent*>(event)->invoke();
    83 
    84     return QObject::event(event);
     56    dispatchFunctionsFromMainThread();
    8557}
    8658
     
    8860
    8961
    90 void callOnMainThread(MainThreadFunction* function, void* context)
     62void scheduleDispatchFunctionsOnMainThread()
    9163{
    92     QCoreApplication::postEvent(webkit_main_thread_invoker(), new PerformFunctionEvent(function, context));
     64    QCoreApplication::postEvent(webkit_main_thread_invoker(), 0);
    9365}
    9466
  • trunk/WebCore/platform/win/MainThreadWin.cpp

    r30522 r31063  
    3030#include "MainThread.h"
    3131
    32 #include "Logging.h"
    3332#include "Page.h"
    3433#include <windows.h>
     
    3635namespace WebCore {
    3736
    38 struct FunctionWithContext {
    39     MainThreadFunction* function;
    40     void* context;
    41     FunctionWithContext(MainThreadFunction* f = 0, void* c = 0) : function(f), context(c) { }
    42 };
    43 
    44 typedef Vector<FunctionWithContext> FunctionQueue;
    45 
    46 static HWND threadingWindowHandle = 0;
    47 static UINT threadingFiredMessage = 0;
     37static HWND threadingWindowHandle;
     38static UINT threadingFiredMessage;
    4839const LPCWSTR kThreadingWindowClassName = L"ThreadingWindowClass";
    49 static bool processingCustomThreadingMessage = false;
    50 
    51 static Mutex& functionQueueMutex()
    52 {
    53     static Mutex staticFunctionQueueMutex;
    54     return staticFunctionQueueMutex;
    55 }
    56 
    57 static FunctionQueue& functionQueue()
    58 {
    59     static FunctionQueue staticFunctionQueue;
    60     return staticFunctionQueue;
    61 }
    62 
    63 static void callFunctionsOnMainThread()
    64 {
    65     FunctionQueue queueCopy;
    66     {
    67         MutexLocker locker(functionQueueMutex());
    68         queueCopy.swap(functionQueue());
    69     }
    70 
    71     LOG(Threading, "Calling %u functions on the main thread", queueCopy.size());
    72     for (unsigned i = 0; i < queueCopy.size(); ++i)
    73         queueCopy[i].function(queueCopy[i].context);
    74 }
    7540
    7641LRESULT CALLBACK ThreadingWindowWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    7742{
    78     if (message == threadingFiredMessage) {
    79         processingCustomThreadingMessage = true;
    80         callFunctionsOnMainThread();
    81         processingCustomThreadingMessage = false;
    82     } else
     43    if (message == threadingFiredMessage)
     44        dispatchFunctionsFromMainThread();
     45    else
    8346        return DefWindowProc(hWnd, message, wParam, lParam);
    8447    return 0;
     
    10467    threadingFiredMessage = RegisterWindowMessage(L"com.apple.WebKit.MainThreadFired");
    10568}
    106    
    107 void callOnMainThread(MainThreadFunction* function, void* context)
     69
     70void scheduleDispatchFunctionsOnMainThread()
    10871{
    109     ASSERT(function);
    11072    ASSERT(threadingWindowHandle);
    111 
    112     if (processingCustomThreadingMessage)
    113         LOG(Threading, "callOnMainThread() called recursively.  Beware of nested PostMessage()s");
    114 
    115     {
    116         MutexLocker locker(functionQueueMutex());
    117         functionQueue().append(FunctionWithContext(function, context));
    118     }
    119 
    12073    PostMessage(threadingWindowHandle, threadingFiredMessage, 0, 0);
    12174}
  • trunk/WebCore/platform/wx/MainThreadWx.cpp

    r30522 r31063  
    2828
    2929#include "config.h"
     30#include "MainThread.h"
     31
    3032#include "NotImplemented.h"
    31 #include "MainThread.h"
    3233
    3334namespace WebCore {
    3435
    35 void callOnMainThread(MainThreadFunction*, void*)
     36void scheduleDispatchFunctionsOnMainThread()
    3637{
    3738    notImplemented();
Note: See TracChangeset for help on using the changeset viewer.