1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
// https://github.com/ImagicTheCat/ljuv
// MIT license (see LICENSE or src/ljuv.lua)
#include <stdlib.h>
#include <lua.h>
#include <lauxlib.h>
#include <uv.h>
typedef struct ljuv_wrapper{
int non_empty;
} ljuv_wrapper;
static ljuv_wrapper wrapper;
int luaopen_ljuv_wrapper_c(lua_State *L)
{
lua_pushlightuserdata(L, &wrapper);
return 1;
}
|
>
>
|
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
|
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
|
// https://github.com/ImagicTheCat/ljuv
// MIT license (see LICENSE or src/ljuv.lua)
#include <stdlib.h>
#include <lua.h>
#include <lauxlib.h>
#include <uv.h>
// Object reference counting.
typedef struct ljuv_object{
uint32_t count;
void (*destructor)(struct ljuv_object *obj);
uv_mutex_t mutex;
} ljuv_object;
// return < 0 on failure
int object_init(ljuv_object *obj, void (*destructor)(ljuv_object *obj))
{
obj->count = 1;
obj->destructor = destructor;
return uv_mutex_init(&obj->mutex);
}
void object_retain(ljuv_object *obj)
{
uv_mutex_lock(&obj->mutex);
obj->count++;
uv_mutex_unlock(&obj->mutex);
}
void object_release(ljuv_object *obj)
{
uv_mutex_lock(&obj->mutex);
uint32_t count = --obj->count;
uv_mutex_unlock(&obj->mutex);
if(count == 0){
if(obj->destructor) obj->destructor(obj);
free(obj);
}
}
// Channel
// Expose wrapper functions.
typedef struct ljuv_wrapper{
int (*object_init)(ljuv_object *obj, void (*destructor)(ljuv_object *obj));
void (*object_retain)(ljuv_object *obj);
void (*object_release)(ljuv_object *obj);
} ljuv_wrapper;
static ljuv_wrapper wrapper = {
object_init,
object_retain,
object_release
};
int luaopen_ljuv_wrapper_c(lua_State *L)
{
lua_pushlightuserdata(L, &wrapper);
return 1;
}
|