annotate win64-msvc/include/kj/thread.h @ 169:223a55898ab9 tip default

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