comparison osx/include/kj/thread.h @ 49:3ab5a40c4e3b

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