summaryrefslogtreecommitdiff
path: root/support/dktools/Dk4WxProcessingController.cpt
blob: 9e03ed13c5a63029119686ba6ed8661e14ffc9e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2018-xxxx
SPDX-License-Identifier:	BSD-3-Clause

%%	header

/**	@file	Dk4WxProcessingController.h	Processing controller to skip
	concurrent execution of code accessing the same variables.

	@code
	class AnyClass {
		protected:
		Dk4WxProcessingController	procon;
		...
	};

	AnyClass::SomeMethod(void)
	{
		if (procon.CanEnterCriticalSection()) {
			... Yes, we are allowed to execute code accessing variable ...
			...
			... Notify the controller that we are done ...
			procon.LeaveCriticalSection();
		}
		else {
			... No, we are not allowed to access the variables ...
		}
	}
	@endcode
*/

#ifndef	DK4CONF_H_INCLUDED
#include <dk4conf.h>
#endif

#include <wx/wxprec.h>
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

#ifndef	WX_THREAD_H_INCLUDED
#include <wx/thread.h>
#define	WX_THREAD_H_INCLUDED 1
#endif



/**	Protect code from concurrent execution, mainly for timer handlers.
	In contrast to a critical section which delays execution of
	code to avoid concurrent access to data this class skips
	the execution of concurrent code.
*/
class Dk4WxProcessingController
{
	protected:

		/**	Critical section to protect the "in use" flag.
		*/
		wxCriticalSection	csProtect;

		/**	Flag to mark section as currently in use.
		*/
		bool				bInUse;

	public:

		/**	Constructor
		*/
		Dk4WxProcessingController(void);

		/**	Check whether the critical code section can be entered.
			@return	True if the section can be entered, false otherwise.
			If the method returns true, the bInUse flag is set, you must
			call LeaveCriticalSection() to reset it.
		*/
		bool
		CanEnterCriticalSection(void);

		/**	LeaveCriticalSection the critical section.
		*/
		void
		LeaveCriticalSection(void);

};



/* vim: set ai sw=4 ts=4 : */
%%	module

#ifndef	DK4CONF_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4conf.h"
#else
#include <dktools-4/dk4conf.h>
#endif
#endif

#ifndef	DK4WXCRITICALSECTION_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "Dk4WxProcessingController.h"
#else
#include <dktools-4/Dk4WxProcessingController.h>
#endif
#endif


Dk4WxProcessingController::Dk4WxProcessingController(void)
{
	bInUse = false;
}



bool
Dk4WxProcessingController::CanEnterCriticalSection(void)
{
	bool	back	=	false;

	{
		wxCriticalSectionLocker	lock(csProtect);

		if (!(bInUse)) {
			bInUse = true;
			back = true;
		}
	}
	return back;
}


void
Dk4WxProcessingController::LeaveCriticalSection(void)
{
	wxCriticalSectionLocker	lock(csProtect);
	bInUse = false;
}


/* vim: set ai sw=4 ts=4 : */