cannam@147: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors cannam@147: // Licensed under the MIT License: cannam@147: // cannam@147: // Permission is hereby granted, free of charge, to any person obtaining a copy cannam@147: // of this software and associated documentation files (the "Software"), to deal cannam@147: // in the Software without restriction, including without limitation the rights cannam@147: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cannam@147: // copies of the Software, and to permit persons to whom the Software is cannam@147: // furnished to do so, subject to the following conditions: cannam@147: // cannam@147: // The above copyright notice and this permission notice shall be included in cannam@147: // all copies or substantial portions of the Software. cannam@147: // cannam@147: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR cannam@147: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, cannam@147: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE cannam@147: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER cannam@147: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, cannam@147: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN cannam@147: // THE SOFTWARE. cannam@147: cannam@147: #ifndef KJ_THREAD_H_ cannam@147: #define KJ_THREAD_H_ cannam@147: cannam@147: #if defined(__GNUC__) && !KJ_HEADER_WARNINGS cannam@147: #pragma GCC system_header cannam@147: #endif cannam@147: cannam@147: #include "common.h" cannam@147: #include "function.h" cannam@147: #include "exception.h" cannam@147: cannam@147: namespace kj { cannam@147: cannam@147: class Thread { cannam@147: // A thread! Pass a lambda to the constructor, and it runs in the thread. The destructor joins cannam@147: // the thread. If the function throws an exception, it is rethrown from the thread's destructor cannam@147: // (if not unwinding from another exception). cannam@147: cannam@147: public: cannam@147: explicit Thread(Function func); cannam@147: KJ_DISALLOW_COPY(Thread); cannam@147: cannam@147: ~Thread() noexcept(false); cannam@147: cannam@147: #if !_WIN32 cannam@147: void sendSignal(int signo); cannam@147: // Send a Unix signal to the given thread, using pthread_kill or an equivalent. cannam@147: #endif cannam@147: cannam@147: void detach(); cannam@147: // Don't join the thread in ~Thread(). cannam@147: cannam@147: private: cannam@147: struct ThreadState { cannam@147: Function func; cannam@147: kj::Maybe exception; cannam@147: cannam@147: unsigned int refcount; cannam@147: // Owned by the parent thread and the child thread. cannam@147: cannam@147: void unref(); cannam@147: }; cannam@147: ThreadState* state; cannam@147: cannam@147: #if _WIN32 cannam@147: void* threadHandle; cannam@147: #else cannam@147: unsigned long long threadId; // actually pthread_t cannam@147: #endif cannam@147: bool detached = false; cannam@147: cannam@147: #if _WIN32 cannam@147: static unsigned long __stdcall runThread(void* ptr); cannam@147: #else cannam@147: static void* runThread(void* ptr); cannam@147: #endif cannam@147: }; cannam@147: cannam@147: } // namespace kj cannam@147: cannam@147: #endif // KJ_THREAD_H_