OWLNext
7.0
Borland's Object Windows Library for the modern age
Toggle main menu visibility
Loading...
Searching...
No Matches
msgthred.cpp
Go to the documentation of this file.
1
//----------------------------------------------------------------------------
2
// Borland WinSys Library
3
// Copyright (c) 1995, 1996 by Borland International, All Rights Reserved
4
//
5
/// \file
6
/// Implementation of class TMsgThread. This implements basic behavior for
7
/// threads that own message queues, including mutex locking for the queue
8
//----------------------------------------------------------------------------
9
#include <
owl/pch.h
>
10
#include <
owl/msgthred.h
>
11
12
namespace
owl
{
13
14
DIAG_DEFINE_GROUP_INIT
(
OWL_INI
, MsgThreads, 1, 0);
15
16
//
17
/// Attaches to the current running thread. This is often the initial process
18
/// thread, or even the only thread for non-threaded systems.
19
//
20
#if defined(BI_MULTI_THREAD_RTL)
// No threads without RTL support
21
TMsgThread::TMsgThread
(TCurrent c)
22
:
23
TThread
(c),
24
#if defined(BI_MULTI_THREAD_RTL)
25
UseMutex(false),
// Default for primary thread is single-threaded
26
#endif
27
#else
28
TMsgThread
::
TMsgThread
(
TCurrent
/*c*/
)
29
:
30
#endif
31
BreakMessageLoop
(false),
32
MessageLoopResult
(0),
33
LoopRunning
(false)
34
{
35
}
36
37
#if defined(BI_MULTI_THREAD_RTL)
// No real threads without RTL support
38
//
39
/// Create a thread.
40
/// The thread is in a paused-state.
41
//
42
TMsgThread::TMsgThread
()
43
:
44
TThread
(),
45
MessageLoopResult(0),
46
BreakMessageLoop(false),
47
LoopRunning(false),
48
UseMutex(true)
// Default for created thread is multi-threaded
49
{
50
}
51
#endif
52
53
//
54
/// Retrieves and processes messages from the thread's message queue using
55
/// PumpWaitingMessages() until BreakMessageLoop becomes true. Catches exceptions to
56
/// post a quit message and cleanup before resuming.
57
///
58
/// Calls IdleAction() when there are no messages
59
//
60
int
61
TMsgThread::MessageLoop
()
62
{
63
long
idleCount = 0;
64
MessageLoopResult
= 0;
65
try
{
66
while
(!
BreakMessageLoop
) {
67
if
(!
IdleAction
(idleCount++))
68
::WaitMessage();
// allow system to go idle
69
if
(
PumpWaitingMessages
())
// pumps any waiting messages
70
idleCount = 0;
71
}
72
}
73
catch
(...) {
74
::PostQuitMessage(-1);
75
BreakMessageLoop
=
false
;
76
throw
;
77
}
78
BreakMessageLoop
=
false
;
79
return
MessageLoopResult
;
80
}
81
82
//
83
/// Called each time there are no messages in the queue. Idle count is incremented
84
/// each time, and zeroed when messages are pumped. Returns whether or not more
85
/// processing needs to be done.
86
//
87
bool
88
#if defined(__TRACE)
89
TMsgThread::IdleAction
(
long
idleCount)
90
#else
91
TMsgThread::IdleAction
(
long
/*idleCount*/
)
92
#endif
93
{
94
TRACEX
(MsgThreads, 1,
"TMsgThread::IdleAction() called @"
<< (
void
*)
this
<<
95
" idleCount "
<< idleCount);
96
return
false
;
97
}
98
99
//
100
/// Called for each message that is pulled from the queue, to perform all
101
/// translation and dispatching.
102
///
103
/// Returns true to drop out of the pump.
104
//
105
bool
106
TMsgThread::ProcessMsg
(MSG& msg)
107
{
108
// Translate the message & dispatch it.TMsgThread
109
//
110
::TranslateMessage(&msg);
111
::DispatchMessage(&msg);
112
// ResumeThrow();
113
114
return
false
;
115
}
116
117
//
118
/// The inner message loop. Retrieves and processes messages from the OWL
119
/// application's message queue until it is empty. Sets BreakMessageLoop if a WM_QUIT
120
/// passes through. Calls ProcessAppMsg for each message to allow special pre-handling.
121
//
122
bool
123
TMsgThread::PumpWaitingMessages
()
124
{
125
MSG msg;
126
bool
foundOne =
false
;
127
128
while
(::PeekMessage(&msg,
nullptr
, 0, 0, PM_REMOVE)) {
129
foundOne =
true
;
130
if
(msg.message == WM_QUIT) {
131
BreakMessageLoop
=
true
;
132
MessageLoopResult
=
static_cast<
int
>
(msg.wParam);
133
::PostQuitMessage(
MessageLoopResult
);
// make sure all loops exit
134
break
;
135
}
136
bool
shouldBreak =
ProcessMsg
(msg);
137
if
(shouldBreak)
138
break
;
139
}
140
return
foundOne;
141
}
142
143
//
144
/// Flushes all real messages from the message queue.
145
//
146
void
147
TMsgThread::FlushQueue
()
148
{
149
MSG msg;
150
while
(::PeekMessage(&msg,
nullptr
,0,0,PM_NOYIELD|PM_REMOVE)
151
&& msg.message != WM_PAINT)
152
;
153
}
154
155
//
156
/// Runs this message thread, returns when the message queue quits. Initializes
157
/// instances. Runs the thread's message loop. Each of the virtual functions called
158
/// are expected to throw an exception if there is an error.
159
/// Exceptions that are not handled, that is, where the status remains non-zero, are
160
/// propagated out of this function. The message queue is still flushed and
161
/// TermInstance called.
162
//
163
int
164
TMsgThread::Run
()
165
{
166
int
status = 0;
167
try
{
168
InitInstance
();
169
LoopRunning
=
true
;
170
status =
MessageLoop
();
171
}
172
catch
(...) {
173
LoopRunning
=
false
;
174
FlushQueue
();
175
TermInstance
(status);
176
throw
;
177
}
178
179
LoopRunning
=
false
;
180
FlushQueue
();
181
return
TermInstance
(status);
182
}
183
184
//
185
/// Handles initialization for each executing instance of the message thread.
186
/// Derived classes can override this to perform initialization for each instance.
187
//
188
void
189
TMsgThread::InitInstance
()
190
{
191
TRACEX
(MsgThreads, 1,
"TMsgThread::InitInstance() called @"
<< (
void
*)
this
);
192
193
// Override to perform initialization prior to running
194
// Call TMsgThread::InitInstance() before body
195
}
196
197
//
198
/// Handles termination for each executing instance of the message thread. Called at
199
/// the end of a Run() with the final return status.
200
//
201
int
202
TMsgThread::TermInstance
(
int
status)
203
{
204
TRACEX
(MsgThreads, 1,
"TMsgThread::TermInstance() called @"
<< (
void
*)
this
);
205
206
// Override to perform termination cleanup after running
207
// Call TMsgThread::TermInstance(status) after body
208
209
return
status;
210
}
211
212
}
// OWL namespace
213
TRACEX
#define TRACEX(group, level, message)
Definition
checks.h:263
owl::TMsgThread::IdleAction
virtual bool IdleAction(long idleCount)
Called each time there are no messages in the queue.
Definition
msgthred.cpp:91
owl::TMsgThread::Run
virtual int Run()
Runs this message thread, returns when the message queue quits.
Definition
msgthred.cpp:164
owl::TMsgThread::BreakMessageLoop
bool BreakMessageLoop
Message loop is broken via WM_QUIT.
Definition
msgthred.h:106
owl::TMsgThread::InitInstance
virtual void InitInstance()
Handles initialization for each executing instance of the message thread.
Definition
msgthred.cpp:189
owl::TMsgThread::TermInstance
virtual int TermInstance(int status)
Handles termination for each executing instance of the message thread.
Definition
msgthred.cpp:202
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::MessageLoop
virtual int MessageLoop()
Retrieves and processes messages from the thread's message queue using PumpWaitingMessages() until Br...
Definition
msgthred.cpp:61
owl::TMsgThread::ProcessMsg
virtual bool ProcessMsg(MSG &msg)
Called for each message that is pulled from the queue, to perform all translation and dispatching.
Definition
msgthred.cpp:106
owl::TMsgThread::MessageLoopResult
int MessageLoopResult
Return value from message loop.
Definition
msgthred.h:107
owl::TMsgThread::FlushQueue
void FlushQueue()
Flushes all real messages from the message queue.
Definition
msgthred.cpp:147
owl::TMsgThread::TMsgThread
TMsgThread(TCurrent)
Attaches to the current running thread.
Definition
msgthred.cpp:28
owl::TMsgThread::PumpWaitingMessages
bool PumpWaitingMessages()
The inner message loop.
Definition
msgthred.cpp:123
owl::TThread
TThread provides a system-independent interface to threads.
Definition
thread.h:601
pch.h
msgthred.h
Definition of message queue oriented thread class support.
owl
Object Windows Library (OWLNext Core).
Definition
animctrl.h:22
owl::DIAG_DEFINE_GROUP_INIT
DIAG_DEFINE_GROUP_INIT(OWL_INI, OwlApp, 1, 0)
OWL_INI
#define OWL_INI
Definition
defs.h:170
source
owlcore
msgthred.cpp
Generated by
1.18.0