mirror of https://github.com/ogoun/Zero.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.4 KiB
92 lines
2.4 KiB
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
// Licensed under the MIT license.
|
|
|
|
using System.Threading;
|
|
using System.Runtime.InteropServices;
|
|
using System;
|
|
|
|
namespace FASTER.core
|
|
{
|
|
[StructLayout(LayoutKind.Explicit)]
|
|
struct AtomicOwner
|
|
{
|
|
[FieldOffset(0)]
|
|
int owner;
|
|
[FieldOffset(4)]
|
|
int count;
|
|
[FieldOffset(0)]
|
|
long atomic;
|
|
|
|
/// <summary>
|
|
/// Enqueue token
|
|
/// true: success + caller is new owner
|
|
/// false: success + someone else is owner
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool Enqueue()
|
|
{
|
|
while (true)
|
|
{
|
|
var older = this;
|
|
var newer = older;
|
|
newer.count++;
|
|
if (older.owner == 0)
|
|
newer.owner = 1;
|
|
|
|
if (Interlocked.CompareExchange(ref this.atomic, newer.atomic, older.atomic) == older.atomic)
|
|
{
|
|
return older.owner == 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dequeue token (caller is/remains owner)
|
|
/// true: successful dequeue
|
|
/// false: failed dequeue
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool Dequeue()
|
|
{
|
|
while (true)
|
|
{
|
|
var older = this;
|
|
var newer = older;
|
|
newer.count--;
|
|
|
|
if (Interlocked.CompareExchange(ref this.atomic, newer.atomic, older.atomic) == older.atomic)
|
|
{
|
|
return newer.count > 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Release queue ownership
|
|
/// true: successful release
|
|
/// false: failed release
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool Release()
|
|
{
|
|
while (true)
|
|
{
|
|
var older = this;
|
|
var newer = older;
|
|
|
|
if (newer.count > 0)
|
|
return false;
|
|
|
|
if (newer.owner == 0)
|
|
throw new Exception("Invalid release by non-owner thread");
|
|
newer.owner = 0;
|
|
|
|
if (Interlocked.CompareExchange(ref this.atomic, newer.atomic, older.atomic) == older.atomic)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|