github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

tibbe / event

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 14
    • 4
  • Source
  • Commits
  • Network (4)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Tree: 05a7e50

click here to add a description

click here to add a homepage

  • Branches (1)
    • master
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

A Haskell event notification library — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Added design doc draft 
tibbe (author)
Wed Jul 15 09:47:30 -0700 2009
commit  05a7e509496675d2f2afea2680541ca5d504bc74
tree    8d5cfbd8faafa77b4f07a527eca6a9d26bc70cbf
parent  1f2d3411a6de988b99eaecfb388ba264a45b0905
event / docs / design.md docs/design.md
100644 92 lines (68 sloc) 3.294 kb
edit raw blame history

An Event Notification Library for Haskell

Overview

This library provides an efficient way to monitor a large number of file descriptors for readiness change notifications and timers for timeouts. The API executes a user supplied callback on each event.

Goals

The primary goal of this library is to provide an event notification system scalable enough to build high performance servers as outlined in The C10K Problem.

The secondary goal is to allow the library to replace the select based event notification system currently used in the GHC RTS.

High Level Design

The library is designed around a so called event loop. In each iteration of the loop the library checks

  • if any file descriptors have become ready, and
  • if any timeouts have occurred.

The first point is achieved using a system call like select or epoll. Different operating systems provide different system calls. We will refer to the different system calls as different back-ends. The second point is achieved using the a priority queue that stores all timeouts sorted by the remaining time.

Detailed Design

Storing The Callbacks

Once we know which file descriptors are ready, by polling the back-end, we need to find the callback associated with each file descriptors. To do so efficiently we store the callbacks in an array indexed by the file descriptor. Since operating systems typically allocate file descriptors in an increasing fashion the size of this array should not be much bigger than the number of watched file descriptors.

The callback associated with a given file descriptor might change over time and thus we need a mutable array to store the callbacks. Furthermore, since the number of watched file descriptors grows over time we need to grow the array. We need something similar to a C++ vector:

-- | A mutable, dynamic array type for use in the @ST@ monad.
newtype Vector s a = Vector (STRef s (C s a))

-- The actual array contents.
data C s a = C
    {-# UNPACK #-} !(MutableArray# s a)  -- Elements
    {-# UNPACK #-} !Int                  -- Capacity
    {-# UNPACK #-} !Int                  -- Current length

It might be possible to use a different data type as long as it supports efficient indexing and it's resizable.

Note: Once the library is used from the RTS we might need to synchronize access to it as several different threads might use it at the same time.

Back-end Interface

Each back-end must support polling for new events and registering interest in events on file descriptors. This suggests an interface of the form:

-- | The possible I/O events.
data Event = Read | Write

class Backend a where
    set :: a
        -> CInt      -- File descriptor
        -> [Event]   -- Events to watch for
        -> IO ()

    poll :: a
         -> (CInt -> [Event] -> IO ())  -- Callback (see below)
         -> IO ()

Note that the back-end does not store the client supplied callbacks directly. Instead we provide the back-end with a callback that in turn will look up the user supplied callback in the array of file descriptors described in the previous section.

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server