Ignore:
Timestamp:
Jul 29, 2008, 9:11:00 AM (17 years ago)
Author:
Adam Roben
Message:

Add support for setting thread names on Windows

JavaScriptCore:

Add support for setting thread names on Windows

These thread names make it much easier to identify particular threads
in Visual Studio's Threads panel.

WTF::createThread now takes a const char* representing the thread's
name. On Windows, we throw a special exception to set this string as
the thread's name. Other platforms do nothing with this name for now.

Reviewed by Anders Carlsson.

  • JavaScriptCore.exp: Export the new version of createThread that takes 3 arguments (the old one continues to be exported for backward compatibility).
  • wtf/Threading.h: Add a threadName argument to createThread.
  • wtf/ThreadingGtk.cpp: (WTF::createThread):
  • wtf/ThreadingNone.cpp: (WTF::createThread): Updated for function signature change.
  • wtf/ThreadingPthreads.cpp: (WTF::createThread): Updated for function signature change. We keep around the old 2-argument version of createThread for backward compatibility.
  • wtf/ThreadingWin.cpp: (WTF::setThreadName): Added. This function's implementation came from MSDN. (WTF::initializeThreading): Set the name of the main thread. (WTF::createThread): Call setThreadName. We keep around the old 2-argument version of createThread for backward compatibility.

WebCore:

Add names for WebCore's threads

Reviewed by Anders Carlsson.

  • loader/icon/IconDatabase.cpp: (WebCore::IconDatabase::open):
  • storage/DatabaseThread.cpp: (WebCore::DatabaseThread::start):
  • storage/LocalStorageThread.cpp: (WebCore::LocalStorageThread::start): Pass in names to createThread.
  • platform/network/cf/ResourceHandleCFNet.cpp: (WebCore::runLoaderThread): (WebCore::ResourceHandle::loaderRunLoop): Changed to use WTF::createThread.

WebKit/win:

Export the new version of createThread

Reviewed by Anders Carlsson.

  • WebKit.vcproj/WebKit.def:
  • WebKit.vcproj/WebKit_debug.def: Also moved the old version of createThread into the deprecated section.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/ThreadingWin.cpp

    r31871 r35419  
    7878namespace WTF {
    7979
     80// MS_VC_EXCEPTION, THREADNAME_INFO, and setThreadName all come from <http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx>.
     81static const DWORD MS_VC_EXCEPTION = 0x406D1388;
     82
     83#pragma pack(push, 8)
     84typedef struct tagTHREADNAME_INFO {
     85    DWORD dwType; // must be 0x1000
     86    LPCSTR szName; // pointer to name (in user addr space)
     87    DWORD dwThreadID; // thread ID (-1=caller thread)
     88    DWORD dwFlags; // reserved for future use, must be zero
     89} THREADNAME_INFO;
     90#pragma pack(pop)
     91
     92static void setThreadName(DWORD dwThreadID, LPCSTR szThreadName)
     93{
     94    THREADNAME_INFO info;
     95    info.dwType = 0x1000;
     96    info.szName = szThreadName;
     97    info.dwThreadID = dwThreadID;
     98    info.dwFlags = 0;
     99
     100    __try {
     101        RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), reinterpret_cast<ULONG_PTR*>(&info));
     102    } __except (EXCEPTION_CONTINUE_EXECUTION) {
     103    }
     104}
     105
    80106Mutex* atomicallyInitializedStaticMutex;
    81107
     
    96122        initializeMainThread();
    97123        mainThreadIdentifier = currentThread();
     124        setThreadName(mainThreadIdentifier, "Main Thread");
    98125    }
    99126}
     
    146173}
    147174
    148 ThreadIdentifier createThread(ThreadFunction entryPoint, void* data)
    149 {
     175ThreadIdentifier createThread(ThreadFunction entryPoint, void* data, const char* threadName)
     176{
     177    // Visual Studio has a 31-character limit on thread names. Longer names will
     178    // be truncated silently, but we'd like callers to know about the limit.
     179    ASSERT_ARG(szThreadName, strlen(szThreadName) <= 31);
     180
    150181    unsigned threadIdentifier = 0;
    151182    ThreadIdentifier threadID = 0;
     
    157188    }
    158189
     190    if (threadName)
     191        setThreadName(threadIdentifier, threadName);
     192
    159193    threadID = static_cast<ThreadIdentifier>(threadIdentifier);
    160194    storeThreadHandleByIdentifier(threadIdentifier, threadHandle);
    161195
    162196    return threadID;
     197}
     198
     199// This function is deprecated but needs to be kept around for backward
     200// compatibility. Use the 3-argument version of createThread above.
     201ThreadIdentifier createThread(ThreadFunction entryPoint, void* data)
     202{
     203    return createThread(entryPoint, data, 0);
    163204}
    164205
Note: See TracChangeset for help on using the changeset viewer.