OWLNext    7.0
Borland's Object Windows Library for the modern age
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
27namespace owl {
28
29#include <owl/preclass.h>
30
31class _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)
41class _OWLCLASS TMsgThread : public TThread
42{
43#else
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.
74# else
75 typedef TMutex TMsgMutex;
76# endif
79
80 class _OWLCLASS TQueueLock {
81 public:
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();
119 private:
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//
139inline 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//
151inline void TMsgThread::EnableMultiThreading(bool enable)
152{
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//
160inline 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//
168inline TMsgThread::TAppMutex::TAppMutex()
169:
171{
172 Mutex = new TMsgThread::TMsgMutex;
173}
174
175//
176inline 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//
185inline TMsgThread::TMsgMutex* TMsgThread::TAppMutex::GetMutex()
186{
187 return Mutex;
188}
189
190//
191/// Only lock if the mutex is available. Do nothing otherwise.
192//
193inline 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//
204inline TMsgThread::TQueueLock::~TQueueLock()
205{
206 delete Lock;
207}
208
209//
210/// Release the real lock if it was locked
211//
212inline 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
#define CHECK(condition)
Definition checks.h:239
Lightweight intra-process thread synchronization.
Definition thread.h:308
TMsgThread implements basic behavior for threads that own message queues, including mutex locking for...
Definition msgthred.h:45
bool BreakMessageLoop
Message loop is broken via WM_QUIT.
Definition msgthred.h:106
bool LoopRunning
Track if the loop is running.
Definition msgthred.h:110
int MessageLoopResult
Return value from message loop.
Definition msgthred.h:107
Mutual-exclusive semaphore.
Definition thread.h:121
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
void InUse(const T &arg)
Handy utility to avoid compiler warnings about unused parameters.
Definition defs.h:299
#define _OWLCLASS
Definition defs.h:338
Definition of TSystem, a system information provider class.
Includes windowing system headers, with necessary macros defined.