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