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