Commit 7f493695 authored by jorlow@chromium.org's avatar jorlow@chromium.org

Make the context that was passed to the ThreadFunction accessible.

https://bugs.webkit.org/show_bug.cgi?id=35379

Patch by Jochen Eisinger <jochen@chromium.org> on 2010-02-25
Reviewed by Jeremy Orlow.

When a database is opened, right now you
don't have any context from where it is opened. The problem is that
the actual calls that open a database go through the sqlite3 vfs
layer, so there's no easy way to pass this function down to to
platform/sql/chromium/SQLFileSystemChromium*.cpp

This patch will allow you to get from anywhere within webkit a pointer
to the Thread object that actually created the thread you're currently
on (in case of the database, this can be either a thread forked of
from the main thread or from a worker thread), and query the object
for context information.

* wtf/Threading.h:
* wtf/ThreadingNone.cpp:
(WTF::threadContext):
* wtf/ThreadingPthreads.cpp:
(WTF::):
(WTF::identifierByPthreadHandle):
(WTF::establishIdentifierForPthreadHandle):
(WTF::pthreadHandleForIdentifier):
(WTF::contextForIdentifier):
(WTF::createThreadInternal):
(WTF::currentThread):
(WTF::threadContext):
* wtf/ThreadingWin.cpp:
(WTF::):
(WTF::threadMap):
(WTF::storeThreadHandleByIdentifier):
(WTF::threadHandleForIdentifier):
(WTF::contextForIdentifier):
(WTF::createThreadInternal):
(WTF::threadContext):



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@55247 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 162895c0
2010-02-25 Jochen Eisinger <jochen@chromium.org>
Reviewed by Jeremy Orlow.
Make the context that was passed to the ThreadFunction accessible.
https://bugs.webkit.org/show_bug.cgi?id=35379
When a database is opened, right now you
don't have any context from where it is opened. The problem is that
the actual calls that open a database go through the sqlite3 vfs
layer, so there's no easy way to pass this function down to to
platform/sql/chromium/SQLFileSystemChromium*.cpp
This patch will allow you to get from anywhere within webkit a pointer
to the Thread object that actually created the thread you're currently
on (in case of the database, this can be either a thread forked of
from the main thread or from a worker thread), and query the object
for context information.
* wtf/Threading.h:
* wtf/ThreadingNone.cpp:
(WTF::threadContext):
* wtf/ThreadingPthreads.cpp:
(WTF::):
(WTF::identifierByPthreadHandle):
(WTF::establishIdentifierForPthreadHandle):
(WTF::pthreadHandleForIdentifier):
(WTF::contextForIdentifier):
(WTF::createThreadInternal):
(WTF::currentThread):
(WTF::threadContext):
* wtf/ThreadingWin.cpp:
(WTF::):
(WTF::threadMap):
(WTF::storeThreadHandleByIdentifier):
(WTF::threadHandleForIdentifier):
(WTF::contextForIdentifier):
(WTF::createThreadInternal):
(WTF::threadContext):
2010-02-25 Jeremy Orlow <jorlow@chromium.org>
Reverting to re-submit with better change log.
......
......@@ -127,6 +127,7 @@ ThreadIdentifier currentThread();
bool isMainThread();
int waitForThreadCompletion(ThreadIdentifier, void**);
void detachThread(ThreadIdentifier);
void* threadContext(ThreadIdentifier);
#if USE(PTHREADS)
typedef pthread_mutex_t PlatformMutex;
......
......@@ -41,6 +41,7 @@ int waitForThreadCompletion(ThreadIdentifier, void**) { return 0; }
void detachThread(ThreadIdentifier) { }
ThreadIdentifier currentThread() { return ThreadIdentifier(); }
bool isMainThread() { return true; }
void* threadContext(ThreadIdentifier) { return 0; }
Mutex::Mutex() { }
Mutex::~Mutex() { }
......
......@@ -53,7 +53,12 @@
namespace WTF {
typedef HashMap<ThreadIdentifier, pthread_t> ThreadMap;
typedef struct {
pthread_t handle;
void* context;
} ThreadInfo;
typedef HashMap<ThreadIdentifier, ThreadInfo> ThreadMap;
static Mutex* atomicallyInitializedStaticMutex;
......@@ -105,14 +110,14 @@ static ThreadIdentifier identifierByPthreadHandle(const pthread_t& pthreadHandle
ThreadMap::iterator i = threadMap().begin();
for (; i != threadMap().end(); ++i) {
if (pthread_equal(i->second, pthreadHandle))
if (pthread_equal(i->second.handle, pthreadHandle))
return i->first;
}
return 0;
}
static ThreadIdentifier establishIdentifierForPthreadHandle(const pthread_t& pthreadHandle)
static ThreadIdentifier establishIdentifierForPthreadHandle(const pthread_t& pthreadHandle, void* context)
{
ASSERT(!identifierByPthreadHandle(pthreadHandle));
......@@ -120,7 +125,10 @@ static ThreadIdentifier establishIdentifierForPthreadHandle(const pthread_t& pth
static ThreadIdentifier identifierCount = 1;
threadMap().add(identifierCount, pthreadHandle);
ThreadInfo info;
info.handle = pthreadHandle;
info.context = context;
threadMap().add(identifierCount, info);
return identifierCount++;
}
......@@ -129,9 +137,17 @@ static pthread_t pthreadHandleForIdentifier(ThreadIdentifier id)
{
MutexLocker locker(threadMapMutex());
return threadMap().get(id);
return threadMap().get(id).handle;
}
static void* contextForIdentifier(ThreadIdentifier id)
{
MutexLocker locker(threadMapMutex());
return threadMap().get(id).context;
}
void clearPthreadHandleForIdentifier(ThreadIdentifier id)
{
MutexLocker locker(threadMapMutex());
......@@ -174,7 +190,7 @@ ThreadIdentifier createThreadInternal(ThreadFunction entryPoint, void* data, con
delete threadData;
return 0;
}
return establishIdentifierForPthreadHandle(threadHandle);
return establishIdentifierForPthreadHandle(threadHandle, data);
}
#else
ThreadIdentifier createThreadInternal(ThreadFunction entryPoint, void* data, const char*)
......@@ -185,7 +201,7 @@ ThreadIdentifier createThreadInternal(ThreadFunction entryPoint, void* data, con
return 0;
}
return establishIdentifierForPthreadHandle(threadHandle);
return establishIdentifierForPthreadHandle(threadHandle, data);
}
#endif
......@@ -235,7 +251,7 @@ ThreadIdentifier currentThread()
return id;
// Not a WTF-created thread, ThreadIdentifier is not established yet.
id = establishIdentifierForPthreadHandle(pthread_self());
id = establishIdentifierForPthreadHandle(pthread_self(), 0);
ThreadIdentifierData::initialize(id);
return id;
}
......@@ -249,6 +265,11 @@ bool isMainThread()
#endif
}
void* threadContext(ThreadIdentifier id)
{
return contextForIdentifier(id);
}
Mutex::Mutex()
{
pthread_mutex_init(&m_mutex, NULL);
......
......@@ -118,6 +118,11 @@ typedef struct tagTHREADNAME_INFO {
} THREADNAME_INFO;
#pragma pack(pop)
typedef struct {
HANDLE handle;
void* context;
} ThreadInfo;
void initializeCurrentThreadInternal(const char* szThreadName)
{
THREADNAME_INFO info;
......@@ -165,23 +170,32 @@ void initializeThreading()
}
}
static HashMap<DWORD, HANDLE>& threadMap()
static HashMap<DWORD, ThreadInfo>& threadMap()
{
static HashMap<DWORD, HANDLE> map;
static HashMap<DWORD, ThreadInfo> map;
return map;
}
static void storeThreadHandleByIdentifier(DWORD threadID, HANDLE threadHandle)
static void storeThreadHandleByIdentifier(DWORD threadID, HANDLE threadHandle, void* context)
{
MutexLocker locker(threadMapMutex());
ASSERT(!threadMap().contains(threadID));
threadMap().add(threadID, threadHandle);
ThreadInfo info;
info.handle = threadHandle;
info.context = context;
threadMap().add(threadID, info);
}
static HANDLE threadHandleForIdentifier(ThreadIdentifier id)
{
MutexLocker locker(threadMapMutex());
return threadMap().get(id);
return threadMap().get(id).handle;
}
static void* contextForIdentifier(ThreadIdentifier id)
{
MutexLocker locker(threadMapMutex());
return threadMap().get(id).context;
}
static void clearThreadHandleForIdentifier(ThreadIdentifier id)
......@@ -237,7 +251,7 @@ ThreadIdentifier createThreadInternal(ThreadFunction entryPoint, void* data, con
}
threadID = static_cast<ThreadIdentifier>(threadIdentifier);
storeThreadHandleByIdentifier(threadIdentifier, threadHandle);
storeThreadHandleByIdentifier(threadIdentifier, threadHandle, data);
return threadID;
}
......@@ -280,6 +294,11 @@ bool isMainThread()
return currentThread() == mainThreadIdentifier;
}
void* threadContext(ThreadIdentifier threadID)
{
return contextForIdentifier(threadID);
}
Mutex::Mutex()
{
m_mutex.m_recursionCount = 0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment