Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Thread-ThreadPool to Task Conversion #43

Closed
wants to merge 1 commit into from

2 participants

@semihokur

My refactoring tool converted good candidates of Thread-ThreadPool usages to corresponding Task constructs. I manually verified the transformations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 30, 2013
  1. Thread-ThreadPool to Task Conversion is completed

    Semih Okur authored
This page is out of date. Refresh to see the latest.
Showing with 56 additions and 40 deletions.
  1. +2 −2 src/EventStore/EventStore.ClientAPI/Connection/SubscriptionsChannel.cs
  2. +2 −2 src/EventStore/EventStore.ClientAPI/EventStoreConnection.cs
  3. +4 −4 src/EventStore/EventStore.Core.Tests/Bus/QueueSpeedTest.cs
  4. +3 −2 src/EventStore/EventStore.Core.Tests/Bus/when_stopping_queued_handler.cs
  5. +2 −2 src/EventStore/EventStore.Core.Tests/ClientAPI/transaction.cs
  6. +3 −2 src/EventStore/EventStore.Core/Bus/QueuedHandlerAutoReset.cs
  7. +3 −2 src/EventStore/EventStore.Core/Bus/QueuedHandlerMRES.cs
  8. +3 −2 src/EventStore/EventStore.Core/Bus/QueuedHandlerPulse.cs
  9. +3 −2 src/EventStore/EventStore.Core/Bus/QueuedHandlerSleep.cs
  10. +3 −2 src/EventStore/EventStore.Core/Bus/QueuedHandlerThreadPool.cs
  11. +3 −2 src/EventStore/EventStore.Core/Index/TableIndex.cs
  12. +2 −1  src/EventStore/EventStore.Core/Services/Monitoring/Utils/ShellExecutor.cs
  13. +3 −4 src/EventStore/EventStore.Core/Services/Storage/StorageChaser.cs
  14. +2 −1  src/EventStore/EventStore.Core/Services/Storage/StorageScavenger.cs
  15. +4 −4 src/EventStore/EventStore.Core/Services/TimerService/ThreadBasedScheduler.cs
  16. +2 −1  src/EventStore/EventStore.Core/TransactionLog/Chunks/TFChunkManager.cs
  17. +3 −2 src/EventStore/EventStore.Projections.Core.Tests/Playground/LaunchpadBase.cs
  18. +4 −0 src/EventStore/EventStore.Projections.v8Integration/EventStore.Projections.v8Integration.vcxproj
  19. +3 −2 src/EventStore/EventStore.TestClient/Client.cs
  20. +2 −1  src/EventStore/EventStore.TestClient/CommandsProcessor.cs
