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