Title: [124823] trunk/Source
Revision
124823
Author
par...@webkit.org
Date
2012-08-06 17:29:14 -0700 (Mon, 06 Aug 2012)

Log Message

[WIN] Remove dependency on pthread from MachineStackMarker
https://bugs.webkit.org/show_bug.cgi?id=68429

Reviewed by Geoffrey Garen.

Windows has no support for calling a destructor for thread specific data.
Since we need more control over creating and deleting thread specific keys
we can not simply extend WTF::ThreadSpecific with this functionality.

All thread specific keys created via the new API get stored in a list.
After a thread function finished we iterate over this list and call
the registered destructor for every item if needed.

Source/_javascript_Core:

* heap/MachineStackMarker.cpp:  Use the new functions instead of pthread directly.
(JSC::MachineThreads::~MachineThreads):
(JSC::MachineThreads::makeUsableFromMultipleThreads):
(JSC::MachineThreads::addCurrentThread):
* heap/MachineStackMarker.h:
(MachineThreads):

Source/WTF:

* wtf/ThreadSpecific.h:
(WTF):
(WTF::threadSpecificKeyCreate): Added wrapper around pthread_key_create.
(WTF::threadSpecificKeyDelete): Added wrapper around pthread_key_delete.
(WTF::threadSpecificSet): Added wrapper around pthread_setspecific.
(WTF::threadSpecificGet): Added wrapper around pthread_getspecific.
* wtf/ThreadSpecificWin.cpp:

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (124822 => 124823)


--- trunk/Source/_javascript_Core/ChangeLog	2012-08-07 00:17:47 UTC (rev 124822)
+++ trunk/Source/_javascript_Core/ChangeLog	2012-08-07 00:29:14 UTC (rev 124823)
@@ -1,5 +1,27 @@
 2012-08-06  Patrick Gansterer  <par...@webkit.org>
 
+        [WIN] Remove dependency on pthread from MachineStackMarker
+        https://bugs.webkit.org/show_bug.cgi?id=68429
+
+        Reviewed by Geoffrey Garen.
+
+        Windows has no support for calling a destructor for thread specific data.
+        Since we need more control over creating and deleting thread specific keys
+        we can not simply extend WTF::ThreadSpecific with this functionality.
+
+        All thread specific keys created via the new API get stored in a list.
+        After a thread function finished we iterate over this list and call
+        the registered destructor for every item if needed.
+
+        * heap/MachineStackMarker.cpp:  Use the new functions instead of pthread directly.
+        (JSC::MachineThreads::~MachineThreads):
+        (JSC::MachineThreads::makeUsableFromMultipleThreads):
+        (JSC::MachineThreads::addCurrentThread):
+        * heap/MachineStackMarker.h:
+        (MachineThreads):
+
+2012-08-06  Patrick Gansterer  <par...@webkit.org>
+
         Unify JSC date and time formating functions
         https://bugs.webkit.org/show_bug.cgi?id=92282
 

Modified: trunk/Source/_javascript_Core/heap/MachineStackMarker.cpp (124822 => 124823)


