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