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