--- trunk/Source/_javascript_Core/heap/MachineStackMarker.cpp	2012-08-07 00:17:47 UTC (rev 124822)
+++ trunk/Source/_javascript_Core/heap/MachineStackMarker.cpp	2012-08-07 00:29:14 UTC (rev 124823)
@@ -141,10 +141,8 @@
 
 MachineThreads::~MachineThreads()
 {
-    if (m_threadSpecific) {
-        int error = pthread_key_delete(m_threadSpecific);
-        ASSERT_UNUSED(error, !error);
-    }
+    if (m_threadSpecific)
+        threadSpecificKeyDelete(m_threadSpecific);
 
     MutexLocker registeredThreadsLock(m_registeredThreadsMutex);
     for (Thread* t = m_registeredThreads; t;) {
@@ -181,19 +179,17 @@
     if (m_threadSpecific)
         return;
 
-    int error = pthread_key_create(&m_threadSpecific, removeThread);
-    if (error)
-        CRASH();
+    threadSpecificKeyCreate(&m_threadSpecific, removeThread);
 }
 
 void MachineThreads::addCurrentThread()
 {
     ASSERT(!m_heap->globalData()->exclusiveThread || m_heap->globalData()->exclusiveThread == currentThread());
 
-    if (!m_threadSpecific || pthread_getspecific(m_threadSpecific))
+    if (!m_threadSpecific || threadSpecificGet(m_threadSpecific))
         return;
 
-    pthread_setspecific(m_threadSpecific, this);
+    threadSpecificSet(m_threadSpecific, this);
     Thread* thread = new Thread(getCurrentPlatformThread(), wtfThreadData().stack().origin());
 
     MutexLocker lock(m_registeredThreadsMutex);

Modified: trunk/Source/_javascript_Core/heap/MachineStackMarker.h (124822 => 124823)


--- trunk/Source/_javascript_Core/heap/MachineStackMarker.h	2012-08-07 00:17:47 UTC (rev 124822)
+++ trunk/Source/_javascript_Core/heap/MachineStackMarker.h	2012-08-07 00:29:14 UTC (rev 124823)
@@ -22,8 +22,8 @@
 #ifndef MachineThreads_h
 #define MachineThreads_h
 
-#include <pthread.h>
 #include <wtf/Noncopyable.h>
+#include <wtf/ThreadSpecific.h>
 #include <wtf/ThreadingPrimitives.h>
 
 namespace JSC {
@@ -55,7 +55,7 @@
         Heap* m_heap;
         Mutex m_registeredThreadsMutex;
         Thread* m_registeredThreads;
-        pthread_key_t m_threadSpecific;
+        WTF::ThreadSpecificKey m_threadSpecific;
     };
 
 } // namespace JSC

Modified: trunk/Source/WTF/ChangeLog (124822 => 124823)


--- trunk/Source/WTF/ChangeLog	2012-08-07 00:17:47 UTC (rev 124822)
+++ trunk/Source/WTF/ChangeLog	2012-08-07 00:29:14 UTC (rev 124823)
@@ -1,3 +1,26 @@
+2012-08-06  Patrick Gansterer  <par...@webkit.org>
+
+        [WIN] Remove dependency on pthread from MachineStackMarker
+        https://bugs.webkit.org/show_bug.cgi?id=68429
+
+        Reviewed by Geoffrey Garen.
+
+        Windows has no support for calling a destructor for thread specific data.
+        Since we need more control over creating and deleting thread specific keys
+        we can not simply extend WTF::ThreadSpecific with this functionality.
+
+        All thread specific keys created via the new API get stored in a list.
+        After a thread function finished we iterate over this list and call
+        the registered destructor for every item if needed.
+
+        * wtf/ThreadSpecific.h:
+        (WTF):
+        (WTF::threadSpecificKeyCreate): Added wrapper around pthread_key_create.
+        (WTF::threadSpecificKeyDelete): Added wrapper around pthread_key_delete.
+        (WTF::threadSpecificSet): Added wrapper around pthread_setspecific.
+        (WTF::threadSpecificGet): Added wrapper around pthread_getspecific.
+        * wtf/ThreadSpecificWin.cpp:
+
 2012-08-04  No'am Rosenthal  <noam.rosent...@nokia.com>
 
         [Qt] UI_SIDE_COMPOSITING code has confusing names

Modified: trunk/Source/WTF/wtf/ThreadSpecific.h (124822 => 124823)


--- trunk/Source/WTF/wtf/ThreadSpecific.h	2012-08-07 00:17:47 UTC (rev 124822)
+++ trunk/Source/WTF/wtf/ThreadSpecific.h	2012-08-07 00:29:14 UTC (rev 124823)
@@ -1,6 +1,7 @@
 /*
  * Copyright (C) 2008 Apple Inc. All rights reserved.
  * Copyright (C) 2009 Jian Li <jia...@chromium.org>
+ * Copyright (C) 2012 Patrick Gansterer <par...@paroga.com>
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -102,6 +103,33 @@
 };
 
 #if USE(PTHREADS)
+
+typedef pthread_key_t ThreadSpecificKey;
+
+inline void threadSpecificKeyCreate(ThreadSpecificKey* key, void (*destructor)(void *))
+{
+    int error = pthread_key_create(key, destructor);
+    if (error)
+        CRASH();
+}
+
+inline void threadSpecificKeyDelete(ThreadSpecificKey key)
+{
+    int error = pthread_key_delete(key);
+    if (error)
+        CRASH();
+}
+
+inline void threadSpecificSet(ThreadSpecificKey key, void* value)
+{
+    pthread_setspecific(key, value);
+}
+
+inline void* threadSpecificGet(ThreadSpecificKey key)
+{
+    return pthread_getspecific(key);
+}
+
 template<typename T>
 inline ThreadSpecific<T>::ThreadSpecific()
 {
@@ -139,6 +167,14 @@
 WTF_EXPORT_PRIVATE long& tlsKeyCount();
 WTF_EXPORT_PRIVATE DWORD* tlsKeys();
 
+class PlatformThreadSpecificKey;
+typedef PlatformThreadSpecificKey* ThreadSpecificKey;
+
+void threadSpecificKeyCreate(ThreadSpecificKey*, void (*)(void *));
+void threadSpecificKeyDelete(ThreadSpecificKey);
+void threadSpecificSet(ThreadSpecificKey, void*);
+void* threadSpecificGet(ThreadSpecificKey);
+
 template<typename T>
 inline ThreadSpecific<T>::ThreadSpecific()
     : m_index(-1)

Modified: trunk/Source/WTF/wtf/ThreadSpecificWin.cpp (124822 => 124823)


--- trunk/Source/WTF/wtf/ThreadSpecificWin.cpp	2012-08-07 00:17:47 UTC (rev 124822)
+++ trunk/Source/WTF/wtf/ThreadSpecificWin.cpp	2012-08-07 00:29:14 UTC (rev 124823)
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2009 Jian Li <jia...@chromium.org>
+ * Copyright (C) 2012 Patrick Gansterer <par...@paroga.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -19,15 +20,61 @@
  */
 
 #include "config.h"
-
 #include "ThreadSpecific.h"
 
-#if USE(PTHREADS)
-#error This file should not be compiled by ports that do not use Windows native ThreadSpecific implementation.
-#endif
+#include "StdLibExtras.h"
+#include "ThreadingPrimitives.h"
+#include <wtf/DoublyLinkedList.h>
 
+#if !USE(PTHREADS)
+
 namespace WTF {
 
+static DoublyLinkedList<PlatformThreadSpecificKey>& destructorsList()
+{
+    DEFINE_STATIC_LOCAL(DoublyLinkedList<PlatformThreadSpecificKey>, staticList, ());
+    return staticList;
+}
+
+static Mutex& destructorsMutex()
+{
+    DEFINE_STATIC_LOCAL(Mutex, staticMutex, ());
+    return staticMutex;
+}
+
+class PlatformThreadSpecificKey : public DoublyLinkedListNode<PlatformThreadSpecificKey> {
+public:
+    friend class DoublyLinkedListNode<PlatformThreadSpecificKey>;
+
+    PlatformThreadSpecificKey(void (*destructor)(void *))
+        : m_destructor(destructor)
+    {
+        m_tlsKey = TlsAlloc();
+        if (m_tlsKey == TLS_OUT_OF_INDEXES)
+            CRASH();
+    }
+
+    ~PlatformThreadSpecificKey()
+    {
+        TlsFree(m_tlsKey);
+    }
+
+    void setValue(void* data) { TlsSetValue(m_tlsKey, data); }
+    void* value() { return TlsGetValue(m_tlsKey); }
+
+    void callDestructor()
+    {
+       if (void* data = ""
+            m_destructor(data);
+    }
+
+private:
+    void (*m_destructor)(void *);
+    DWORD m_tlsKey;
+    PlatformThreadSpecificKey* m_prev;
+    PlatformThreadSpecificKey* m_next;
+};
+
 long& tlsKeyCount()
 {
     static long count;
@@ -40,6 +87,31 @@
     return keys;
 }
 
+void threadSpecificKeyCreate(ThreadSpecificKey* key, void (*destructor)(void *))
+{
+    *key = new PlatformThreadSpecificKey(destructor);
+
+    MutexLocker locker(destructorsMutex());
+    destructorsList().push(*key);
+}
+
+void threadSpecificKeyDelete(ThreadSpecificKey key)
+{
+    MutexLocker locker(destructorsMutex());
+    destructorsList().remove(key);
+    delete key;
+}
+
+void threadSpecificSet(ThreadSpecificKey key, void* data)
+{
+    key->setValue(data);
+}
+
+void* threadSpecificGet(ThreadSpecificKey key)
+{
+    return key->value();
+}
+
 void ThreadSpecificThreadExit()
 {
     for (long i = 0; i < tlsKeyCount(); i++) {
@@ -48,6 +120,16 @@
         if (data)
             data->destructor(data);
     }
+
+    MutexLocker locker(destructorsMutex());
+    PlatformThreadSpecificKey* key = destructorsList().head();
+    while (key) {
+        PlatformThreadSpecificKey* nextKey = key->next();
+        key->callDestructor();
+        key = nextKey;
+    }
 }
 
 } // namespace WTF
+
+#endif // !USE(PTHREADS)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to