forked from couchbase/couchbase-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiops.c
More file actions
599 lines (505 loc) · 14.2 KB
/
Copy pathiops.c
File metadata and controls
599 lines (505 loc) · 14.2 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/**
* Copyright 2013 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
#include "pycbc.h"
#include "iops.h"
#include "structmember.h"
static PyTypeObject pycbc_EventType = {
PYCBC_POBJ_HEAD_INIT(NULL)
0
};
static PyTypeObject pycbc_IOEventType = {
PYCBC_POBJ_HEAD_INIT(NULL)
0
};
static PyTypeObject pycbc_TimerEventType = {
PYCBC_POBJ_HEAD_INIT(NULL)
0
};
static struct PyMemberDef pycbc_Event_TABLE_members[] = {
{ "__dict__", T_OBJECT_EX, offsetof(pycbc_Event, vdict), 0 },
{ "evtype", T_INT, offsetof(pycbc_Event, type), READONLY },
{ "state", T_INT, offsetof(pycbc_Event, state), READONLY },
{ NULL }
};
static struct PyMemberDef pycbc_IOEvent_TABLE_members[] = {
{ "fd", T_LONGLONG, offsetof(pycbc_IOEvent, fd), READONLY },
{ "flags", T_SHORT, offsetof(pycbc_IOEvent, flags), READONLY },
{ NULL }
};
static struct PyMemberDef pycbc_TimerEvent_TABLE_members[] = {
{ NULL }
};
static void event_fire_common(pycbc_Event *ev, short which)
{
lcb_socket_t fd = 0;
if (ev->state == PYCBC_EVSTATE_FREED) {
return;
}
if (ev->type == PYCBC_EVTYPE_IO) {
fd = (lcb_socket_t)((pycbc_IOEvent*)ev)->fd;
}
ev->cb.handler(fd, which, ev->cb.data);
}
#define READY_RETURN() \
if (PyErr_Occurred()) { return NULL; } Py_RETURN_NONE
/**
* e.g.:
* event.event_received(PYCBC_LCB_READ_EVENT)
*/
static PyObject *
Event_on_ready(pycbc_Event *ev, PyObject *args)
{
short flags;
int rv;
rv = PyArg_ParseTuple(args, "h", &flags);
if (!rv) {
return NULL;
}
event_fire_common(ev, flags);
READY_RETURN();
}
static PyObject *
Event_on_read(pycbc_Event *ev)
{
event_fire_common(ev, LCB_READ_EVENT);
READY_RETURN();
}
static PyObject *
Event_on_write(pycbc_Event *ev)
{
event_fire_common(ev, LCB_WRITE_EVENT);
READY_RETURN();
}
static PyObject *
Event_on_readwrite(pycbc_Event *ev)
{
event_fire_common(ev, LCB_RW_EVENT);
READY_RETURN();
}
static PyObject *
IOEvent_fileno(pycbc_IOEvent *self, PyObject *args)
{
(void)args;
return pycbc_IntFromL((lcb_socket_t)self->fd);
}
static PyObject *
IOEvent__repr__(pycbc_IOEvent *self)
{
return PyUnicode_FromFormat("%s<fd=%lu,flags=0x%x @%p>",
Py_TYPE(self)->tp_name,
(unsigned long)self->fd,
(int)self->flags,
self);
}
static struct PyMethodDef pycbc_Event_TABLE_methods[] = {
{ "ready",
(PyCFunction)Event_on_ready,
METH_VARARGS,
PyDoc_STR("Called when an event is ready")
},
{ "ready_r",
(PyCFunction)Event_on_read,
METH_NOARGS,
PyDoc_STR("Called for read events. This is the efficient \n"
"form of ``ready(LCB_READ_EVENT)``\n")
},
{ "ready_w",
(PyCFunction)Event_on_write,
METH_NOARGS,
PyDoc_STR("Called for write events. This is equivalent to\n"
"``ready(LCB_WRITE_EVENT)``\n")
},
{ "ready_rw",
(PyCFunction)Event_on_readwrite,
METH_NOARGS,
PyDoc_STR("Called for rw events. This is equivalent to\n"
"``ready(LCB_READ_EVENT|LCB_WRITE_EVENT)``\n")
},
{ NULL }
};
static struct PyMethodDef pycbc_IOEvent_TABLE_methods[] = {
{ "fileno", (PyCFunction)IOEvent_fileno, METH_VARARGS },
{ NULL }
};
static int
Event__init__(pycbc_Event *self, PyObject *args, PyObject *kwargs)
{
if (PyBaseObject_Type.tp_init((PyObject *)self, args, kwargs) != 0) {
return -1;
}
if (!self->vdict) {
self->vdict = PyDict_New();
}
return 0;
}
static void
Event_dealloc(pycbc_Event *self)
{
Py_XDECREF(self->vdict);
Py_TYPE(self)->tp_free((PyObject*)self);
}
int
pycbc_EventType_init(PyObject **ptr)
{
PyTypeObject *p = &pycbc_EventType;
*ptr = (PyObject*)p;
if (p->tp_name) {
return 0;
}
p->tp_name = "Event";
p->tp_doc = PyDoc_STR("Internal event handle");
p->tp_new = PyType_GenericNew;
p->tp_basicsize = sizeof(pycbc_Event);
p->tp_members = pycbc_Event_TABLE_members;
p->tp_methods = pycbc_Event_TABLE_methods;
p->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
p->tp_init = (initproc)Event__init__;
p->tp_dealloc = (destructor)Event_dealloc;
p->tp_dictoffset = offsetof(pycbc_Event, vdict);
return PyType_Ready(p);
}
int
pycbc_IOEventType_init(PyObject **ptr)
{
PyTypeObject *p = &pycbc_IOEventType;
*ptr = (PyObject*)p;
if (p->tp_name) {
return 0;
}
p->tp_name = "IOEvent";
p->tp_new = PyType_GenericNew;
p->tp_basicsize = sizeof(pycbc_IOEvent);
p->tp_members = pycbc_IOEvent_TABLE_members;
p->tp_methods = pycbc_IOEvent_TABLE_methods;
p->tp_repr = (reprfunc)IOEvent__repr__;
p->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
p->tp_dealloc = (destructor)Event_dealloc;
p->tp_base = &pycbc_EventType;
return PyType_Ready(p);
}
int
pycbc_TimerEventType_init(PyObject **ptr)
{
PyTypeObject *p = &pycbc_TimerEventType;
*ptr = (PyObject*)p;
if (p->tp_name) {
return 0;
}
p->tp_name = "TimerEvent";
p->tp_new = PyType_GenericNew;
p->tp_basicsize = sizeof(pycbc_TimerEvent);
p->tp_members = pycbc_TimerEvent_TABLE_members;
p->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
p->tp_dealloc = (destructor)Event_dealloc;
p->tp_base = &pycbc_EventType;
return PyType_Ready(p);
}
static int
modify_event_python(pycbc_iops_t *pio,
pycbc_Event *ev,
pycbc_evaction_t action,
lcb_socket_t newsock,
void *arg)
{
int ret;
PyObject *result;
PyObject *o_arg;
PyObject *meth = NULL;
PyObject *argtuple;
short flags = 0;
unsigned long usecs = 0;
argtuple = PyTuple_New(3);
Py_INCREF((PyObject *)ev);
PyTuple_SET_ITEM(argtuple, 0, (PyObject *)ev);
PyTuple_SET_ITEM(argtuple, 1, pycbc_IntFromL(action));
if (ev->type == PYCBC_EVTYPE_IO) {
flags = *(short*)arg;
o_arg = pycbc_IntFromL(flags);
((pycbc_IOEvent *)ev)->fd = newsock;
meth = pio->meths.modevent;
} else {
usecs = *(lcb_uint32_t*)arg;
o_arg = pycbc_IntFromL(usecs);
meth = pio->meths.modtimer;
}
PyTuple_SET_ITEM(argtuple, 2, o_arg);
result = PyObject_CallObject(meth, argtuple);
Py_DECREF(argtuple);
Py_XDECREF(result);
if (ev->type == PYCBC_EVTYPE_IO) {
pycbc_IOEvent *evio = (pycbc_IOEvent*)ev;
evio->flags = flags;
}
if (action == PYCBC_EVACTION_WATCH) {
ev->state = PYCBC_EVSTATE_ACTIVE;
} else {
ev->state = PYCBC_EVSTATE_SUSPENDED;
}
if (!result) {
ret = -1;
PYCBC_EXC_WRAP(PYCBC_EXC_INTERNAL, 0, "Couldn't invoke IO Function");
} else {
ret = 0;
}
return ret;
}
/**
* Begin GLUE
*/
static void *
create_event_python(lcb_io_opt_t io, pycbc_evtype_t evtype)
{
PyObject *meth, *ret;
PyTypeObject *defltype;
pycbc_iops_t *pio = (pycbc_iops_t *)io;
if (evtype == PYCBC_EVTYPE_IO) {
defltype = &pycbc_IOEventType;
meth = pio->meths.mkevent;
} else {
defltype = &pycbc_TimerEventType;
meth = pio->meths.mktimer;
}
if (meth) {
ret = PyObject_CallObject(meth, NULL);
if (!ret) {
PyErr_PrintEx(0);
abort();
}
} else {
PyErr_Clear();
ret = PYCBC_TYPE_CTOR(defltype);
}
((pycbc_Event *)ret)->type = evtype;
return ret;
}
static void *
create_event(lcb_io_opt_t io)
{
(void)io;
return create_event_python(io, PYCBC_EVTYPE_IO);
}
static void *
create_timer(lcb_io_opt_t io)
{
(void)io;
return create_event_python(io, PYCBC_EVTYPE_TIMER);
}
static void
destroy_event_common(lcb_io_opt_t io, void *arg)
{
pycbc_Event *ev = arg;
lcb_uint32_t dummy = 0;
(void)io;
pycbc_assert(ev->state != PYCBC_EVSTATE_ACTIVE);
modify_event_python((pycbc_iops_t *)io,
ev,
PYCBC_EVACTION_CLEANUP,
0,
&dummy);
ev->state = PYCBC_EVSTATE_FREED;
Py_DECREF(arg);
}
static int
update_event(lcb_io_opt_t io,
lcb_socket_t sock,
void *event,
short flags,
void *data,
pycbc_lcb_cb_t handler)
{
pycbc_IOEvent *ev = (pycbc_IOEvent*)event;
pycbc_evaction_t action;
pycbc_evstate_t new_state;
if (!flags) {
action = PYCBC_EVACTION_UNWATCH;
new_state = PYCBC_EVSTATE_SUSPENDED;
} else {
action = PYCBC_EVACTION_WATCH;
new_state = PYCBC_EVSTATE_ACTIVE;
}
ev->cb.handler = handler;
ev->cb.data = data;
if (ev->flags == flags && new_state == ev->state && ev->fd == sock) {
return 0;
}
return modify_event_python((pycbc_iops_t*)io,
(pycbc_Event*)ev,
action,
sock,
&flags);
}
static void
delete_event(lcb_io_opt_t io, lcb_socket_t sock, void *event)
{
pycbc_IOEvent *ev = (pycbc_IOEvent*)event;
modify_event_python((pycbc_iops_t*) io,
(pycbc_Event*) ev,
PYCBC_EVACTION_UNWATCH,
sock,
&ev->flags);
}
static void
delete_timer(lcb_io_opt_t io, void *timer)
{
lcb_uint32_t dummy = 0;
modify_event_python((pycbc_iops_t*)io,
(pycbc_Event*)timer,
PYCBC_EVACTION_UNWATCH,
-1,
&dummy);
}
static int
update_timer(lcb_io_opt_t io,
void *timer,
lcb_uint32_t usec,
void *data,
pycbc_lcb_cb_t handler)
{
pycbc_TimerEvent *ev = (pycbc_TimerEvent*)timer;
ev->cb.data = data;
ev->cb.handler = handler;
return modify_event_python((pycbc_iops_t*)io,
(pycbc_Event*)ev,
PYCBC_EVACTION_WATCH,
-1,
&usec);
}
static void
run_event_loop(lcb_io_opt_t io)
{
pycbc_iops_t *pio = (pycbc_iops_t*)io;
pio->in_loop = 1;
PyObject_CallFunctionObjArgs(pio->meths.startwatch, NULL);
}
static void
stop_event_loop(lcb_io_opt_t io)
{
pycbc_iops_t *pio = (pycbc_iops_t*)io;
pio->in_loop = 0;
PyObject_CallFunctionObjArgs(pio->meths.stopwatch, NULL);
}
#define XIONAME_CACHENTRIES(X) \
X(modevent, 0) \
X(modtimer, 0) \
X(startwatch, 0) \
X(stopwatch, 0) \
X(mkevent, 1) \
X(mktimer, 1)
static void
iops_destructor(lcb_io_opt_t io)
{
pycbc_iops_t *pio = (pycbc_iops_t *)io;
#define X(b, unused) Py_XDECREF(pio->meths.b);
XIONAME_CACHENTRIES(X)
#undef X
}
static int
load_cached_method(PyObject *obj,
PyObject *attr, PyObject **target, int optional)
{
*target = PyObject_GetAttr(obj, attr);
if (*target) {
if (!PyCallable_Check(*target)) {
PYCBC_EXC_WRAP_OBJ(PYCBC_EXC_ARGUMENTS, 0,
"Invalid IOPS object", obj);
return -1;
}
return 0;
}
if (optional) {
PyErr_Clear();
return 0;
}
return -1;
}
static int
cache_io_methods(pycbc_iops_t *pio, PyObject *obj)
{
#define X(b, is_optional) \
if (load_cached_method(obj, \
pycbc_helpers.ioname_##b, \
&pio->meths.b,\
is_optional) == -1) { \
return -1; \
}
XIONAME_CACHENTRIES(X)
return 0;
#undef X
#undef XIONAME_CACHENTRIES
}
lcb_io_opt_t
pycbc_iops_new(pycbc_Bucket *unused, PyObject *pyio)
{
lcb_io_opt_t ret = NULL;
lcb_io_opt_t dfl = NULL;
lcb_error_t err;
struct lcb_create_io_ops_st options = { 0 };
pycbc_iops_t *pio;
(void)unused;
pio = calloc(1, sizeof(*pio));
ret = &pio->iops;
pio->pyio = pyio;
Py_INCREF(pyio);
/**
* We create the select 'iops' handle and copy over its functionality
* from there. Now that libcouchbase has the 'select' iops build in, we use
* that instead.
*
* We discard the default iops loop data at the expense of leaking a
* dlhandle.
*/
options.v.v0.type = LCB_IO_OPS_SELECT;
err = lcb_create_io_ops(&dfl, &options);
if (err != LCB_SUCCESS) {
PYCBC_EXC_WRAP(PYCBC_EXC_LCBERR, err, "Couldn't create IOPS");
return NULL;
}
memcpy(&pio->iops, dfl, sizeof(*dfl));
/* hide the dlsym */
dfl->dlhandle = NULL;
lcb_destroy_io_ops(dfl);
dfl = NULL;
if (-1 == cache_io_methods(pio, pyio)) {
return NULL;
}
ret->v.v0.create_event = create_event;
ret->v.v0.create_timer = create_timer;
ret->v.v0.destroy_event = destroy_event_common;
ret->v.v0.destroy_timer = destroy_event_common;
ret->v.v0.update_event = update_event;
ret->v.v0.delete_event = delete_event;
ret->v.v0.delete_timer = delete_timer;
ret->v.v0.update_timer = update_timer;
ret->v.v0.run_event_loop = run_event_loop;
ret->v.v0.stop_event_loop = stop_event_loop;
ret->destructor = iops_destructor;
return ret;
}
void
pycbc_iops_free(lcb_io_opt_t io)
{
pycbc_iops_t *pio = (pycbc_iops_t*)io;
Py_XDECREF(pio->pyio);
free(pio);
}
PyObject *
pycbc_event_new(void)
{
pycbc_TimerEvent *ev;
ev =(pycbc_TimerEvent *)PYCBC_TYPE_CTOR(&pycbc_TimerEventType);
ev->type = PYCBC_EVTYPE_TIMER;
return (PyObject *)ev;
}