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