Giter Site home page Giter Site logo

eloop.c's Introduction

eloop.c

Simple single threaded event loop API

why ?

To learn and experiment

install

$ clib install jwerle/eloop.c

usage

#include <stdio.h>
#include <eloop/eloop.h>

static int
work (void *data) {
  printf("%s\n", (char *) data);
  return EL_WORK_COMPLETE;
}

int
main (int argc, char **argv) {
  el_t *loop = el_alloc();
  el_enqueue(loop, "hello", work);
  return el_run(loop);
}

api

Worker status codes:

EL_WORK_ERROR = -1
EL_WORK_PENDING = 0
EL_WORK_COMPLETE = 1

Event loop API:

/**
 * Worker function with data passed to it
 */

typedef el_work_status_t (*el_work_handle_t) (void *);

/**
 * Work structure with function handle and data
 */

typedef struct el_work {
  el_work_handle_t handle;
  void *data;
} el_work_t;

/**
 * Event loop arbiter
 */

typedef struct eloop {
  el_work_t *work[EL_MAX_WORK];
  size_t length;
} el_t;

/**
 * Allocates a new `el_t' type
 */

el_t *
el_alloc (void);

/**
 * Frees an event loop
 */

void
el_free (el_t *);

/**
 * Initializes work with argument and function handle
 */

el_work_t *
el_work_init (void *, el_work_handle_t);

/**
 * Destroys work. Does not destroy argument pointer
 */

void
el_work_destroy (el_work_t *);

/**
 * Enqueues work with argument and handle
 */

size_t
el_enqueue (el_t *, void *, el_work_handle_t);

/**
 * Dequeues all queued work
 */

size_t
el_dequeue (el_t *);

/**
 * Runs the event loop
 */

int
el_run (el_t *);

license

MIT

eloop.c's People

Contributors

jwerle avatar

Stargazers

FanYuFei avatar Jon Mayo avatar Ioan Rîpan avatar UncleNine avatar Oleksandr Dunaievskyi avatar Daniel Durante avatar  avatar

Watchers

James Cloos avatar  avatar

Forkers

unclenine

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.