annotate win64-msvc/include/kj/thread.h @ 83:ae30d91d2ffe

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