annotate win64-msvc/include/kj/thread.h @ 53:e1712f7d74a4

Rebuild with DW2 exception handling to match Qt
author Chris Cannam
date Thu, 27 Oct 2016 10:26:57 +0100
parents d93140aac40b
children 0f2d93caa50c
rev   line source
Chris@47 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
Chris@47 2 // Licensed under the MIT License:
Chris@47 3 //
Chris@47 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
Chris@47 5 // of this software and associated documentation files (the "Software"), to deal
Chris@47 6 // in the Software without restriction, including without limitation the rights
Chris@47 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Chris@47 8 // copies of the Software, and to permit persons to whom the Software is
Chris@47 9 // furnished to do so, subject to the following conditions:
Chris@47 10 //
Chris@47 11 // The above copyright notice and this permission notice shall be included in
Chris@47 12 // all copies or substantial portions of the Software.
Chris@47 13 //
Chris@47 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chris@47 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Chris@47 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Chris@47 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Chris@47 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Chris@47 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Chris@47 20 // THE SOFTWARE.
Chris@47 21
Chris@47 22 #ifndef KJ_THREAD_H_
Chris@47 23 #define KJ_THREAD_H_
Chris@47 24
Chris@47 25 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
Chris@47 26 #pragma GCC system_header
Chris@47 27 #endif
Chris@47 28
Chris@47 29 #include "common.h"
Chris@47 30 #include "function.h"
Chris@47 31 #include "exception.h"
Chris@47 32
Chris@47 33 namespace kj {
Chris@47 34
Chris@47 35 class Thread {
Chris@47 36 // A thread! Pass a lambda to the constructor, and it runs in the thread. The destructor joins
Chris@47 37 // the thread. If the function throws an exception, it is rethrown from the thread's destructor
Chris@47 38 // (if not unwinding from another exception).
Chris@47 39
Chris@47 40 public:
Chris@47 41 explicit Thread(Function<void()> func);
Chris@47 42 KJ_DISALLOW_COPY(Thread);
Chris@47 43
Chris@47 44 ~Thread() noexcept(false);
Chris@47 45
Chris@47 46 #if !_WIN32
Chris@47 47 void sendSignal(int signo);
Chris@47 48 // Send a Unix signal to the given thread, using pthread_kill or an equivalent.
Chris@47 49 #endif
Chris@47 50
Chris@47 51 void detach();
Chris@47 52 // Don't join the thread in ~Thread().
Chris@47 53 //
Chris@47 54 // TODO(soon): Currently broken: the thread uses the Thread objects during its execution; instead
Chris@47 55 // the Thread object and the thread itself will need to share a refcounted object.
Chris@47 56
Chris@47 57 private:
Chris@47 58 struct ThreadState {
Chris@47 59 Function<void()> func;
Chris@47 60 kj::Maybe<kj::Exception> exception;
Chris@47 61
Chris@47 62 unsigned int refcount;
Chris@47 63 // Owned by the parent thread and the child thread.
Chris@47 64
Chris@47 65 void unref();
Chris@47 66 };
Chris@47 67 ThreadState* state;
Chris@47 68
Chris@47 69 #if _WIN32
Chris@47 70 void* threadHandle;
Chris@47 71 #else
Chris@47 72 unsigned long long threadId; // actually pthread_t
Chris@47 73 #endif
Chris@47 74 bool detached = false;
Chris@47 75
Chris@47 76 #if _WIN32
Chris@47 77 static unsigned long __stdcall runThread(void* ptr);
Chris@47 78 #else
Chris@47 79 static void* runThread(void* ptr);
Chris@47 80 #endif
Chris@47 81 };
Chris@47 82
Chris@47 83 } // namespace kj
Chris@47 84
Chris@47 85 #endif // KJ_THREAD_H_