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