annotate win32-mingw/include/kj/thread.h @ 60:95867ba8caa8

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