Skip to content

Instantly share code, notes, and snippets.

@acfoltzer
Created March 9, 2012 05:42
Show Gist options
  • Save acfoltzer/2005212 to your computer and use it in GitHub Desktop.
Save acfoltzer/2005212 to your computer and use it in GitHub Desktop.
Haskell affinity
{-# LANGUAGE ForeignFunctionInterface #-}
module System.Posix.Affinity (setAffinityOS) where
import Control.Monad
import Foreign
import Foreign.C
import System.IO.Error
foreign import ccall unsafe "pin_pthread" pin_pthread :: CInt -> IO Errno
setAffinityOS :: Int -> IO ()
setAffinityOS cpu = do
err <- pin_pthread $ fromIntegral cpu
when (err /= eOK) $
ioError $ errnoToIOError "setAffinityOS" err Nothing Nothing
#define _GNU_SOURCE
#include <pthread.h>
#include <sched.h>
#include <stdlib.h>
#include "pin.h"
int pin_pthread(int cpu) {
cpu_set_t cs;
pthread_t thread;
thread = pthread_self();
CPU_ZERO(&cs);
CPU_SET(cpu, &cs);
return pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment