1 | // -*- coding: utf-8 -*-
|
---|
2 |
|
---|
3 | // Copyright (c) 2012 by István Váradi
|
---|
4 |
|
---|
5 | // This file is part of libxplcommon, a common utility library for
|
---|
6 | // development related to X-Plane
|
---|
7 |
|
---|
8 | // Copyright (c) 2013, István Váradi
|
---|
9 | // All rights reserved.
|
---|
10 |
|
---|
11 | // Redistribution and use in source and binary forms, with or without
|
---|
12 | // modification, are permitted provided that the following conditions are met:
|
---|
13 |
|
---|
14 | // 1. Redistributions of source code must retain the above copyright notice, this
|
---|
15 | // list of conditions and the following disclaimer.
|
---|
16 | // 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
17 | // this list of conditions and the following disclaimer in the documentation
|
---|
18 | // and/or other materials provided with the distribution.
|
---|
19 |
|
---|
20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
---|
21 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
22 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
23 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
---|
24 | // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
25 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
26 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
27 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
29 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
30 |
|
---|
31 | // The views and conclusions contained in the software and documentation are those
|
---|
32 | // of the authors and should not be interpreted as representing official policies,
|
---|
33 | // either expressed or implied, of the FreeBSD Project.
|
---|
34 |
|
---|
35 | #ifndef XPLCOMMON_WIN32THREAD_H
|
---|
36 | #define XPLCOMMON_WIN32THREAD_H
|
---|
37 | //------------------------------------------------------------------------------
|
---|
38 |
|
---|
39 | #include "config.h"
|
---|
40 |
|
---|
41 | #ifdef HAVE_WINDOWS_H
|
---|
42 | //------------------------------------------------------------------------------
|
---|
43 |
|
---|
44 | #include <windows.h>
|
---|
45 |
|
---|
46 | //------------------------------------------------------------------------------
|
---|
47 |
|
---|
48 | namespace xplcommon {
|
---|
49 |
|
---|
50 | //------------------------------------------------------------------------------
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * A Win32 API thread implementation.
|
---|
54 | */
|
---|
55 | class Win32Thread
|
---|
56 | {
|
---|
57 | private:
|
---|
58 | /**
|
---|
59 | * The thread-runner function.
|
---|
60 | */
|
---|
61 | static DWORD WINAPI threadFn(LPVOID arg);
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * The thread handle.
|
---|
65 | */
|
---|
66 | HANDLE handle;
|
---|
67 |
|
---|
68 | public:
|
---|
69 | /**
|
---|
70 | * Virtual destructor.
|
---|
71 | */
|
---|
72 | virtual ~Win32Thread();
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Start the thread.
|
---|
76 | *
|
---|
77 | * @return if the thread could be started
|
---|
78 | */
|
---|
79 | bool start();
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Perform the thread's real operation.
|
---|
83 | */
|
---|
84 | virtual void run() = 0;
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Wait for the thread to quit.
|
---|
88 | */
|
---|
89 | void join();
|
---|
90 | };
|
---|
91 |
|
---|
92 | //------------------------------------------------------------------------------
|
---|
93 |
|
---|
94 | /// Common type for OS-independent code
|
---|
95 | typedef Win32Thread Thread;
|
---|
96 |
|
---|
97 | //------------------------------------------------------------------------------
|
---|
98 | // Inline definitions
|
---|
99 | //------------------------------------------------------------------------------
|
---|
100 |
|
---|
101 | inline Win32Thread::~Win32Thread()
|
---|
102 | {
|
---|
103 | }
|
---|
104 |
|
---|
105 | //------------------------------------------------------------------------------
|
---|
106 |
|
---|
107 | inline bool Win32Thread::start()
|
---|
108 | {
|
---|
109 | handle = CreateThread(0, 0, &threadFn, this, 0, 0);
|
---|
110 | return handle!=0;
|
---|
111 | }
|
---|
112 |
|
---|
113 | //------------------------------------------------------------------------------
|
---|
114 |
|
---|
115 | inline void Win32Thread::join()
|
---|
116 | {
|
---|
117 | WaitForSingleObject(handle, INFINITE);
|
---|
118 | }
|
---|
119 |
|
---|
120 | //------------------------------------------------------------------------------
|
---|
121 |
|
---|
122 | } /* namespace xplcommon */
|
---|
123 |
|
---|
124 | //------------------------------------------------------------------------------
|
---|
125 | #endif // HAVE_PTHREAD_H
|
---|
126 |
|
---|
127 | #endif // XPLCOMMON_POSIXTHREAD_H
|
---|
128 |
|
---|
129 | // Local Variables:
|
---|
130 | // mode: C++
|
---|
131 | // c-basic-offset: 4
|
---|
132 | // indent-tabs-mode: nil
|
---|
133 | // End:
|
---|