1%%	options
2
3copyright owner	=	Dirk Krause
4copyright year	=	2018-xxxx
5SPDX-License-Identifier:	BSD-3-Clause
6
7%%	header
8
9/**	@file	Dk4WxProcessingController.h	Processing controller to skip
10	concurrent execution of code accessing the same variables.
11
12	@code
13	class AnyClass {
14		protected:
15		Dk4WxProcessingController	procon;
16		...
17	};
18
19	AnyClass::SomeMethod(void)
20	{
21		if (procon.CanEnterCriticalSection()) {
22			... Yes, we are allowed to execute code accessing variable ...
23			...
24			... Notify the controller that we are done ...
25			procon.LeaveCriticalSection();
26		}
27		else {
28			... No, we are not allowed to access the variables ...
29		}
30	}
31	@endcode
32*/
33
34#ifndef	DK4CONF_H_INCLUDED
35#include "dk4conf.h"
36#endif
37
38#include <wx/wxprec.h>
39#ifdef __BORLANDC__
40#pragma hdrstop
41#endif
42#ifndef WX_PRECOMP
43#include <wx/wx.h>
44#endif
45
46#ifndef	WX_THREAD_H_INCLUDED
47#include <wx/thread.h>
48#define	WX_THREAD_H_INCLUDED 1
49#endif
50
51
52
53/**	Protect code from concurrent execution, mainly for timer handlers.
54	In contrast to a critical section which delays execution of
55	code to avoid concurrent access to data this class skips
56	the execution of concurrent code.
57*/
58class Dk4WxProcessingController
59{
60	protected:
61
62		/**	Critical section to protect the "in use" flag.
63		*/
64		wxCriticalSection	csProtect;
65
66		/**	Flag to mark section as currently in use.
67		*/
68		bool				bInUse;
69
70	public:
71
72		/**	Constructor
73		*/
74		Dk4WxProcessingController(void);
75
76		/**	Check whether the critical code section can be entered.
77			@return	True if the section can be entered, false otherwise.
78			If the method returns true, the bInUse flag is set, you must
79			call LeaveCriticalSection() to reset it.
80		*/
81		bool
82		CanEnterCriticalSection(void);
83
84		/**	LeaveCriticalSection the critical section.
85		*/
86		void
87		LeaveCriticalSection(void);
88
89};
90
91
92
93/* vim: set ai sw=4 ts=4 : */
94%%	module
95
96#ifndef	DK4CONF_H_INCLUDED
97#if DK4_BUILDING_DKTOOLS4
98#include "dk4conf.h"
99#else
100#include <dktools-4/dk4conf.h>
101#endif
102#endif
103
104#ifndef	DK4WXCRITICALSECTION_H_INCLUDED
105#if DK4_BUILDING_DKTOOLS4
106#include <libdk4wx/Dk4WxProcessingController.h>
107#else
108#include <dktools-4/Dk4WxProcessingController.h>
109#endif
110#endif
111
112
113Dk4WxProcessingController::Dk4WxProcessingController(void)
114{
115	bInUse = false;
116}
117
118
119
120bool
121Dk4WxProcessingController::CanEnterCriticalSection(void)
122{
123	bool	back	=	false;
124
125	{
126		wxCriticalSectionLocker	lock(csProtect);
127
128		if (!(bInUse)) {
129			bInUse = true;
130			back = true;
131		}
132	}
133	return back;
134}
135
136
137void
138Dk4WxProcessingController::LeaveCriticalSection(void)
139{
140	wxCriticalSectionLocker	lock(csProtect);
141	bInUse = false;
142}
143
144
145/* vim: set ai sw=4 ts=4 : */
146