Inside 206-105

Existential Pontification and Generalized Abstract Digressions

C

C. The damage and surgical precision of a nuclear warhead.

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 […]

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> […]

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 […]

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 […]

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 […]

Safety first: FFI and threading

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> #-} {-# LANGUAGE ForeignFunctionInterface #-} module FfiExample where import Foreign.C -- get the C types -- pure function -- "unsafe" means it's slightly faster but […]

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, […]

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; […]

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 […]