annotate win32-mingw/include/kj/thread.h @ 65:a69c1527268d

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