1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include "../../ucx/map.h"
30 #include "../util/atomic.h"
31
32 #include "event.h"
33
34 UcxMap *event_handler_map =
NULL;
35 int numevhandlers =
0;
36
37 EVHandler *default_event_handler =
NULL;
38
39 EVHandler *last_handler_c =
NULL;
40
41 int create_event_handler(EventHandlerConfig *cfg) {
42 if(event_handler_map ==
NULL) {
43 event_handler_map = ucx_map_new(
16);
44 }
45
46
47 if(ucx_map_sstr_get(event_handler_map, cfg->name)) {
48
49
50 return 1;
51 }
52
53
54 EVHandler *e = evhandler_create(cfg);
55 if(e ==
NULL) {
56 return 1;
57 }
58
59 if(cfg->isdefault) {
60 if(default_event_handler) {
61
62
63 }
64 default_event_handler = e;
65 }
66
67 int ret = ucx_map_sstr_put(event_handler_map, cfg->name, e);
68 if(ret ==
0) {
69 last_handler_c = e;
70 numevhandlers++;
71 }
72
73 return ret;
74 }
75
76
77
78
79
80 int check_event_handler_cfg() {
81 if(numevhandlers >
0 ) {
82 if(default_event_handler) {
83 return 0;
84 }
else {
85 default_event_handler = last_handler_c;
86 return 0;
87 }
88 }
89
90 EventHandlerConfig cfg;
91 cfg.name = sstr(
"default");
92 cfg.nthreads =
1;
93 cfg.isdefault =
1;
94
95 return create_event_handler(&cfg);
96 }
97
98
99 EVHandler* get_default_event_handler() {
100 return default_event_handler;
101 }
102
103 EVHandler* get_event_handler(
char *name) {
104 return ucx_map_cstr_get(event_handler_map, name);
105 }
106
107 EventHandler* ev_instance(EVHandler *ev) {
108 int nev = ev->numins;
109 if(nev ==
1) {
110 return ev->instances[
0];
111 }
112
113 int ins = ev->current & nev;
114 ws_atomic_inc32(&ev->current);
115 return ev->instances[ins];
116 }
117