Merge branch '1.x' of ssh://authdev.it.ohio-state.edu/~scantor/git/cpp-xmltooling...
[shibboleth/cpp-xmltooling.git] / xmltooling / util / Win32Threads.cpp
index 2613c80..3f64975 100644 (file)
@@ -1,29 +1,35 @@
-/*
- *  Copyright 2001-2007 Internet2
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
  *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * UCAID licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the
+ * License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
  */
 
 /**
  * Win32Threads.cpp
  *
- * Thread and locking wrappers for Win32 platforms
+ * Thread and locking wrappers for Win32 platforms.
  */
 
 #include "internal.h"
 #include "logging.h"
 #include "util/Threads.h"
 
+#include <algorithm>
+
 #ifndef WIN32
 # error "This implementation is for WIN32 platforms."
 #endif
@@ -94,13 +100,13 @@ namespace xmltooling {
     private:
         HANDLE thread_id;
     public:
-        ThreadImpl(void* (*start_routine)(void*), void* arg) : thread_id(0) {
+        ThreadImpl(void* (*start_routine)(void*), void* arg, size_t stacksize) : thread_id(0) {
             thread_id=CreateThread(
                 0, // security attributes
-                0, // use default stack size, maybe this should be setable
+                stacksize, // 0 just means the default size anyway
                 (LPTHREAD_START_ROUTINE ) start_routine,
                 arg,
-                0, // flags, default is ignore stacksize and don't create suspended which is what we want
+                0, // flags, default is don't create suspended which is what we want
                 0);
             if (thread_id==0) {
                 map_windows_error_status_to_pthreads();
@@ -145,33 +151,24 @@ namespace xmltooling {
 
     class XMLTOOL_DLLLOCAL MutexImpl : public Mutex {
     private:
-        HANDLE mhandle;
+        CRITICAL_SECTION mhandle;
     public:
-        MutexImpl() : mhandle(CreateMutex(0,false,0)) {
-            if (mhandle==0) {
-                map_windows_error_status_to_pthreads();
-                throw ThreadingException("Mutex creation failed.");
-            }
+        MutexImpl() {
+            InitializeCriticalSection(&mhandle);
         }
 
         ~MutexImpl() {
-            if((mhandle!=0) && (!CloseHandle(mhandle)))
-                map_windows_error_status_to_pthreads();
+            DeleteCriticalSection(&mhandle);
         }
 
         int lock() {
-            int rc=WaitForSingleObject(mhandle,INFINITE);
-            switch(rc) {
-                case WAIT_ABANDONED:
-                case WAIT_OBJECT_0:
-                    return 0;
-                default:
-                    return map_windows_error_status_to_pthreads();
-            }
+            EnterCriticalSection(&mhandle);
+            return 0;
         }
 
         int unlock() {
-            return map_windows_error_status_to_pthreads(ReleaseMutex(mhandle));
+            LeaveCriticalSection(&mhandle);
+            return 0;
         }
     };
 
@@ -342,30 +339,44 @@ namespace xmltooling {
 
     class XMLTOOL_DLLLOCAL ThreadKeyImpl : public ThreadKey {
     private:
-        //destroy_hook_type destroy_hook;
+        destroy_hook_type destroy_hook;
         DWORD key;
-
+        static critical_section cs;
+        static set<ThreadKeyImpl*> m_keys;
+        friend class ThreadKey;
     public:
-        ThreadKeyImpl(void (*destroy_fcn)(void*)) { // : destroy_hook(destroy_fcn) {
-            if (destroy_fcn)
-                throw ThreadingException("TLS destructor function not supported.");
+        ThreadKeyImpl(void (*destroy_fcn)(void*)) : destroy_hook(destroy_fcn) {
             key=TlsAlloc();
+            if (destroy_fcn) {
+                with_crit_section wcs(cs);
+                m_keys.insert(this);
+            }
         };
 
         virtual ~ThreadKeyImpl() {
-            //if (destroy_hook)
-            //    destroy_hook(TlsGetValue(key));
+            if (destroy_hook) {
+                destroy_hook(TlsGetValue(key));
+                with_crit_section wcs(cs);
+                m_keys.erase(this);
+            }
             TlsFree(key);
         }
 
         int setData(void* data) {
-            TlsSetValue(key,data);
+            TlsSetValue(key, data);
             return 0;
         }
 
         void* getData() const {
             return TlsGetValue(key);
         }
+
+        void onDetach() const {
+            if (destroy_hook) {
+                destroy_hook(TlsGetValue(key));
+                TlsSetValue(key, nullptr);
+            }
+        }
     };
 
 };
@@ -374,9 +385,9 @@ namespace xmltooling {
 // public "static" creation functions
 //
 
-Thread* Thread::create(void* (*start_routine)(void*), void* arg)
+Thread* Thread::create(void* (*start_routine)(void*), void* arg, size_t stacksize)
 {
-    return new ThreadImpl(start_routine, arg);
+    return new ThreadImpl(start_routine, arg, stacksize);
 }
 
 void Thread::exit(void* return_val)
@@ -404,7 +415,16 @@ RWLock * RWLock::create()
     return new RWLockImpl();
 }
 
+critical_section ThreadKeyImpl::cs;
+set<ThreadKeyImpl*> ThreadKeyImpl::m_keys;
+
 ThreadKey* ThreadKey::create (void (*destroy_fcn)(void*))
 {
     return new ThreadKeyImpl(destroy_fcn);
 }
+
+void ThreadKey::onDetach()
+{
+    with_crit_section wcs(ThreadKeyImpl::cs);
+    for_each(ThreadKeyImpl::m_keys.begin(), ThreadKeyImpl::m_keys.end(), mem_fun<void,ThreadKeyImpl>(&ThreadKeyImpl::onDetach));
+}