Event driven construct in c

This is a little theoretical question. Imagine a device full of sensors. Now, if sensor x detects something, something must happen. Meanwhile, if something else is discovered, as two sensors detect two different things, then this device should behave differently.

From webdesign (so javascript) I learned about events, for example (using jquery) $(".x").on("click", function(){})or from angularjs $scope.watch("name_of_var", function()).

Is it possible to reproduce this behavior in C without using complex libraries?

Thank.

+5
source share
3 answers

I would suggest that you have an embedded system with access to interrupts or the main event loop in a separate thread, otherwise this is not possible.

:

#define NOEVENT 0

typedef void *(*EventHandler)(void *);

void *doNothing(void *p){/*do nothing absolutely*/ return NULL; }
typedef struct _event{
  EventHandler handler;
}Event, *PEvent;

Event AllEvents[1000];
unsigned short counter = 0;
void InitEvents()
{
    LOCK(AllEvents);
    for(int i = 0; i < 1000; i++){ 
        AllEvents[i].handler = doNothing;
    }
    UNLOCK(AllEvents);
}
void AddEvent(int EventType, EventHandler ev_handler)
{
    LOCK(AllEvents);
    AllEvents[EventType].handler = ev_handler;
    UNLOCK(AllEvents);
}

void RemoveEvent(int EventType, EventHandler ev_handler)
{
   LOCK(AllEvents);
   AllEvents[EventType] = doNothing;
   UNLOCK(AllEvents); /*to safeguard the event loop*/
}

/*to be run in separate thread*/
void EventLoop()
{
   int event = NOEVENT;
   EventHandler handler;
   while(1){
       while(event == NOEVENT)event=GetEvents();
       handler = AllEvents[event].handler;
       handler();/*perform on an event*/
  }
}

, .. , .

+1

, , -. -, (, , , , -). - , , : .
, , , < , .

. , , . , ( ), . , . , , , , , .
, , , , , .
:


/*
 * Some data structures to begin with
 */
struct event;
struct notifier;
struct subscription;
struct notify_sched;


typedef int (*notify_cbck)(struct event *evt, void *private);
/*
 *@type : a value to show the type of event
 *@t : the timestamp of the event
 *@value : a pointer towards the event data
 */
struct event {
    int type;
    struct timeval t; // the timestamp
    void *value;
};

/*
 * @type : the type in which the subscriber is interested
 * @cb : the callback that should be run when an event occur
 * @cb_data : the data to provide to the callback
 * @next,prev : doubly-linked list
 */
struct subscription {
    int type;
    notify_cbck cb;
    void *cb_data;
    struct subscription *next, *prev;
};

/*
 * This structure gathers the subscriptions of a given type.
 * @type : the event type
 * @subs : the subscription list
 * @mutex : a mutex to protect the list while inserting/removing subscriptions
 * @next,prev : link to other typed subscriptions
 */

struct typed_subscription {
    int type;
    struct subscription *subs;
    mutex_t mutex;
    struct typed_subscription *next, *prev;
};

/*
 * @magic : the ID of the event producer
 * @t_subs : the typed_subscription list
 * @mutex : a mutex to protect data when (un)registering new types to the producer
 * @next, prev : doubly-linked list ...
 */
struct notifier {
    int magic;
    struct typed_subscription *t_subs;
    mutex_t mutex;
    struct notifier *next, *prev;
};

/*
 * @ntf : the notifiers list
 * @mutex : a mutex to protect the ntf list
 * @th : something to identify the task that hosts the scheduler
 */
struct notify_sched {
    struct notifier *ntf;
    mutex_t mutex;
    pthread_t th; // I assume it a classic pthread in this example.
};

, , , . , , . , .

+3

, , (, ), , . , .

, API , , , , , . , , , , , , , .

, , . Linux/Unix/Posix select() poll(). , - ( ). , select()/poll() , - ( IP-) //, , , , , , , select()/poll().

, , , , , , - , . , , . , , . , , , . " " GUI : / , .

So yes, it’s quite simple to have an event-based system with C. Just have a loop with select () / poll () in it, define event types, create data structures for events and have a list of pointer functions that will be called with the new event structure as a parameter for each type of event.

0
source

All Articles