-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! Avoid buffer race conditions in CGroups
- Loading branch information
Showing
2 changed files
with
88 additions
and
48 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,45 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using Microsoft.Extensions.ObjectPool; | ||
|
||
#pragma warning disable CA1716 | ||
namespace Microsoft.Shared.Pools; | ||
#pragma warning restore CA1716 | ||
|
||
/// <summary> | ||
/// Represents a buffer writer that can be automatically returned to an object pool upon dispose. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the elements in the buffer.</typeparam> | ||
#if !SHARED_PROJECT | ||
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] | ||
#endif | ||
internal readonly struct ReturnableBufferWriter<T> : IDisposable | ||
{ | ||
private readonly ObjectPool<BufferWriter<T>> _pool; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ReturnableBufferWriter{T}"/> struct. | ||
/// </summary> | ||
/// <param name="pool">The object pool to return the buffer writer to.</param> | ||
public ReturnableBufferWriter(ObjectPool<BufferWriter<T>> pool) | ||
{ | ||
_pool = pool; | ||
Buffer = pool.Get(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the buffer writer. | ||
/// </summary> | ||
public BufferWriter<T> Buffer { get; } | ||
|
||
/// <summary> | ||
/// Disposes the buffer writer and returns it to the object pool. | ||
/// </summary> | ||
public readonly void Dispose() | ||
{ | ||
Buffer.Reset(); | ||
_pool.Return(Buffer); | ||
} | ||
} |