Skip to content

Instantly share code, notes, and snippets.

View rogeralsing's full-sized avatar
😈
Feeling Awesome

Roger Johansson rogeralsing

😈
Feeling Awesome
View GitHub Profile
@rogeralsing
rogeralsing / RingbufferReaderWriter.cs
Last active January 1, 2016 08:58
Attempt to get fast thread to thread communication using ring buffers and no locks.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
@rogeralsing
rogeralsing / gist:8313019
Last active January 2, 2016 13:49
Concurrent mailbox C#. Need help in how to schedule the run handler in the Post method, with the criteria that it may not be scheduled concurrently. "run" may only run in one thread at a time.. The current implementation works but hogs the system when there are no messages in any of the queues. And I'd like this to be as efficient as possible, s…
public class ConcurrentQueueMailbox : Mailbox
{
private ConcurrentQueue<Envelope> userMessages = new ConcurrentQueue<Envelope>();
private ConcurrentQueue<Envelope> systemMessages = new ConcurrentQueue<Envelope>();
private WaitCallback run = null;
public ConcurrentQueueMailbox()
{
run = new WaitCallback(_ =>
{
@rogeralsing
rogeralsing / gist:8314112
Last active January 2, 2016 13:59
Is this safe?
public class Mailbox
{
private ConcurrentQueue<Envelope> queue = new ConcurrentQueue<Envelope>();
private int scheduled = 0;
private volatile bool hasUnScheduledMessages = false;
private WaitCallback run;
public Mailbox ()
{
@rogeralsing
rogeralsing / gist:8318627
Last active January 2, 2016 14:39
fast mailbox
public class Mailbox
{
private static class MailboxStatus
{
public const int Idle = 0;
public const int Busy = 1;
}
private readonly ConcurrentQueue<Envelope> _queue = new ConcurrentQueue<Envelope>();
private volatile bool _hasUnScheduledMessages;
@rogeralsing
rogeralsing / ThreadPoolguardian.cs
Last active January 3, 2016 13:59
ThreadPoolGuardian, kills of zombie processes that run for too long.
public static class ThreadPoolGuardian
{
private static readonly BlockingCollection<Guard> guards = new BlockingCollection<Guard>();
public static void Queue(Action action)
{
Action wrapper = () =>
{
var guard = new Guard
{
@rogeralsing
rogeralsing / gist:8684159
Last active February 29, 2016 11:33
Akka config parser
using System;
using System.Collections.Generic;
using System.Text;
namespace ConfigParser
{
internal class Program
{
private static void Main(string[] args)
{
var chatMessages = GetObservableOfChatMessages();
var chatIsIdle = chatMessages. _dontknow_ (TimeSpan.FromSeconds(5));
chatMessages.Subscribe (msg => Console.WriteLine("{0} says: {1}",msg.User,msg.Text));
//I want this to occur every time the chatmessages observable have been silent for 5 sec
//not just once
chatIsIdle.Subscribe ( ... => Console.WriteLine("Chat is idle...");
public class Foo<T> where T: IDoStuff
{
private T _stuffer;
public Foo(T stuffer)
{
_stuffer = stuffer;
}
public void Bar()
@rogeralsing
rogeralsing / gist:73bb7441c1e794e7497c
Created January 29, 2015 13:27
activator vs expression
using System;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace ConsoleApplication10
{
internal delegate T ObjectActivator<out T>(params object[] args);
@rogeralsing
rogeralsing / gist:26d0dcbc7dd7b2924074
Last active August 29, 2015 14:14
AsyncAwaitActors
using System;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.TestKit;
using Xunit;
namespace Akka.Tests.Dispatch
{
public class AsyncAwaitActor : ReceiveActor
{