Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion SmartThreadPool/SmartThreadPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ internal static void ValidateWorkItemsGroupWaitForIdle(IWorkItemsGroup workItems
ValidateWorkItemsGroupWaitForIdleImpl(workItemsGroup, workItem);
if ((null != workItemsGroup) &&
(null != workItem) &&
CurrentThreadEntry.CurrentWorkItem.WasQueuedBy(workItemsGroup))
workItem.WasQueuedBy(workItemsGroup))
{
throw new NotSupportedException("WaitForIdle cannot be called from a thread on its SmartThreadPool, it causes a deadlock");
}
Expand Down Expand Up @@ -1474,6 +1474,11 @@ public static bool IsWorkItemCanceled
{
get
{
if (CurrentThreadEntry == null || CurrentThreadEntry.CurrentWorkItem == null)
{
throw new InvalidOperationException(
"IsWorkItemCanceled can only be called from a work item executing in the SmartThreadPool");
}
return CurrentThreadEntry.CurrentWorkItem.IsCanceled;
}
}
Expand Down
8 changes: 7 additions & 1 deletion SmartThreadPool/SynchronizedDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ public SynchronizedDictionary()

public int Count
{
get { return _dictionary.Count; }
get
{
lock (_lock)
{
return _dictionary.Count;
}
}
}

public bool Contains(TKey key)
Expand Down
37 changes: 23 additions & 14 deletions SmartThreadPool/WorkItemsGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,14 @@ public override int Concurrency
{
Debug.Assert(value > 0);

int diff = value - _concurrency;
_concurrency = value;
if (diff > 0)
lock (_lock)
{
EnqueueToSTPNextNWorkItem(diff);
int diff = value - _concurrency;
_concurrency = value;
if (diff > 0)
{
EnqueueToSTPNextNWorkItem(diff);
}
}
}
}
Expand Down Expand Up @@ -165,14 +168,17 @@ public override WIGStartInfo WIGStartInfo
/// </summary>
public override void Start()
{
// If the Work Items Group already started then quit
if (!_isSuspended)
lock (_lock)
{
return;
// If the Work Items Group already started then quit
if (!_isSuspended)
{
return;
}
_isSuspended = false;

EnqueueToSTPNextNWorkItem(Math.Min(_workItemsQueue.Count, _concurrency));
}
_isSuspended = false;

EnqueueToSTPNextNWorkItem(Math.Min(_workItemsQueue.Count, _concurrency));
}

public override void Cancel(bool abortExecution)
Expand Down Expand Up @@ -219,12 +225,15 @@ private void RegisterToWorkItemCompletion(IWorkItemResult wir)

public void OnSTPIsStarting()
{
if (_isSuspended)
lock (_lock)
{
return;
if (_isSuspended)
{
return;
}

EnqueueToSTPNextNWorkItem(_concurrency);
}

EnqueueToSTPNextNWorkItem(_concurrency);
}

public void EnqueueToSTPNextNWorkItem(int count)
Expand Down
9 changes: 6 additions & 3 deletions SmartThreadPool/WorkItemsQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -582,11 +582,14 @@ public void Dispose()

public void Dispose()
{
if (!_isDisposed)
lock (this)
{
Cleanup();
if (!_isDisposed)
{
Cleanup();
_isDisposed = true;
}
}
_isDisposed = true;
}

private void ValidateNotDisposed()
Expand Down