Skip to content

Instantly share code, notes, and snippets.

View ysbaddaden's full-sized avatar

Julien Portalier ysbaddaden

  • Crystal, Ruby, JavaScript
  • Amiens, France
  • X @ysbaddaden
View GitHub Profile
@ysbaddaden
ysbaddaden / MT.md
Last active October 30, 2018 15:31

Crystal MTs (Multi Threaded schedulers)

Goals:

  • developers must only care about fibers and channels;
  • crystal runtime should start 1 upto N threads;
  • each thread can take & resume any fiber in the loop;
  • channels as user-code sync primitives;
  • also propose fiber-aware mutex/monitors;
  • additional sync primitives (e.g. IO events, ...)
@ysbaddaden
ysbaddaden / sample.cr
Created August 17, 2018 22:57
Syslog Logger for Crystal (pure)
require "./syslog"
syslog = Syslog.new
syslog.info "bla bla bla"
# $ tail -n1 /var/log/syslog
# Aug 18 00:55:05 xps crystal-run-syslog.tmp[27911]: bla bla bla
@ysbaddaden
ysbaddaden / hasher_randomize_distributions_test.cr
Created October 21, 2017 12:49
Search for collisions and display distribution (libsdl2) of hashes for a list of patterns (e.g. words, uuids, numbers, ...) from STDIN
# Requires https://github.com/ysbaddaden/sdl.cr
require "sdl"
# Depends on the Crystal::Hasher implementation.
# Adjust to match the type of `"".hash`
alias HashType = UInt64
word_count = 0
maximum = 0
collisions = 0
diff --git a/src/exception.cr b/src/exception.cr
index b9bdc8a..c583f0b 100644
--- a/src/exception.cr
+++ b/src/exception.cr
@@ -44,9 +44,21 @@ struct CallStack
callstack = [] of Void*
backtrace_fn = ->(context : LibUnwind::Context, data : Void*) do
bt = data.as(typeof(callstack))
- ip = Pointer(Void).new(LibUnwind.get_ip(context))
+
@ysbaddaden
ysbaddaden / adns.cr
Created June 9, 2016 08:59
Asynchronous DNS resolver for Crystal (call getaddrinfo in thread + socket pair communication)
# # Asynchronous DNS resolver for Crystal (sort of)
#
# It consists on starting a thread that will run the blocking `getaddrinfo`
# syscall, thus allowing the main thread —the one running the event loop— to not
# stop while resolving domains, which can take from milliseconds to seconds.
# Communication with the thread is achieved through a socket pair, one for the
# main thread, and one for the resolver thread. Since we rely on socket, the
# event-loop will pause or resume fibers accordingly to messages sent or
# received from the socket.
#
@ysbaddaden
ysbaddaden / 001.txt
Last active May 25, 2016 08:45
Geographical Distances Benchmarks (Crystal)
# Benchmark run with Crystal 0.17.3 (LLVM 3.6) on "Intel Core i7-4712HQ CPU @ 2.30GHz" CPU
Brooklin (block)
haversine: 0.144098
sqrt: 0.19007 (+31.904%)
spheroid: 0.144098 (+1.59146e-09%)
ellipsoid: 0.188279 (+30.6604%)
Paris -> Marseille
haversine: 660.815
@ysbaddaden
ysbaddaden / adjectives.txt
Created March 7, 2016 18:34
Generate Ubuntu-like release names
abandoned
able
absolute
adorable
adventurous
academic
acceptable
acclaimed
accomplished
accurate
diff --git a/src/thread/lib_pthread.cr b/src/thread/lib_pthread.cr
index 5094260..96f2530 100644
--- a/src/thread/lib_pthread.cr
+++ b/src/thread/lib_pthread.cr
@@ -17,6 +17,7 @@ lib LibPThread
fun create = pthread_create(thread : Thread*, attr : Void*, start : Void* ->, arg : Void*) : Int32
fun exit = pthread_exit(value : Void*)
+ fun yield = pthread_yield() : Int32
fun join = pthread_join(thread : Thread, value : Void**) : Int32
@ysbaddaden
ysbaddaden / prax_sig_segfault.txt
Last active August 29, 2015 14:15
Prax Signal Segfault
Program received signal SIGPIPE, Broken pipe.
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffcd7f2700 (LWP 9702)]
0x0000000000000001 in ?? ()
(gdb) bt
#1 0x0000000000418491 in *Signal::handler<Int32>:(Nil | Void) () at /home/github/crystal/src/signal.cr:57
#2 <signal handler called>
#3 0x00007ffff772135d in write () at ../sysdeps/unix/syscall-template.S:81
@ysbaddaden
ysbaddaden / v102.ofx
Created October 7, 2014 14:06
Example OFX with multiple accounts
FXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:USASCII
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE