OWLNext    7.0
Borland's Object Windows Library for the modern age
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
12namespace owl {
13
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
22:
23 TThread(c),
25 UseMutex(false), // Default for primary thread is single-threaded
26#endif
27#else
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//
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//
60int
62{
63 long idleCount = 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 (...) {
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//
87bool
88#if defined(__TRACE)
90#else
91TMsgThread::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//
105bool
107{
108 // Translate the message & dispatch it.TMsgThread
109 //
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//
122bool
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//
146void
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//
163int
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//
188void
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//
201int
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
#define TRACEX(group, level, message)
Definition checks.h:263
#define DIAG_DEFINE_GROUP_INIT(f, g, e, l)
Definition checks.h:429
TMsgThread implements basic behavior for threads that own message queues, including mutex locking for...
Definition msgthred.h:45
virtual bool IdleAction(long idleCount)
Called each time there are no messages in the queue.
Definition msgthred.cpp:91
virtual int Run()
Runs this message thread, returns when the message queue quits.
Definition msgthred.cpp:164
bool BreakMessageLoop
Message loop is broken via WM_QUIT.
Definition msgthred.h:106
virtual void InitInstance()
Handles initialization for each executing instance of the message thread.
Definition msgthred.cpp:189
virtual int TermInstance(int status)
Handles termination for each executing instance of the message thread.
Definition msgthred.cpp:202
bool LoopRunning
Track if the loop is running.
Definition msgthred.h:110
virtual int MessageLoop()
Retrieves and processes messages from the thread's message queue using PumpWaitingMessages() until Br...
Definition msgthred.cpp:61
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
int MessageLoopResult
Return value from message loop.
Definition msgthred.h:107
void FlushQueue()
Flushes all real messages from the message queue.
Definition msgthred.cpp:147
TMsgThread(TCurrent)
Attaches to the current running thread.
Definition msgthred.cpp:28
bool PumpWaitingMessages()
The inner message loop.
Definition msgthred.cpp:123
TThread provides a system-independent interface to threads.
Definition thread.h:601
Definition of message queue oriented thread class support.
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
#define OWL_INI
Definition defs.h:170