No subject


Mon Jul 16 08:45:43 PDT 2012


Group is introduced to specify the group of a task. When posting a task, we=
 can specify the task group where the task belongs. See TaskThread::postTas=
k(const Function<void()>& task, PassRefPtr<TaskGroup> group) method below.

class TaskGroup : public ThreadSafeRefCounted<TaskGroup> { };


3.3 Thread

TaskThread is our new abstraction for task thread. FileThread, DatabaseThre=
ad and other task threads reuse the code by inheriting from TaskThread.

class TaskThread : public ThreadSafeRefCounted<TaskThread> {
public:
    ~TaskThread();
    void start();
    void stop();

    // Schedule a task to be executed by the task thread.
    void postTask(const Function<void()>& task);
    // Schedule a task to be executed by the task thread and also specify t=
he task group.
    // Tasks scheduled with this method can be cancelled by removeTasks() m=
ethod.
    void postTask(const Function<void()>& task, PassRefPtr<TaskGroup> group=
);

    // Schedule a task to be immediately executed by the task thread.
    void postImmediateTask(PassOwnPtr<TaskThreadTask> task);
    // Schedule a task to be executed by the task thread and also specify t=
he task group.
    // Tasks scheduled with this method can be cancelled by removeTasks() m=
ethod.
    void postImmediateTask(const Function<void()>& task, PassRefPtr<TaskGro=
up> group);

    // Cancel pending tasks that belong to the given group.
    void removeTasks(PassRefPtr<TaskGroup> group);

protected:
    TaskThread();
    // Subclasses can override this method to specify an action to be execu=
ted
    // on thread termination
    virtual void onRunLoopTerminated();
};


3.4 Callback to ScriptExecutionContext

ScriptExecutionContext::postTask() now takes a WTF::Function as an argument=
 because WTF::Function replaced CrossThreadTask.

class ScriptExecutionContext :
   public SecurityContext, public Supplementable<ScriptExecutionContext> {
public:
=E2=80=A6
   virtual void postTask(const Function<void()>&) =3D 0;
...
};

There is a small change in usage. CrossThreadTask does not bind the pointer=
 to ScriptExecutionContext because it is automatically passed as the first =
argument when the task is invoked. However, since WTF::Function does not su=
pport partial application, the pointer to ScriptExecutionContext must be bo=
und as the first argument when binding parameters to a task. This is possib=
le because task threads already know the pointer. See the example below.

void AsyncFileStream::openForReadOnFileThread(const String& path, long long=
 offset, long long length)
{
   bool success =3D m_stream->openForRead(path, offset, length);
   m_context->postTask(createCallbackTask(&didOpen, AllowCrossThreadAccess(=
this), success));
}

=3D>

void AsyncFileStream::openForReadOnFileThread(const String& path, long long=
 offset, long long length)
{
   bool success =3D m_stream->openForRead(path, offset, length);
   m_context->postTask(bind(&didOpen, m_context, this, success));
}


3.5 CrossThreadCopyable

If the parameter type is a subclass of CrossThreadCopyable, the parameter i=
s copied by calling crossThreadCopy().

template<typename Orig, New>
class CrossThreadCopyable<Orig, New> {
public:
    virtual New crossThreadCopy();
}

AllowCrossThreadAccess is no longer needed because WTF::Function ref counts=
 only the first parameter.


3.6 deepCopy

If a class can=E2=80=99t be a subclass of CrossThreadCopyable, deepCopy() c=
an be used to pass thread-unsafe instances by calling custom copy methods. =
For example, we can pass String and KURL instances thread-safely by wrappin=
g them with deepCopy. For a string s, deepCopy(s) returns s.isolatedCopy().

void LocalFileSystem::readFileSystem(ScriptExecutionContext* context, FileS=
ystemType, PassOwnPtr<AsyncFileSystemCallbacks> callbacks, FileSystemSynchr=
onousType)
{
    context->postTask(bind(&openFileSystem, context, deepCopy(fileSystemBas=
ePath()), deepCopy(fcontext->securityOrigin()->databaseIdentifier()), false=
, callbacks));
}


4. Plan

This proposal does not add any new mechanism except for a few minor changes=
 in WTF::Function. It simply removes duplicated code and extracts common co=
de into reusable components. Once we have this refactoring done successfull=
y, we can move on to fancier cross thread communication mechanism.

Before I take an initiative to work hard on this proposal, I want to hear o=
pinions from reviewers and committers who understand WebKit threads very we=
ll. Also if you have any concerns on implementation level details, please l=
et me know before I struggle with C++ template hacks :)

--=20
Configure bugmail: https://bugs.webkit.org/userprefs.cgi?tab=3Demail
------- You are receiving this mail because: -------
You are the assignee for the bug.=


More information about the webkit-unassigned mailing list