annotate win32-mingw/include/kj/thread.h @ 163:5cc1366da2e9

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