Skip to content

Instantly share code, notes, and snippets.

View bblum's full-sized avatar

ben blum bblum

View GitHub Profile
1287 #[test]
1288 #[should_fail]
1289 #[ignore(cfg(windows))]
1290 fn test_tls_cleanup_on_failure() unsafe {
1291 fn str_key(+_x: @str) { }
1292 fn box_key(+_x: @@()) { }
1293 fn int_key(+_x: @int) { }
1294 local_data_set(str_key, @"parent data");
1295 local_data_set(box_key, @@());
1296 task::spawn{|| // spawn_linked
@bblum
bblum / map_send.rs
Created July 5, 2012 23:06
Incomplete adaptation of std::map to be sendable (~ptrs only, no @boxes).
//! A map type
import chained::hashmap;
export hashmap, hashfn, eqfn, set, map, chained, hashmap, str_hash;
export unique_str_hash;
export bytes_hash, int_hash, uint_hash, set_add;
export hash_from_vec, hash_from_strs, hash_from_bytes;
export hash_from_ints, hash_from_uints;
export vec_from_set;
@bblum
bblum / rust_cond_lock.cpp
Created August 2, 2012 22:59
Current progress on multi-waiter-enabled condition variables that can be exposed to rust.
/****************************************************************************
* A rust-scheduler-enabled concurrency primitive for mutual exclusion and
* blocking/signalling.
*
* This code is intimately linked with task killing, so interacts with tasks'
* kill_locks in a complicated way. Because killing a task also needs to punt
* it off of cvar wait queues, it needs to take the cvar's lock too.
*
* NB: Rules for locking order & data ownership are as follows:
*
@bblum
bblum / gist:3279078
Created August 6, 2012 22:30
Idea for weakening shared state tasks
fn shared_state_task(..., other_ch: comm::chan<()>) {
while ... {
...
match msg {
...
deref {
...
if new_refcount == 0 { break; }
}
}
@bblum
bblum / codify.sh
Created August 16, 2012 18:36
Turns raw rust code into syntax hilighted latex.
#!/bin/bash
# Use with extreme caution.
# You will probably need to \definecolor several of these.
IFS='\n'
while read line; do
echo -n "\\texttt{"
echo -n "$line" | sed 's/{/\\{/g' | sed 's/}/\\}/g' | sed 's/_/\\_/g' | sed 's/~/\\textasciitilde/g' | sed 's/ /~/g' | sed 's/^\t*//' | sed 's/%/\\%/g' | sed 's/&/\\\&/g' | \
sed 's/\<fn\>/\\hilight{brown}{fn}/g' | \
sed 's/\<do\>/\\hilight{brown}{do}/g' | \
@bblum
bblum / gist:3612070
Created September 3, 2012 18:42
for digitalknight, a scheme of how to spawn a global memory watcher task.
fn send_message_to_memory_watcher() {
// this is code that all tasks will run
let chan = get_memory_watcher_chan();
chan.send(...message...);
}
fn get_memory_watcher_chan() -> shared_chan<MemoryWatcherMessage> {
let shared_chan_ptr = rustrt::get_global_shared_chan_ptr();
// Double-check locking. (look this up on wikipedia if you don't know what
// it means)
@bblum
bblum / coerce.rs
Created September 24, 2012 15:54
fn coerce(A) -> B using shared mutable state. Prints "hello world".
use either::{Either,Left,Right};
// SharedMutableState is an unsafe internal library used by both the ARC and the RWARC.
use private::{SharedMutableState, shared_mutable_state, clone_shared_mutable_state,
get_shared_mutable_state, get_shared_immutable_state};
type ARC<T: Send> = SharedMutableState<T>;
fn ARC<T: Send>(data: T) -> ARC<T> unsafe { shared_mutable_state(data) }
fn clone<T: Send>(x: &ARC<T>) -> ARC<T> unsafe { clone_shared_mutable_state(x) }
fn get<T: Send>(x: &a/ARC<T>) -> &a/T unsafe { get_shared_immutable_state(x) }
@bblum
bblum / List.hs
Created October 13, 2012 15:36
An attempt at translating https://gist.github.com/3885017 to haskell that doesn't compile.
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
module Main where
import Control.Monad (when)
import Control.Applicative ((<$>))
class Nat n
newtype Zero = Zero ()
@bblum
bblum / gist:3885017
Created October 13, 2012 15:34
Lists with lengths in their types.
trait Nat { }
enum Zero = ();
enum Suc<N: Nat> = N;
impl
// ----------
Zero : Nat { }
impl <N: Nat>
@bblum
bblum / gist:5719126
Last active December 18, 2015 03:38
returning a stack closure
fn foo<'a>(blk: &fn(bar: &'a fn() -> &'a fn())) {
let x = 5;
let bar: &fn() = || { io::println(fmt!("%d", x)); };
do blk || { bar };
}
fn main() {
do foo |bar| { bar()(); }
}