-
Notifications
You must be signed in to change notification settings - Fork 570
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into async-support
- Loading branch information
Showing
8 changed files
with
479 additions
and
356 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using System; | ||
using System.Threading; | ||
|
||
namespace FASTER.core | ||
{ | ||
class FixedPool<T> : IDisposable where T : IDisposable | ||
{ | ||
readonly T[] items; | ||
readonly int[] owners; | ||
readonly int size; | ||
readonly Func<T> creator; | ||
bool disposed = false; | ||
|
||
public FixedPool(int size, Func<T> creator) | ||
{ | ||
items = new T[size]; | ||
owners = new int[size]; | ||
this.size = size; | ||
this.creator = creator; | ||
} | ||
|
||
public (T, int) Get() | ||
{ | ||
while (true) | ||
{ | ||
for (int i=0; i<size; i++) | ||
{ | ||
if (disposed) | ||
throw new Exception("Disposed"); | ||
|
||
var val = owners[i]; | ||
if (val == 0) | ||
{ | ||
if (Interlocked.CompareExchange(ref owners[i], 2, val) == val) | ||
{ | ||
items[i] = creator(); | ||
return (items[i], i); | ||
} | ||
} | ||
else if (val == 1) | ||
{ | ||
if (Interlocked.CompareExchange(ref owners[i], 2, val) == val) | ||
{ | ||
return (items[i], i); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void Return(int offset) | ||
{ | ||
if (!disposed) | ||
Interlocked.CompareExchange(ref owners[offset], 1, 2); | ||
else | ||
{ | ||
if (Interlocked.CompareExchange(ref owners[offset], -1, 2) == 2) | ||
items[offset].Dispose(); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
disposed = true; | ||
bool done = false; | ||
|
||
while (!done) | ||
{ | ||
done = true; | ||
|
||
for (int i = 0; i < size; i++) | ||
{ | ||
var val = owners[i]; | ||
if (val == 0) | ||
{ | ||
if (Interlocked.CompareExchange(ref owners[i], -1, val) != val) | ||
done = false; | ||
} | ||
else if (val == 1) | ||
{ | ||
done = false; | ||
if (Interlocked.CompareExchange(ref owners[i], -1, val) == val) | ||
items[i].Dispose(); | ||
} | ||
else if (val == 2) | ||
{ | ||
done = false; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.