Tiberian Technologies Scripts Reference Revision: 9000
Loading...
Searching...
No Matches
CriticalSectionClass.h
1/* Renegade Scripts.dll
2 Copyright 2013 Tiberian Technologies
3
4 This file is part of the Renegade scripts.dll
5 The Renegade scripts.dll is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2, or (at your option) any later
8 version. See the file COPYING for more details.
9 In addition, an exemption is given to allow Run Time Dynamic Linking of this code with any closed source module that does not contain code covered by this licence.
10 Only the source code to the module(s) containing the licenced code has to be released.
11*/
12#ifndef TT_INCLUDE__CRITICALSECTIONCLASS_H
13#define TT_INCLUDE__CRITICALSECTIONCLASS_H
14
15
16
17#include "ThreadClass.h"
18
19
20
21class SCRIPTS_API CriticalSectionClass
22{
23
24private:
25
26 void* handle;
27 unsigned int locked;
28
29public:
30
31 class LockClass
32 {
33
34 private:
35
36 CriticalSectionClass& CriticalSection;
37
38 public:
39
40 LockClass(CriticalSectionClass& section);
41 ~LockClass();
42 LockClass operator=(LockClass&);
43
44 };
45
46 CriticalSectionClass();
47 ~CriticalSectionClass();
48 void Enter();
49 void Exit();
50
51};
52
53
54class FastCriticalSectionClass
55{
56
57private:
58
59 friend class LockClass;
60
61 volatile long Flag; // 0000
62
63
64 void Enter()
65 {
66 TT_ASSERT((size_t)&Flag % 4 == 0); // aligned to 4 bytes please
67 for (;;)
68 {
69 if (_interlockedbittestandset(&Flag, 0) == 0) return;
70 _mm_pause();
71 };
72 }
73
74
75 void Leave()
76 {
77 Flag = 0;
78 }
79
80
81public:
82
83 class LockClass
84 {
85 FastCriticalSectionClass& criticalSection;
86
87 LockClass& operator=(const LockClass&)
88 {
89 return *this;
90 }
91
92
93 public:
94
95 LockClass(FastCriticalSectionClass& _criticalSection) :
96 criticalSection(_criticalSection)
97 {
98 criticalSection.Enter();
99 }
100
101 ~LockClass()
102 {
103 criticalSection.Leave();
104 }
105
106 };
107
108 FastCriticalSectionClass() :
109 Flag(0)
110 {
111 }
112
113}; // 0004
114
115
116
117#endif