-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathservice_listener.c
266 lines (242 loc) · 7.79 KB
/
service_listener.c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#define LOKI_MODULE
#include "loki_services.h"
#include <assert.h>
typedef struct lk_ListenerState lk_ListenerState;
typedef struct lk_Listener lk_Listener;
typedef struct lk_SlotEntry lk_SlotEntry;
struct lk_ListenerState {
lk_State *S;
lk_Lock lock;
lk_Table svrmap;
lk_Table slotmap;
lk_MemPool listeners;
};
struct lk_Listener {
lk_Source source;
lk_SlotEntry *entry;
lk_Listener *link;
lk_Listener *next;
lk_Listener *prev;
};
struct lk_SlotEntry {
lk_Entry base;
lk_ListenerState *ls;
lk_Slot *target;
lk_Listener *listeners;
};
#define lkX_svrstate(svr) ((lk_ListenerState*)lk_data((lk_Slot*)(svr)))
#define lkX_state(S) ((lk_ListenerState*)lk_userdata(S))
static void lkX_link (lk_Listener **pp, lk_Listener *node) {
if (*pp == NULL)
*pp = node->prev = node->next = node;
else {
lk_Listener *h = *pp;
node->prev = h->prev;
node->next = h;
h->prev->next = node;
h->prev = node;
}
}
static void lkX_unlink (lk_Listener **pp, lk_Listener *node) {
if (*pp == node) *pp = node == node->next ? NULL : node->next;
node->prev->next = node->next;
node->next->prev = node->prev;
}
static unsigned lkX_ptrhash (unsigned p) {
p = (p+0x7ed55d16) + (p<<12);
p = (p^0xc761c23c) ^ (p>>19);
p = (p+0x165667b1) + (p<<5);
p = (p+0xd3a2646c) ^ (p<<9);
p = (p+0xfd7046c5) + (p<<3);
p = (p^0xb55a4f09) ^ (p>>16);
return p;
}
static lk_Entry *lkX_gettable (lk_Table *t, void *key) {
unsigned hash;
lk_Entry *e;
if (t->size == 0 || key == NULL) return NULL;
hash = lkX_ptrhash((unsigned)(ptrdiff_t)key);
e = (lk_Entry*)((char*)t->hash + ((hash & (t->size - 1))*t->entry_size));
for (;;) {
if (e->key && (e->hash == hash && strcmp(e->key, key) == 0))
return e;
if (e->next == 0) return NULL;
e = (lk_Entry*)((char*)e + e->next);
}
return e;
}
static lk_Entry *lkX_settable (lk_State *S, lk_Table *t, void *key) {
lk_Entry e, *ret;
if (key == NULL) return NULL;
if ((ret = lkX_gettable(t, key)) != NULL)
return ret;
e.key = key;
e.hash = lkX_ptrhash((unsigned)(ptrdiff_t)key);
return lk_newkey(S, t, &e);
}
static int lkX_next (lk_Listener *h, lk_Listener **pnode, lk_Listener **pnext) {
if (h == NULL) return 0;
if (*pnode != NULL) {
lk_Listener *next = pnext ? *pnext : (*pnode)->next;
if (next != h) {
if (pnext) *pnext = next;
*pnode = next;
return 1;
}
*pnode = NULL;
return 0;
}
*pnode = h;
if (pnext) *pnext = h->next;
return 1;
}
static int lkX_srcdeletor (lk_State *S, lk_Slot *sender, lk_Signal *sig) {
lk_Listener *node = (lk_Listener*)sig->source;
lk_SlotEntry *list = node->entry;
lk_ListenerState *ls = list->ls;
(void)S, (void)sender;
lk_lock(ls->lock);
lkX_unlink(&list->listeners, node);
lk_poolfree(&list->ls->listeners, node);
if (list->listeners == NULL) {
lk_Entry *e = lkX_gettable(&ls->slotmap, list->target);
e->key = NULL;
lk_sethook(list->target, NULL, NULL);
}
lk_unlock(ls->lock);
return LK_OK;
}
static int lkX_broadcast (lk_State *S, lk_Slot *sender, lk_Signal *sig) {
lk_Source *src = sig->source;
lk_SlotEntry *list = (lk_SlotEntry*)lk_userdata(S);
lk_ListenerState *ls = list->ls;
lk_Listener *node = NULL;
(void)sender;
lk_lock(ls->lock);
while (lkX_next(list->listeners, &node, NULL)) {
sig->source = &node->source;
lk_emit((lk_Slot*)node->source.service, sig);
}
lk_unlock(ls->lock);
sig->source = src;
return LK_OK;
}
static int lkX_addlistener (lk_ListenerState *ls, lk_Slot *slot, lk_Handler *h, void *ud) {
lk_Listener *node;
lk_PtrEntry *e = (lk_PtrEntry*)
lkX_gettable(&ls->svrmap, lk_service(slot));
lk_SlotEntry *list;
if (e == NULL) return LK_ERR;
list = (lk_SlotEntry*)lkX_settable(ls->S, &ls->slotmap, slot);
if (list->ls == NULL) {
list->ls = ls;
list->target = slot;
lk_sethook(slot, lkX_broadcast, list);
}
node = (lk_Listener*)lk_poolalloc(ls->S, &ls->listeners);
lk_initsource(ls->S, &node->source, h, ud);
node->source.deletor = lkX_srcdeletor;
node->source.refcount = 1;
node->source.force = 1;
node->entry = list;
node->link = (lk_Listener*)e->data;
e->data = node;
lkX_link(&list->listeners, node);
return LK_OK;
}
static lk_Listener *lkX_dellistener (lk_ListenerState *ls, lk_Slot *slot, lk_Handler *h, void *ud) {
lk_Listener **pp, *node = NULL;
lk_PtrEntry *e = (lk_PtrEntry*)lkX_gettable(&ls->slotmap, slot);
for (pp = (lk_Listener**)&e->data; *pp != NULL; pp = &(*pp)->link)
if ((*pp)->source.callback == h && (*pp)->source.ud == ud)
break;
if (*pp != NULL) {
node = *pp;
*pp = (*pp)->link;
}
return node;
}
static int lkX_launch (lk_State *S, lk_Slot *sender, lk_Signal *sig) {
lk_ListenerState *ls = (lk_ListenerState*)lk_userdata(S);
lk_Service *svr = (lk_Service*)sig->data;
lk_PtrEntry *e;
(void)sender;
lk_lock(ls->lock);
e = (lk_PtrEntry*)lkX_settable(S, &ls->svrmap, svr);
if (lk_key(e) == NULL) {
lk_key(e) = (const char*)svr;
e->data = NULL;
}
lk_unlock(ls->lock);
return LK_OK;
}
static int lkX_close (lk_State *S, lk_Slot *sender, lk_Signal *sig) {
lk_ListenerState *ls = lkX_state(S);
lk_PtrEntry *e;
lk_Listener *node = NULL;
(void)sender;
lk_lock(ls->lock);
if ((e = (lk_PtrEntry*)lkX_gettable(&ls->svrmap, sig->data)) != NULL) {
lk_key(e) = NULL;
node = (lk_Listener*)e->data;
e->data = NULL;
}
lk_unlock(ls->lock);
while (node) {
lk_Listener *next = node->link;
lk_freesource(&node->source);
node = next;
}
return LK_OK;
}
LK_API int lk_addlistener (lk_Service *svr, lk_Slot *slot, lk_Handler *h, void *ud) {
lk_ListenerState *ls = lkX_svrstate(svr);
int ret = LK_ERR;
if (h != NULL) {
lk_lock(ls->lock);
ret = lkX_addlistener(ls, slot, h, ud);
lk_unlock(ls->lock);
}
return ret;
}
LK_API int lk_dellistener (lk_Service *svr, lk_Slot *slot, lk_Handler *h, void *ud) {
lk_ListenerState *ls = lkX_svrstate(svr);
lk_Listener *node;
lk_lock(ls->lock);
node = lkX_dellistener(ls, slot, h, ud);
lk_unlock(ls->lock);
if (node != NULL) {
lk_freesource(&node->source);
return LK_OK;
}
return LK_ERR;
}
LKMOD_API int loki_service_listener (lk_State *S, lk_Slot *sender, lk_Signal *sig) {
if (sender == NULL) { /* init */
lk_ListenerState *ls = (lk_ListenerState*)
lk_malloc(S, sizeof(lk_ListenerState));
memset(ls, 0, sizeof(*ls));
ls->S = S;
if (!lk_initlock(&ls->lock)) return LK_ERR;
lk_initpool(&ls->listeners, sizeof(lk_Listener));
lk_inittable(&ls->svrmap, sizeof(lk_PtrEntry));
lk_inittable(&ls->slotmap, sizeof(lk_SlotEntry));
lk_newslot(S, LK_SLOTNAME_LAUNCH, lkX_launch, ls);
lk_newslot(S, LK_SLOTNAME_CLOSE, lkX_close, ls);
lkX_settable(S, &ls->svrmap, S);
lk_setdata(lk_current(S), ls);
return LK_WEAK;
}
else if (sig == NULL) { /* free */
lk_ListenerState *ls = lkX_state(S);
lk_freetable(S, &ls->svrmap);
lk_freetable(S, &ls->slotmap);
lk_freepool(S, &ls->listeners);
lk_freelock(ls->lock);
lk_free(S, ls, sizeof(*ls));
}
return LK_OK;
}
/* win32cc: flags+='-s -mdll -xc' output='loki.dll' libs+='-lws2_32'
* unixcc: flags+='-fPIC -shared -xc' output='loki.so'
* cc: flags+='-Wextra -O3' input+='lokilib.c' */