annotate win64-msvc/include/kj/thread.h @ 144:a0c5ef2265f5

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