Skip to content

Instantly share code, notes, and snippets.

void Main()
{
}
public static class ChannelOpDsl
{
public static ChannelCommandQueue<TCommand> BeginCommandQueue<TCommand>(int bufferSize, bool multiWrite)
{
return new ChannelCommandQueue<TCommand>(bufferSize,multiWrite);
<Query Kind="Program">
<NuGetReference>LanguageExt.Core</NuGetReference>
<NuGetReference>System.Threading.Channels</NuGetReference>
<NuGetReference>ValueTaskSupplement</NuGetReference>
<Namespace>LanguageExt</Namespace>
<Namespace>System.Collections.Concurrent</Namespace>
<Namespace>System.Diagnostics.CodeAnalysis</Namespace>
<Namespace>System.Threading.Channels</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
<Namespace>ValueTaskSupplement</Namespace>
@to11mtm
to11mtm / SemiEsotericChannelBatchingHandler.cs
Last active August 22, 2022 21:21
Generic/Adaptable batching writer using System.Channels.
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
//This probably mostly works. [1]
//Basically an adaptation of akka.persistence.linq2db's stream setup
//To use channels instead. :D
async Task Main()
{
var tcs = new TaskCompletionSource();
var t = new Thread(async () =>
{
try
{
for (int i = 0; i < 2; i++)
{
Thread.CurrentThread.IsThreadPoolThread.Dump();
@to11mtm
to11mtm / UnrolledLinkedList.cs
Created November 5, 2022 20:10
Implementation of an UnrolledLinkedList in C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace GlutenFree.Collectons
{
public class UnrolledLinkedList<T> : IList<T>
{
private readonly int nodeCapacity = Node.NodeSize;
@to11mtm
to11mtm / SingleThreadedScheduler.cs
Created November 13, 2022 23:20 — forked from davidfowl/SingleThreadedScheduler.cs
A sample showing how to schedule work on a single thread
using System.Collections.Concurrent;
using System.Threading.Channels;
var channel = Channel.CreateUnbounded<int>();
var syncContext = new SingleThreadedSyncContext();
syncContext.Post(async _ =>
{
await foreach (var item in channel.Reader.ReadAllAsync())
<Query Kind="Program">
<Namespace>System.Threading.Channels</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
async Task Main()
{
var channel = Channel.CreateBounded<string>(100);
Func<CancellationTokenSource> newCts = ()=> new CancellationTokenSource();
@to11mtm
to11mtm / WaitOrCompletionLock.cs
Last active January 11, 2024 03:49
An example of using Channels to provide wait/completion locks.
using System;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
namespace GlutenFree.Synchronizaton
{
public class WaitOrCompletionLock
{
private readonly Channel<int> lockChannel = Channel.CreateBounded<int>(1);
//USE AT OWN RISK, PORTED UNTESTED CODE
/// <devremarks>
/// vaguely based on
/// https://github.com/l-tamas/Unrolled-linked-list/blob/master/src/org/megatherion/util/collections/UnrolledLinkedList.java
/// </devremarks>
public class UnrolledLinkedList<T> //: IList<T>
: IEnumerable<T>
{
private readonly int nodeCapacity = Node.NodeSize;
@to11mtm
to11mtm / Linq2Db-Samples.linq
Created November 8, 2023 05:45
Linq2Db Sample Set Used in Intros/Howtos
<Query Kind="Program">
<NuGetReference>linq2db</NuGetReference>
<NuGetReference>Microsoft.Data.Sqlite.Core</NuGetReference>
<NuGetReference>SQLitePCLRaw.bundle_e_sqlite3</NuGetReference>
<Namespace>LinqToDB</Namespace>
<Namespace>LinqToDB.Data</Namespace>
<Namespace>LinqToDB.Mapping</Namespace>
<Namespace>Microsoft.Data.Sqlite</Namespace>
<UseNoncollectibleLoadContext>true</UseNoncollectibleLoadContext>
<CopyLocal>true</CopyLocal>