Skip to content

Instantly share code, notes, and snippets.

View bblum's full-sized avatar

ben blum bblum

View GitHub Profile
@bblum
bblum / Proposal-for-effects.md
Last active May 10, 2019 06:37
bblum's old proposal for an effect system in rust (cf. https://github.com/rust-lang/rust/issues/3094). excavated from the commit history of https://github.com/rust-lang/rust-wiki-backup.

Goal

The goal is to be able to express the effects that a function may (or may not) have in a static way that the compiler can check. Possible effects include task failure, code marked "unsafe", garbage collection, dynamic allocation in general, file I/O, nondeterminism, task rescheduling, and mutation outside of one's own stack frame. There could also be the ability to add custom user-provided, domain-specific effects (that don't depend on language primitives).

To be feasible for Rust, an effect system will have to be:

  • Unobtrusive. A user who doesn't care about effects in their code should not have to write any effect annotations.
  • Lightweight. When used, effect annotations should be clear in meaning and not need to refer to other effects which aren't relevant.
  • Polymorphic. The system must express a higher-order function's effect depending on the effect of its argument closure. Otherwise that function would have to be assigned the top effect which would make it impossible to use anywh
@bblum
bblum / tsx_syscall.c
Last active January 6, 2019 05:27
what happens if you make even the smallest syscall during HTM
// compile with gcc -mrtm -lpthread (or -pthread)
#include <immintrin.h>
#include <sys/time.h> /* gettimeofday */
#include <sys/types.h> /* pid_t */
#include <sys/syscall.h> /* SYS_gettid */
#include <unistd.h> /* syscall */
#include <stdio.h>
#include <pthread.h>
#include <assert.h>
@bblum
bblum / fizzbuzz.c
Last active May 31, 2018 23:01
just a simple fizzbuzz implementation such as you would write in a programming interview
#define p pthread_create(&i,0,f,
f(char*s){for(usleep(s[1]*999);sleep(76-*s),printf(s););}
main(i){p"Fizz");p"Buzz");for(i=1;i<102;sleep(2),printf("\n%d\33[3D",i++));}
@bblum
bblum / tsx-need-barrier.c
Last active March 13, 2018 13:43
Demonstration of the need for a barrier in TSX failure paths.
// compile with gcc -mrtm -pthread
#include <immintrin.h>
#include <stdio.h>
#include <pthread.h>
#include <assert.h>
int count = 0;
int conflix = 0;
int prevent_txn = 0;
import Data.List
data Op = P | M | X | D deriving (Enum, Eq, Ord)
data Ex = Nobe Op Ex Ex | Leaf Rational deriving Ord
instance Show Ex where
show (Leaf x) = show $ floor x
show (Nobe o e1 e2) = "(" ++ show e1 ++ ["+-x/" !! fromEnum o] ++ show e2 ++ ")"
instance Eq Ex where
@bblum
bblum / gist:6243839
Last active December 21, 2015 03:39
struct R<'self> {
// This struct is needed to create the
// otherwise infinite type of a fn that
// accepts itself as argument:
c: &'self mut &'self fn(&R, bool)
}
fn innocent_looking_victim() {
let mut x = Some(~"hello");
do conspirator |f, writer| {
fn deschedule_running_task_and_then_inner(~self, fn(&mut Scheduler, Task)); // does not BlockedTask::try_block() the task
fn deschedule_running_task_and_then(~self, user_blk: fn(&mut Scheduler, BlockedTask) {
do self.deschedule_running_task_and_then_inner |sched, task| {
// remove this logic from fn run_cleanup_job() now that it will be here
match BlockedTask::try_block(task) {
Left(killed_task) => sched.enqueue_task(killed_task),
Right(blocked_task) => user_blk(sched, blocked_task),
}
}
@bblum
bblum / gist:6042762
Last active December 20, 2015 00:38
17 /// Trait for message-passing primitives that can be select()ed on.
18 pub trait Select {
19 // Returns true if data was available.
20 fn optimistic_check(&mut self) -> bool;
21 // Returns true if data was available. If so, shall also wake() the task.
22 fn block_on(&mut self, &mut Scheduler, BlockedTask) -> bool;
23 // Returns true if data was available.
24 fn unblock_from(&mut self) -> bool;
25 }
26
struct Hello;
impl Neg<Hello> for [Hello, ..0] {
fn neg(&self) -> Hello { Hello }
}
impl Not<()> for Hello {
fn not(&self) { }
}
@bblum
bblum / gist:5751707
Created June 10, 2013 19:53
trying, so far unsuccessfully, to exploit the lack of a value restriction on lifetime-polymorphic closures
fn expect_static(z: &'static int) {
}
fn foo<'f>(x: @mut Option<&'f fn(&'static int)>, y: &'f fn(&'static int)) {
*x = Some(y);
}
fn bar<'b>(x: @mut Option<&fn(&'b int)>) {
let z = 5;
match *x {