Code Monkey home page Code Monkey logo

pev's Introduction

Portable Event Library (PEV)

License Badge GitHub Status

This is a small event library in C based around select() available free for use in the public domain.

Upgrade Warnings

  • v1.3 changes timer resolution from 1 second to 1 microsecond
  • v1.5 changes timer API to add support for one-shot timers

See ChangeLog for details.

The Code

The event loop consists of two files: pev.c and pev.h. You can use it as a library or just include the components into your own project.

See the header file for the API description. If you find bugs or have fixes to share, please report them using GitHub issues or, preferably, pull requests.

Example

#include <stdio.h>
#include <signal.h>
#include "pev.h"

#define TIMEOUT 500000      /* 0.5 sec */

int id;

static void cb(int timeout, void *arg)
{
        printf("Hej %d\n", timeout);
        pev_timer_set(id, timeout + TIMEOUT);
}

static void br(int signo, void *arg)
{
        pev_exit(10);
}

int main(void)
{
        pev_init();
        pev_sig_add(SIGINT, br, NULL);
        id = pev_timer_add(TIMEOUT, 0, cb, NULL);

        return pev_run();
}

Gives the following output:

$ gcc -I. -o example example.c pev.c
$ ./example
Hej 500000
Hej 1000000
Hej 1500000
Hej 2000000
Hej 2500000
^C

Implementation Notes

Note: The timer implementation uses setitimer() for maximum portability. Depending on the system, this can potentially cause problems if your application also use sleep(), usleep(), or alarm(), which may all use the same back end implantation in your operating system. Please check the documentation for your OS for more information on the subject.

Building and Testing

For those that don't just take the sources and integrate into their own project. You may have to set CC, to install you must set prefix. Example:

make clean
make CC=clang
make install prefix=/usr/local
...
make uninstall prefix=/usr/local

History and Background

The design evolved over time in the SMCRoute project, where I wanted to keep the tool small and with as few (no) external dependencies as possible.

It started out as a refactor of the socket polling functionality, and really came into its own in 2017 when I found Mr Rich Felker's mention of the classic timer-to-pipe pattern on Stack Overflow.

In the years passed I've noticed many times the need for a really small and, most importantly, portable event loop. So here's the code I wrote for SMCRoute, cleaned up and now fully free in the public domain.

Take care!
/Joachim :-)

pev's People

Contributors

jackuess avatar troglobit avatar wkz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

wkz hjluo

pev's Issues

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.