Skip to content

Instantly share code, notes, and snippets.

Created August 29, 2014 23:23
Show Gist options
  • Save anonymous/5f97d8db71776b188820 to your computer and use it in GitHub Desktop.
Save anonymous/5f97d8db71776b188820 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Aug 29, 2014.
    51 changes: 51 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    typedef struct chan_t
    {
    pthread_mutex_t m_mu;
    pthread_cond_t m_cond;
    int closed;

    void (* dispose)(struct chan_t * self);
    ...
    } chan_t;

    typedef struct unbuffered_t
    {
    chan_t base;
    ...
    } unbuffered_t;

    typedef struct buffered_t
    {
    chan_t base;
    ...
    } buffered_t;

    chan_t* chan_init(int capacity)
    {
    if (capacity)
    {
    buffered_t * ch = malloc(sizeof *ch);
    ch->dispose = dispose_buffered;
    ...
    return &ch->base;
    }
    else
    {
    Unbuffered_t * ch = malloc(sizeof *ch);
    ch->dispose = dispose_unbuffered;
    ...
    return &ch->base;
    }
    }

    void dispose_unbuffered(chan_t * self)
    {
    unbuffered_t * ch = (chan_t*)self;
    ...
    }

    void dispose_buffered(chan_t * self)
    {
    buffered_t * ch = (chan_t*)self;
    ...
    }