View
4 src/EventStore/EventStore.ClientAPI/Connection/SubscriptionsChannel.cs
@@ -113,10 +113,10 @@ private void ExecuteUserCallbackAsync(Action callback)
_executionQueue.Enqueue(callback);
if (Interlocked.CompareExchange(ref _workerRunning, 1, 0) == 0)
- ThreadPool.QueueUserWorkItem(ExecuteUserCallbacks);
+ Task.Factory.StartNew(ExecuteUserCallbacks);
}
- private void ExecuteUserCallbacks(object state)
+ private void ExecuteUserCallbacks()
{
bool proceed = true;
while (proceed)
View
4 src/EventStore/EventStore.ClientAPI/EventStoreConnection.cs
@@ -94,7 +94,7 @@ public class EventStoreConnection : IDisposable
private readonly Stopwatch _timeoutCheckStopwatch = new Stopwatch();
private int _reconnectionsCount;
- private Thread _worker;
+ private Task _worker;
private volatile bool _stopping;
private readonly ConnectionSettings _settings;
@@ -205,7 +205,7 @@ private Task EstablishConnectionAsync(IPEndPoint tcpEndPoint)
_connection = _connector.CreateTcpConnection(_tcpEndPoint, OnPackageReceived, OnConnectionEstablished, OnConnectionClosed);
_timeoutCheckStopwatch.Start();
- _worker = new Thread(MainLoop) {IsBackground = true, Name = "Worker thread"};
+ _worker = new Task(MainLoop, TaskCreationOptions.LongRunning);
_worker.Start();
return Tasks.CreateCompleted();
View
8 src/EventStore/EventStore.Core.Tests/Bus/QueueSpeedTest.cs
@@ -9,6 +9,7 @@
using EventStore.Core.Messaging;
using EventStore.Core.Tests.Bus.Helpers;
using NUnit.Framework;
+using System.Threading.Tasks;
namespace EventStore.Core.Tests.Bus
{
@@ -130,14 +131,14 @@ public void mres_queued_handler_10_producers_50mln_messages()
private void SpeedTest(Func<IHandle<Message>, IPublisher> queueFactory, int producingThreads, int messageCnt)
{
var queue = queueFactory(new NoopConsumer());
- var threads = new Thread[producingThreads];
+
int msgCnt = messageCnt;
var startEvent = new ManualResetEventSlim(false);
var endEvent = new CountdownEvent(producingThreads);
var msg = new SystemMessage.SystemStart();
for (int i = 0; i < producingThreads; ++i)
{
- threads[i] = new Thread(() =>
+ Task.Factory.StartNew(() =>
{
startEvent.Wait();
@@ -148,8 +149,7 @@ private void SpeedTest(Func<IHandle<Message>, IPublisher> queueFactory, int prod
endEvent.Signal();
- }) { IsBackground = true, Name = "Producer #" + i};
- threads[i].Start();
+ }, TaskCreationOptions.LongRunning);
}
Thread.Sleep(500);
View
5 src/EventStore/EventStore.Core.Tests/Bus/when_stopping_queued_handler.cs
@@ -32,6 +32,7 @@
using EventStore.Core.Messaging;
using EventStore.Core.Tests.Bus.Helpers;
using NUnit.Framework;
+using System.Threading.Tasks;
namespace EventStore.Core.Tests.Bus
{
@@ -58,7 +59,7 @@ public void gracefully_and_queue_is_not_busy_should_not_take_much_time()
var wait = new ManualResetEventSlim(false);
- ThreadPool.QueueUserWorkItem(_ =>
+ Task.Factory.StartNew(() =>
{
Queue.Stop();
wait.Set();
@@ -83,7 +84,7 @@ public void second_time_should_not_take_much_time()
var wait = new ManualResetEventSlim(false);
- ThreadPool.QueueUserWorkItem(_ =>
+ Task.Factory.StartNew(() =>
{
Queue.Stop();
wait.Set();
View
4 src/EventStore/EventStore.Core.Tests/ClientAPI/transaction.cs
@@ -174,7 +174,7 @@ public void should_commit_when_writing_with_exp_ver_any_even_while_somene_is_wri
}
//500 events during transaction
- ThreadPool.QueueUserWorkItem(_ =>
+ Task.Factory.StartNew(() =>
{
using (var store = EventStoreConnection.Create())
{
@@ -200,7 +200,7 @@ public void should_commit_when_writing_with_exp_ver_any_even_while_somene_is_wri
});
//500 events to same stream in parallel
- ThreadPool.QueueUserWorkItem(_ =>
+ Task.Factory.StartNew(() =>
{
using (var store = EventStoreConnection.Create())
{
View
5 src/EventStore/EventStore.Core/Bus/QueuedHandlerAutoReset.cs
@@ -31,6 +31,7 @@
using EventStore.Common.Utils;
using EventStore.Core.Messaging;
using EventStore.Core.Services.Monitoring.Stats;
+using System.Threading.Tasks;
namespace EventStore.Core.Bus
{
@@ -54,7 +55,7 @@ public class QueuedHandlerAutoReset : IQueuedHandler, IHandle<Message>, IPublish
private readonly Common.Concurrent.ConcurrentQueue<Message> _queue = new Common.Concurrent.ConcurrentQueue<Message>();
private readonly AutoResetEvent _msgAddEvent = new AutoResetEvent(false);
- private Thread _thread;
+ private Task _thread;
private volatile bool _stop;
private readonly ManualResetEventSlim _stopped = new ManualResetEventSlim(true);
private readonly TimeSpan _threadStopWaitTimeout;
@@ -92,7 +93,7 @@ public void Start()
_stopped.Reset();
- _thread = new Thread(ReadFromQueue) {IsBackground = true, Name = Name};
+ _thread = new Task(ReadFromQueue, TaskCreationOptions.LongRunning);
_thread.Start();
}
View
5 src/EventStore/EventStore.Core/Bus/QueuedHandlerMRES.cs
@@ -31,6 +31,7 @@
using EventStore.Common.Utils;
using EventStore.Core.Messaging;
using EventStore.Core.Services.Monitoring.Stats;
+using System.Threading.Tasks;
namespace EventStore.Core.Bus
{
@@ -54,7 +55,7 @@ public class QueuedHandlerMRES : IQueuedHandler, IHandle<Message>, IPublisher, I
private readonly Common.Concurrent.ConcurrentQueue<Message> _queue = new Common.Concurrent.ConcurrentQueue<Message>();
private readonly ManualResetEventSlim _msgAddEvent = new ManualResetEventSlim(false);
- private Thread _thread;
+ private Task _thread;
private volatile bool _stop;
private readonly ManualResetEventSlim _stopped = new ManualResetEventSlim(true);
private readonly TimeSpan _threadStopWaitTimeout;
@@ -92,7 +93,7 @@ public void Start()
_stopped.Reset();
- _thread = new Thread(ReadFromQueue) { IsBackground = true, Name = Name };
+ _thread = new Task(ReadFromQueue, TaskCreationOptions.LongRunning);
_thread.Start();
}
View
5 src/EventStore/EventStore.Core/Bus/QueuedHandlerPulse.cs
@@ -31,6 +31,7 @@
using EventStore.Common.Utils;
using EventStore.Core.Messaging;
using EventStore.Core.Services.Monitoring.Stats;
+using System.Threading.Tasks;
namespace EventStore.Core.Bus
{
@@ -53,7 +54,7 @@ public class QueuedHandlerPulse : IQueuedHandler, IHandle<Message>, IPublisher,
private readonly Common.Concurrent.ConcurrentQueue<Message> _queue = new Common.Concurrent.ConcurrentQueue<Message>();
- private Thread _thread;
+ private Task _thread;
private volatile bool _stop;
private readonly ManualResetEventSlim _stopped = new ManualResetEventSlim(true);
private readonly TimeSpan _threadStopWaitTimeout;
@@ -92,7 +93,7 @@ public void Start()
_stopped.Reset();
- _thread = new Thread(ReadFromQueue) { IsBackground = true, Name = Name };
+ _thread = new Task(ReadFromQueue, TaskCreationOptions.LongRunning);
_thread.Start();
}
View
5 src/EventStore/EventStore.Core/Bus/QueuedHandlerSleep.cs
@@ -31,6 +31,7 @@
using EventStore.Common.Utils;
using EventStore.Core.Messaging;
using EventStore.Core.Services.Monitoring.Stats;
+using System.Threading.Tasks;
namespace EventStore.Core.Bus
{
@@ -53,7 +54,7 @@ public class QueuedHandlerSleep : IQueuedHandler, IHandle<Message>, IPublisher,
private readonly Common.Concurrent.ConcurrentQueue<Message> _queue = new Common.Concurrent.ConcurrentQueue<Message>();
- private Thread _thread;
+ private Task _thread;
private volatile bool _stop;
private readonly ManualResetEventSlim _stopped = new ManualResetEventSlim(true);
private readonly TimeSpan _threadStopWaitTimeout;
@@ -91,7 +92,7 @@ public void Start()
_stopped.Reset();
- _thread = new Thread(ReadFromQueue) {IsBackground = true, Name = Name};
+ _thread = new Task(ReadFromQueue, TaskCreationOptions.LongRunning);
_thread.Start();
}
View
5 src/EventStore/EventStore.Core/Bus/QueuedHandlerThreadPool.cs
@@ -31,6 +31,7 @@
using EventStore.Common.Utils;
using EventStore.Core.Messaging;
using EventStore.Core.Services.Monitoring.Stats;
+using System.Threading.Tasks;
namespace EventStore.Core.Bus
{
@@ -98,7 +99,7 @@ public void Stop()
_queueMonitor.Unregister(this);
}
- private void ReadFromQueue(object o)
+ private void ReadFromQueue()
{
bool proceed = true;
while (proceed)
@@ -150,7 +151,7 @@ public void Publish(Message message)
Ensure.NotNull(message, "message");
_queue.Enqueue(message);
if (Interlocked.CompareExchange(ref _isRunning, 1, 0) == 0)
- ThreadPool.QueueUserWorkItem(ReadFromQueue);
+ Task.Factory.StartNew(ReadFromQueue);
}
public void Handle(Message message)
View
5 src/EventStore/EventStore.Core/Index/TableIndex.cs
@@ -33,6 +33,7 @@
using EventStore.Common.Log;
using EventStore.Common.Utils;
using EventStore.Core.Exceptions;
+using System.Threading.Tasks;
namespace EventStore.Core.Index
{
@@ -185,11 +186,11 @@ public void AddEntries(long commitPos, IList<IndexEntry> entries)
{
_backgroundRunningEvent.Reset();
_backgroundRunning = true;
- ThreadPool.QueueUserWorkItem(x => ReadOffQueue());
+ Task.Factory.StartNew(() => ReadOffQueue());
}
if (_additionalReclaim)
- ThreadPool.QueueUserWorkItem(x => ReclaimMemoryIfNeeded(_awaitingMemTables));
+ Task.Factory.StartNew(() => ReclaimMemoryIfNeeded(_awaitingMemTables));
}
}
}
View
3  src/EventStore/EventStore.Core/Services/Monitoring/Utils/ShellExecutor.cs
@@ -32,6 +32,7 @@
using System.Threading;
using EventStore.Common.Utils;
using EventStore.Transport.Http;
+using System.Threading.Tasks;
namespace EventStore.Core.Services.Monitoring.Utils
{
@@ -85,7 +86,7 @@ public void GetOutputAsync(Action<Exception, string> callback)
};
// note MM: takes time to start a process. no async API available
- ThreadPool.QueueUserWorkItem(_ =>
+ Task.Factory.StartNew(() =>
{
Stream inputStream;
try
View
7 src/EventStore/EventStore.Core/Services/Storage/StorageChaser.cs
@@ -37,6 +37,7 @@
using EventStore.Core.TransactionLog;
using EventStore.Core.TransactionLog.Checkpoint;
using EventStore.Core.TransactionLog.LogRecords;
+using System.Threading.Tasks;
namespace EventStore.Core.Services.Storage
{
@@ -56,7 +57,7 @@ public class StorageChaser : IMonitoredQueue,
private readonly ITransactionFileChaser _chaser;
private readonly IReadIndex _readIndex;
private readonly IPEndPoint _vnodeEndPoint;
- private Thread _thread;
+ private Task _thread;
private volatile bool _stop;
private readonly QueueStatsCollector _queueStats = new QueueStatsCollector("Storage Chaser");
@@ -84,9 +85,7 @@ public StorageChaser(IPublisher masterBus, ICheckpoint writerCheckpoint, ITransa
public void Handle(SystemMessage.SystemInit message)
{
- _thread = new Thread(ChaseTransactionLog);
- _thread.IsBackground = true;
- _thread.Name = Name;
+ _thread = new Task(ChaseTransactionLog, TaskCreationOptions.LongRunning);
}
public void Handle(SystemMessage.SystemStart message)
View
3  src/EventStore/EventStore.Core/Services/Storage/StorageScavenger.cs
@@ -33,6 +33,7 @@
using EventStore.Core.Messages;
using EventStore.Core.Services.Storage.ReaderIndex;
using EventStore.Core.TransactionLog.Chunks;
+using System.Threading.Tasks;
namespace EventStore.Core.Services.Storage
{
@@ -62,7 +63,7 @@ public void Handle(SystemMessage.ScavengeDatabase message)
return;
_isScavengingRunning = true;
}
- ThreadPool.QueueUserWorkItem(_ => Scavenge());
+ Task.Factory.StartNew(() => Scavenge());
}
private void Scavenge()
View
8 src/EventStore/EventStore.Core/Services/TimerService/ThreadBasedScheduler.cs
@@ -31,6 +31,7 @@
using EventStore.Core.Bus;
using EventStore.Core.DataStructures;
using EventStore.Core.Services.Monitoring.Stats;
+using System.Threading.Tasks;
namespace EventStore.Core.Services.TimerService
{
@@ -43,7 +44,7 @@ public class ThreadBasedScheduler : IMonitoredQueue, IScheduler, IDisposable
private readonly ITimeProvider _timeProvider;
- private readonly Thread _timerThread;
+ private readonly Task _timerThread;
private volatile bool _stop;
private readonly QueueStatsCollector _queueStats = new QueueStatsCollector("Timer");
@@ -53,9 +54,8 @@ public ThreadBasedScheduler(ITimeProvider timeProvider)
Ensure.NotNull(timeProvider, "timeProvider");
_timeProvider = timeProvider;
- _timerThread = new Thread(DoTiming);
- _timerThread.IsBackground = true;
- _timerThread.Name = Name;
+ _timerThread = new Task(DoTiming,TaskCreationOptions.LongRunning);
+
_timerThread.Start();
}
View
3  src/EventStore/EventStore.Core/TransactionLog/Chunks/TFChunkManager.cs
@@ -32,6 +32,7 @@
using EventStore.Common.Log;
using EventStore.Common.Utils;
using System.Linq;
+using System.Threading.Tasks;
namespace EventStore.Core.TransactionLog.Chunks
{
@@ -88,7 +89,7 @@ private void EnsureBackgroundWorkerRunning()
{
if (_backgroundRunning)
return;
- ThreadPool.QueueUserWorkItem(_ => BackgroundProcessing());
+ Task.Factory.StartNew(() => BackgroundProcessing());
}
}
View
5 src/EventStore/EventStore.Projections.Core.Tests/Playground/LaunchpadBase.cs
@@ -34,6 +34,7 @@
using EventStore.Common.Log;
using EventStore.Core.Bus;
using EventStore.Core.Messaging;
+using System.Threading.Tasks;
namespace EventStore.Projections.Core.Tests.Playground
{
@@ -70,8 +71,8 @@ public void Dispose()
{ApplicationBase = AppDomain.CurrentDomain.BaseDirectory, ConfigurationFile = executable + ".config",};
var appDomain = AppDomain.CreateDomain(
Path.GetFileNameWithoutExtension(executable), AppDomain.CurrentDomain.Evidence, setup);
- ThreadPool.QueueUserWorkItem(
- state =>
+ Task.Factory.StartNew(
+ () =>
{
try
{
View
4 src/EventStore/EventStore.Projections.v8Integration/EventStore.Projections.v8Integration.vcxproj
@@ -28,23 +28,27 @@
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
+ <PlatformToolset>v110</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
+ <PlatformToolset>v110</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
+ <PlatformToolset>v110</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
+ <PlatformToolset>v110</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
View
5 src/EventStore/EventStore.TestClient/Client.cs
@@ -40,6 +40,7 @@
using EventStore.Transport.Tcp.Formatting;
using EventStore.Transport.Tcp.Framing;
using Connection = EventStore.Transport.Tcp.TcpTypedConnection<byte[]>;
+using System.Threading.Tasks;
namespace EventStore.TestClient
{
@@ -125,7 +126,7 @@ public int Run()
if (!InteractiveMode)
return Execute(Options.Command.ToArray());
- new Thread(() =>
+ Task.Factory.StartNew(() =>
{
Thread.Sleep(100);
Console.Write(">>> ");
@@ -154,7 +155,7 @@ public int Run()
Console.Write(">>> ");
}
}
- }) { IsBackground = true, Name = "Client Main Loop Thread" }.Start();
+ },TaskCreationOptions.LongRunning);
return 0;
}
View
3  src/EventStore/EventStore.TestClient/CommandsProcessor.cs
@@ -31,6 +31,7 @@
using System.Text;
using System.Threading;
using EventStore.Common.Log;
+using System.Threading.Tasks;
namespace EventStore.TestClient
{
@@ -82,7 +83,7 @@ public bool TryProcess(CommandProcessorContext context, string[] args, out int e
int exitC = 0;
var executedEvent = new AutoResetEvent(false);
- ThreadPool.QueueUserWorkItem(_ =>
+ Task.Factory.StartNew(() =>
{
try
{
Something went wrong with that request. Please try again.