1
1
using System ;
2
+ using System . Collections . Concurrent ;
2
3
using System . Collections . Generic ;
3
4
using System . ComponentModel ;
4
5
using System . Diagnostics ;
@@ -394,8 +395,7 @@ protected override void OnLoad(EventArgs e)
394
395
}
395
396
Program . FileSystem . AppendToLogFile ( "Game window initialised successfully." ) ;
396
397
//Initialise the loader thread queues
397
- jobs = new Queue < ThreadStart > ( 10 ) ;
398
- locks = new Queue < object > ( 10 ) ;
398
+ jobs = new ConcurrentQueue < ThreadStart > ( ) ;
399
399
Program . Renderer . Initialize ( ) ;
400
400
Program . Renderer . DetermineMaxAFLevel ( ) ;
401
401
Interface . CurrentOptions . Save ( OpenBveApi . Path . CombineFile ( Program . FileSystem . SettingsFolder , "1.5.0/options.cfg" ) ) ;
@@ -1126,18 +1126,15 @@ public void LoadingScreenLoop()
1126
1126
1127
1127
if ( Loading . JobAvailable )
1128
1128
{
1129
- while ( jobs . Count > 0 )
1129
+ while ( ! jobs . IsEmpty )
1130
1130
{
1131
- lock ( jobLock )
1131
+ jobs . TryDequeue ( out ThreadStart currentJob ) ;
1132
+ currentJob ( ) ;
1133
+ lock ( currentJob )
1132
1134
{
1133
- var currentJob = jobs . Dequeue ( ) ;
1134
- var locker = locks . Dequeue ( ) ;
1135
- currentJob ( ) ;
1136
- lock ( locker )
1137
- {
1138
- Monitor . Pulse ( locker ) ;
1139
- }
1135
+ Monitor . Pulse ( currentJob ) ;
1140
1136
}
1137
+
1141
1138
}
1142
1139
Loading . JobAvailable = false ;
1143
1140
}
@@ -1156,28 +1153,22 @@ public void LoadingScreenLoop()
1156
1153
}
1157
1154
}
1158
1155
1159
- private static readonly object jobLock = new object ( ) ;
1160
- private static Queue < ThreadStart > jobs ;
1161
- private static Queue < object > locks ;
1156
+ private static ConcurrentQueue < ThreadStart > jobs ;
1162
1157
1163
1158
/// <summary>This method is used during loading to run commands requiring an OpenGL context in the main render loop</summary>
1164
1159
/// <param name="job">The OpenGL command</param>
1165
1160
/// <param name="timeout">The timeout</param>
1166
1161
internal static void RunInRenderThread ( ThreadStart job , int timeout )
1167
1162
{
1168
1163
object locker = new object ( ) ;
1169
- lock ( jobLock )
1170
- {
1171
- jobs . Enqueue ( job ) ;
1172
- locks . Enqueue ( locker ) ;
1173
- //Don't set the job to available until after it's been loaded into the queue
1174
- Loading . JobAvailable = true ;
1175
- }
1176
- lock ( locker )
1177
- {
1178
- //Failsafe: If our job has taken more than the timeout, stop waiting for it
1179
- //A missing texture is probably better than an infinite loadscreen
1180
- Monitor . Wait ( locker , timeout ) ;
1164
+ jobs . Enqueue ( job ) ;
1165
+ //Don't set the job to available until after it's been loaded into the queue
1166
+ Loading . JobAvailable = true ;
1167
+ //Failsafe: If our job has taken more than the timeout, stop waiting for it
1168
+ //A missing texture is probably better than an infinite loadscreen
1169
+ lock ( job )
1170
+ {
1171
+ Monitor . Wait ( job , timeout ) ;
1181
1172
}
1182
1173
}
1183
1174
}
0 commit comments