#include "../../ucx/map.h"
#include "../util/atomic.h"
#include "event.h"
UcxMap *event_handler_map =
NULL;
int numevhandlers =
0;
EVHandler *default_event_handler =
NULL;
EVHandler *last_handler_c =
NULL;
int create_event_handler(EventHandlerConfig *cfg) {
if(event_handler_map ==
NULL) {
event_handler_map = ucx_map_new(
16);
}
if(ucx_map_sstr_get(event_handler_map, cfg->name)) {
return 1;
}
EVHandler *e = evhandler_create(cfg);
if(e ==
NULL) {
return 1;
}
if(cfg->isdefault) {
if(default_event_handler) {
}
default_event_handler = e;
}
int ret = ucx_map_sstr_put(event_handler_map, cfg->name, e);
if(ret ==
0) {
last_handler_c = e;
numevhandlers++;
}
return ret;
}
int check_event_handler_cfg() {
if(numevhandlers >
0 ) {
if(default_event_handler) {
return 0;
}
else {
default_event_handler = last_handler_c;
return 0;
}
}
EventHandlerConfig cfg;
cfg.name = sstr(
"default");
cfg.nthreads =
1;
cfg.isdefault =
1;
return create_event_handler(&cfg);
}
EVHandler* get_default_event_handler() {
return default_event_handler;
}
EVHandler* get_event_handler(
char *name) {
return ucx_map_cstr_get(event_handler_map, name);
}
EventHandler* ev_instance(EVHandler *ev) {
int nev = ev->numins;
if(nev ==
1) {
return ev->instances[
0];
}
int ins = ev->current & nev;
ws_atomic_inc32(&ev->current);
return ev->instances[ins];
}