ezyang’s blog

the arc of software bends towards understanding

C

Towards platform-agnostic interruptibility

Last post, I talked about some of the notable difficulties in emulating pthread_cancel on Windows. Today, I want to talk about what a platform agnostic compiler like GHC actually ought to do. Recall our three design goals: GHC would like to be able to put blocking IO calls on a worker thread but cancel them […]

  • September 10, 2010

pthread_cancel on Windows

Edward, I’m afraid I have some bad news. Your interruptible GHC patch; it was involved in a terrible accident on the way to Windows portability. I hope you understand: we’re doing our best to patch it up, but there have been some complications... Pop quiz! What does this pthreads code do? #include <pthread.h> #include <stdio.h> […]

  • September 8, 2010

Interrupting GHC

In my tech talk about abcBridge, one of the “unsolved” problems I had with making FFI code usable as ordinary Haskell code was interrupt handling. Here I describe an experimental solution involving a change to the GHC runtime system as suggested by Simon Marlow. The introductory section may be interesting to practitioners looking for working […]

  • August 27, 2010

Buffered streams and iteratees

While attempting to figure out how I might explain lazy versus strict bytestrings in more depth without boring half of my readership to death, I stumbled upon a nice parallel between a standard implementation of buffered streams in imperative languages and iteratees in functional languages. No self-respecting input/output mechanism would find itself without buffering. Buffering […]

  • August 4, 2010

Managing foreign pointers effectively

Foreign.ForeignPtr is a magic wand you can wave at C libraries to make them suddenly garbage collected. It’s not quite that simple, but it is pretty darn simple. Here are a few quick tips from the trenches for using foreign pointers effectively with the Haskell FFI: Use them as early as possible. As soon as […]

  • July 23, 2010

Safety first: FFI and threading

Update. While this blog post presents two true facts, it gets the causal relationship between the two facts wrong. Here is the correction. Attention conservation notice. Don’t use unsafe in your FFI imports! We really mean it! Consider the following example in from an old version of Haskellwiki’s FFI introduction: {-# INCLUDE <math.h> #-} {-# […]

  • July 9, 2010

You could have invented zippers

In the beginning, there was a binary tree: struct ctree { // c for children int val; struct ctree *left; struct ctree *right; } The flow of pointers ran naturally from the root of the tree to the leaves, and it was easy as blueberry pie to walk to the children of a node. Unfortunately, […]

  • April 7, 2010

Replacing small C programs with Haskell

C is the classic go-to tool for small programs that need to be really fast. When scripts.mit.edu needed a small program to be a glorified cat that also added useful HTTP headers to the beginning of its output, there was no question about it: it would be written in C, and it would be fast; […]

  • March 10, 2010

Cute macro tricks in the kernel

A classic stylistic tip given to C programmers is that inline functions should be preferred over macros, when possible. This advice stems from the fact that a macro and an inline function can achieve the same effect, but the inline function also gets type checking. As it turns out, you can achieve static type checking […]

  • February 1, 2010