Skip to content

Instantly share code, notes, and snippets.

View cfsamson's full-sized avatar

Carl Fredrik Samson cfsamson

View GitHub Profile
@cfsamson
cfsamson / iocp_abstraction.rs
Created August 19, 2019 00:13
Ide on how to handle IOCP <-> kqueue abstraction
use std::thread;
use std::sync::{Arc, Mutex};
fn main() {
let mut stream = TcpStream::new(1);
let mut poll = Poll::new();
poll.register(&stream);
@cfsamson
cfsamson / higher_order_trait_bounds.rs
Created August 5, 2019 21:01
Rust smart snippets
trait DisplayTrait {}
struct DisplayImpl<'a> { context: &'a mut () }
struct ViewImpl;
impl<D: DisplayTrait> ViewTrait<D> for ViewImpl {
fn display(&mut self, _: &D) {}
}
@cfsamson
cfsamson / cross_platform_syscall.rs
Last active September 15, 2019 21:20
Futures Code Samples
use std::io;
fn main() {
let sys_message = String::from("Hello world from syscall!\n");
syscall(sys_message).unwrap();
}
// and: http://man7.org/linux/man-pages/man2/write.2.html
#[cfg(not(target_os = "windows"))]
#[link(name = "c")]
@cfsamson
cfsamson / working_windows_context_switch.cr
Last active July 24, 2019 00:51
Working Windows Context Switch in Crystal
# TODO: Save XMM6-XMM15 as well
STACK_SIZE = 1024 * 1024 * 8
SLEEP_INTERVAL = 0.2 # give the Windows some time to kill the process if NT_TIB is not set correctly
# Simple Context Switch Implementation for Crystal
# The two interesting functions is `FiberPoc.makecontext` and `Scheduler.swapcontext`.
# Setting up WSL and cross compile for Win 10 see:
# https://github.com/crystal-lang/crystal/wiki/Porting-to-Windows
@cfsamson
cfsamson / crystal_minimal_example.cr
Last active July 2, 2019 23:08
Trying to create the minimal example needed for the problem with Crystals windows context switch
STACK_SIZE = 1024*1024*8
class Test
property value : UInt64 = 0
property stack = Array(UInt8).new(STACK_SIZE)
property func : Proc(Void) = ->{}
property rsp : UInt64 = 0
def makecontext(proc : Proc)
@func = proc
STACK_SIZE = 1024 * 1024 * 8
SLEEP_INTERVAL = 0.5 # give Windows some time to kill the process if NT_TIB is not set correctly
# Simple Context Switch Implementation for Crystal
# The two interesting functions is `FiberPoc.makecontext` and `Scheduler.swapcontext`.
# Setting up WSL and cross compile for Win 10 see:
# https://github.com/crystal-lang/crystal/wiki/Porting-to-Windows
# https://github.com/crystal-lang/crystal/issues/7932
@cfsamson
cfsamson / closer_to_crystal_impl.cr
Last active July 1, 2019 20:41
Minimum code for proving the context switch assebly on Windows
STACK_SIZE = 1024 * 1024 * 8
PUSH_POP_REG_COUNT = 7
class FiberPoc
@stack : Array(UInt8)
property rsp : UInt8* = Pointer(UInt8).null
property f = Proc(Void).new { }
def initialize
@stack = Array.new(STACK_SIZE, 0_u8) # OK, as long as we don't push/pop -> reallocate
@cfsamson
cfsamson / green_threads_start.rs
Last active January 7, 2021 05:33
Green_threads_code
#![feature(asm)]
// Lets set a small stack size here, only 48 bytes so we can print the stack
// and look at it before we switch contexts
// ===== NOTICE FOR OSX USERS =====
// You'll need to increase this size to at least 624 bytes. This will work in Rust Playground and on Windows
// but the extremely small stack seems to have an issue on OSX.
const SSIZE: isize = 48;
/// Do you recognize these? It's the registers described in the x86-64 ABI that we'll need to save our context.
//! Database-related logic, such as creating connections, CRUD on data
use futures::*;
use futures_state_stream::StateStream;
use tiberius::{BoxableIo, query::QueryRow, SqlConnection};
// I just uncomment so RLS is happy
// use crate::proto::rolemanagement::v1::*;
// lets return a future here instead that we can then chain future futures onto
@cfsamson
cfsamson / raytracer_memincrease_fix_2.rs
Last active March 9, 2019 17:52
raytracer_memincrease_fix_2
// For the sake of brevity I won't repeat the code we replace here instead point out
// that this replaces lines 307-328 in our original code
const BYTES_PER_PIXEL: usize = 3;
fn main() {
// {...}
// first allocate a zeroed Vec<u8> to hold our results.
// btw: this initialization method is the fastest way to ask
// for zeroed memory: see https://github.com/rust-lang/rust/issues/54628
let mut bytes = vec![0u8; h as usize * w as usize * BYTES_PER_PIXEL];