OWLNext
7.0
Borland's Object Windows Library for the modern age
Toggle main menu visibility
Loading...
Searching...
No Matches
msgthred.h
Go to the documentation of this file.
1
//----------------------------------------------------------------------------
2
// ObjectWindows
3
// Copyright (c) 1995, 1996 by Borland International, All Rights Reserved
4
//
5
// Author Yura Bidus
6
/// \file
7
/// Definition of message queue oriented thread class support. Degenerates to a
8
/// simple message queue owner under non-threaded environments
9
//----------------------------------------------------------------------------
10
11
#if !defined(OWL_MSGTHRED_H)
12
#define OWL_MSGTHRED_H
13
14
#include <
owl/private/defs.h
>
15
#if defined(BI_HAS_PRAGMA_ONCE)
16
# pragma once
17
#endif
18
19
#if defined(BI_MULTI_THREAD)
20
# include <
owl/thread.h
>
21
#endif
22
23
#include <
owl/wsysinc.h
>
24
#include <
owl/system.h
>
25
26
27
namespace
owl
{
28
29
#include <
owl/preclass.h
>
30
31
class
_OWLCLASS
TMsgThread
;
32
33
//
34
/// \class TMsgThread
35
// ~~~~~ ~~~~~~~~~~
36
/// TMsgThread implements basic behavior for threads that own message queues,
37
/// including mutex locking for the queue. This class provides message queue
38
/// oriented thread class support. TMsgThread degenerates to a simple message queue
39
/// owner under non-threaded environments.
40
#if defined(BI_MULTI_THREAD_RTL)
41
class
_OWLCLASS
TMsgThread
:
public
TThread
42
{
43
#else
44
class
_OWLCLASS
TMsgThread
45
{
46
public
:
47
enum
TCurrent
{
Current
};
48
#endif
49
50
public
:
51
TMsgThread
(TCurrent);
52
53
// Message queue functions
54
//
55
virtual
int
MessageLoop();
56
virtual
bool
IdleAction(
long
idleCount);
57
virtual
bool
ProcessMsg(MSG& msg);
58
59
bool
PumpWaitingMessages();
60
void
FlushQueue();
61
62
/// Is this queue thread running its message loop?
63
//
64
bool
IsRunning()
const
;
65
66
// Message queue thread synchronization mechanism. Real multi-threading
67
// requires compiler & RTL support (-MT)
68
//
69
#if defined(BI_MULTI_THREAD_RTL)
70
# if defined(BI_USE_CRITICAL_SECTION)
71
// Using a critical section is much faster, but prevents
72
// cross-process locking and potentially breaks existing code.
73
typedef
TCriticalSection
TMsgMutex;
74
# else
75
typedef
TMutex
TMsgMutex;
76
# endif
77
void
EnableMultiThreading(
bool
enable);
78
TMsgMutex* GetMutex();
79
80
class
_OWLCLASS
TQueueLock {
81
public
:
82
TQueueLock(
TMsgThread
& app);
83
~TQueueLock();
84
void
Release(
bool
relinquish =
false
);
85
private
:
86
#if defined(BI_COMP_BORLAND)
87
TMsgThread::TMsgMutex::TLock *Lock;
88
#else
89
TMsgMutex::TLock *Lock;
90
#endif
91
};
92
#endif
93
94
protected
:
95
#if defined(BI_MULTI_THREAD_RTL)
// No real threads without RTL support
96
TMsgThread();
97
#endif
98
99
// Override this for created threads, app thread is Run() from OwlMain
100
//
101
virtual
int
Run();
102
103
virtual
void
InitInstance();
104
virtual
int
TermInstance(
int
status);
105
106
bool
BreakMessageLoop
;
///< Message loop is broken via WM_QUIT
107
int
MessageLoopResult
;
///< Return value from message loop
108
109
// !CQ still need this with thread state?
110
bool
LoopRunning
;
///< Track if the loop is running
111
112
#if defined(BI_MULTI_THREAD_RTL)
113
// !CQ Just use a TMutex directly???
114
class
_OWLCLASS
TAppMutex {
115
public
:
116
TAppMutex();
117
~TAppMutex();
118
TMsgMutex* GetMutex();
119
private
:
120
TMsgMutex *Mutex;
121
};
122
123
TAppMutex Mutex;
///< Mutex to prevent multiple threads
124
///< from processing messages at the
125
///< same time.
126
private
:
127
bool
UseMutex;
128
#endif
129
};
130
131
#include <
owl/posclass.h
>
132
133
//----------------------------------------------------------------------------
134
// Inline implementations
135
136
//
137
/// Returns true if this queue thread is running its message loop.
138
//
139
inline
bool
TMsgThread::IsRunning
()
const
140
{
141
return
LoopRunning
;
142
}
143
144
#if defined(BI_MULTI_THREAD_RTL)
145
146
//
147
/// Enables or disables the use of the mutex to synchronize access to the message
148
/// queue. TMsgThread will only lock its message queue mutex when enabled. Real
149
/// multi-threading requires compiler and RTL support.
150
//
151
inline
void
TMsgThread::EnableMultiThreading(
bool
enable)
152
{
153
UseMutex = enable;
154
}
155
156
//
157
/// Gets this message thread's mutex. Returns 0 if mutexes are not supported, or are
158
/// not enabled for this thread.
159
//
160
inline
TMsgThread::TMsgMutex* TMsgThread::GetMutex()
161
{
162
return
UseMutex ? Mutex.GetMutex() :
nullptr
;
163
}
164
165
//
166
/// The mutex is always constructed/destructed if the system supports threads
167
//
168
inline
TMsgThread::TAppMutex::TAppMutex()
169
:
170
Mutex(nullptr)
171
{
172
Mutex =
new
TMsgThread::TMsgMutex;
173
}
174
175
//
176
inline
TMsgThread::TAppMutex::~TAppMutex()
177
{
178
delete
Mutex;
179
}
180
181
//
182
/// Get the apps mutex. Return 0 if mutexs are not supported, or are not enabled
183
/// for this app.
184
//
185
inline
TMsgThread::TMsgMutex* TMsgThread::TAppMutex::GetMutex()
186
{
187
return
Mutex;
188
}
189
190
//
191
/// Only lock if the mutex is available. Do nothing otherwise.
192
//
193
inline
TMsgThread::TQueueLock::TQueueLock(
TMsgThread
& app)
194
: Lock(nullptr)
195
{
196
TMsgMutex* appMutex = app.GetMutex();
197
if
(appMutex)
198
Lock =
new
TMsgMutex::TLock(*appMutex);
199
}
200
201
//
202
/// Destruct the real lock if it was constructed (locked)
203
//
204
inline
TMsgThread::TQueueLock::~TQueueLock()
205
{
206
delete
Lock;
207
}
208
209
//
210
/// Release the real lock if it was locked
211
//
212
inline
void
TMsgThread::TQueueLock::Release(
bool
relinquish)
213
{
214
if
(Lock) {
215
CHECK
(relinquish ==
true
);
InUse
(relinquish);
216
delete
Lock;
217
Lock =
nullptr
;
218
}
219
}
220
221
#endif
// BI_MULTI_THREAD_RTL
222
223
}
// OWL namespace
224
225
226
227
#endif
// OWL_MSGTHRED_H
CHECK
#define CHECK(condition)
Definition
checks.h:239
owl::TCriticalSection
Lightweight intra-process thread synchronization.
Definition
thread.h:308
owl::TMsgThread
TMsgThread implements basic behavior for threads that own message queues, including mutex locking for...
Definition
msgthred.h:45
owl::TMsgThread::BreakMessageLoop
bool BreakMessageLoop
Message loop is broken via WM_QUIT.
Definition
msgthred.h:106
owl::TMsgThread::IsRunning
bool IsRunning() const
Is this queue thread running its message loop?
Definition
msgthred.h:139
owl::TMsgThread::LoopRunning
bool LoopRunning
Track if the loop is running.
Definition
msgthred.h:110
owl::TMsgThread::TCurrent
TCurrent
Definition
msgthred.h:47
owl::TMsgThread::Current
@ Current
Definition
msgthred.h:47
owl::TMsgThread::MessageLoopResult
int MessageLoopResult
Return value from message loop.
Definition
msgthred.h:107
owl::TMsgThread::TMsgThread
TMsgThread(TCurrent)
Attaches to the current running thread.
Definition
msgthred.cpp:28
owl::TMutex
Mutual-exclusive semaphore.
Definition
thread.h:121
owl::TThread
TThread provides a system-independent interface to threads.
Definition
thread.h:601
owl
Object Windows Library (OWLNext Core).
Definition
animctrl.h:22
owl::InUse
void InUse(const T &arg)
Handy utility to avoid compiler warnings about unused parameters.
Definition
defs.h:299
defs.h
_OWLCLASS
#define _OWLCLASS
Definition
defs.h:338
posclass.h
preclass.h
system.h
Definition of TSystem, a system information provider class.
thread.h
wsysinc.h
Includes windowing system headers, with necessary macros defined.
include
owl
msgthred.h
Generated by
1.18.0