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