-
Notifications
You must be signed in to change notification settings - Fork 31
/
luapool.c
208 lines (187 loc) · 4.64 KB
/
luapool.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
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <pthread.h>
/* Include the Lua API header files. */
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
//#include <ncapi.h>
//#include <trace.h>
#include "common.h"
#include "luapool.h"
#include "scx_util.h"
pthread_mutex_t __lua_pool_lock;
pthread_cond_t __has_free_elements;
lua_pool_t *
lua_pool_create(size_t alloc_cnt)
{
lua_pool_t *pool = NULL;
int i;
TRC_BEGIN((__func__));
pool = SCX_CALLOC(1, sizeof(lua_pool_t));
if (!pool) {
TRC_END;
return NULL;
}
pool->max_cnt = 200;
if (alloc_cnt > pool->max_cnt) {
pool->max_cnt = alloc_cnt;
}
pool->alloc_cnt = alloc_cnt;
pool->current = pool; /* 동적 pool 확장을 위해 사용 */
pool->next = NULL;
pool->start_loc = 0;
pool->elements = SCX_CALLOC(1, pool->max_cnt * sizeof(void *)); /* pool의 크기를 최대 200개까지 확장 가능하도록 한다. */
if (!pool->elements) {
SCX_FREE(pool);
TRC_END;
return NULL;
}
pthread_mutex_init(&__lua_pool_lock, NULL);
pthread_cond_init(&__has_free_elements, NULL);
for(i = 0; i < pool->alloc_cnt; i++) {
lua_elment_t *element = NULL;
element = lua_pool_element_create(i);
if (!element) {
TRACE((T_ERROR, "lua pool element failed to create.\n"));
lua_pool_destroy(pool);
TRC_END;
return NULL;
}
pool->current->elements[i] = element;
}
TRACE((T_INFO, "lua pool create success. count = %d\n", pool->alloc_cnt));
TRC_END;
return pool;
}
int
lua_expend_pool(lua_pool_t *pool, size_t expend_cnt)
{
int i = 0;
int alloc_cnt = pool->alloc_cnt;
for(i = alloc_cnt; i < (alloc_cnt + expend_cnt); i++) {
lua_elment_t *element = NULL;
element = lua_pool_element_create(i);
if (!element) {
TRACE((T_ERROR, "lua pool element failed to create.\n"));
lua_pool_destroy(pool);
return 0;
}
pool->current->elements[i] = element;
}
pool->alloc_cnt += expend_cnt;
TRACE((T_INFO, "lua pool expened to %d.\n", pool->alloc_cnt));
return 1;
}
int
lua_pool_destroy(lua_pool_t *pool)
{
if(pool == NULL)
return 0;
int i;
for(i = 0; i < pool->alloc_cnt; i++) {
lua_pool_element_destroy(pool->current->elements[i]);
}
pthread_cond_destroy(&__has_free_elements);
pthread_mutex_destroy(&__lua_pool_lock);
SCX_FREE(pool->elements);
SCX_FREE(pool);
pool = NULL;
}
lua_elment_t *
lua_pool_element_create(int locate)
{
lua_elment_t *element = NULL;
int i;
element = SCX_CALLOC(1, sizeof(lua_elment_t));
if (!element) {
return NULL;
}
element->locate = locate;
element->state = LUA_STATE_IDLE;
lua_State *L = luaL_newstate();
if(L == NULL) {
SCX_FREE(element);
return NULL;
}
// load the libs
luaL_openlibs(L);
element->data = (void *) L;
return element;
}
int
lua_pool_element_destroy(lua_elment_t *element)
{
if(element == NULL)
return 0;
lua_close((lua_State * )element->data);
SCX_FREE(element);
element = NULL;
return 1;
}
lua_elment_t *
lua_pool_get_element(lua_pool_t *pool)
{
if(pool == NULL)
return NULL;
lua_elment_t *element = NULL;
int i;
TRC_BEGIN((__func__));
pthread_mutex_lock(&__lua_pool_lock);
retry_find_element :
for (i = pool->start_loc; i < pool->alloc_cnt; i++) {
if(((lua_elment_t *)pool->current->elements[i])->state == LUA_STATE_IDLE)
{
element = (lua_elment_t *)pool->current->elements[i];
break;
}
}
if (element == NULL){
for(i = 0; i < pool->start_loc; i++) {
if(((lua_elment_t *)pool->current->elements[i])->state == LUA_STATE_IDLE)
{
element = (lua_elment_t *)pool->current->elements[i];
break;
}
}
}
if (element == NULL) { /* 사용 가능한 pool이 없는 경우. */
if (pool->alloc_cnt < pool->alloc_cnt) {
/* pool에 10개씩 element를 추가로 생성한다. */
lua_expend_pool(pool, 10);
}
else {
/* pool의 최대 한도를 넘은 경우 비어 있는 pool이 생길때 까지 대기 한다 */
TRACE((T_WARN, "Failed to find available lua state pool.\n"));
pthread_cond_wait(&__has_free_elements, &__lua_pool_lock);
goto retry_find_element;
}
}
else {
element->state = LUA_STATE_USING;
if (element->locate == (pool->alloc_cnt - 1)) { /* 할당 받은 element가 마지막인 경우 */
pool->start_loc = 0;
}
else {
pool->start_loc = element->locate + 1;
}
// TRACE((T_INFO, "get element(%d).\n", element->locate));
}
pthread_mutex_unlock(&__lua_pool_lock);
TRC_END;
return element;
}
int
lua_pool_release_element(lua_elment_t *element)
{
TRC_BEGIN((__func__));
pthread_mutex_lock(&__lua_pool_lock);
element->state = LUA_STATE_IDLE;
pthread_cond_signal(&__has_free_elements);
pthread_mutex_unlock(&__lua_pool_lock);
// TRACE((T_INFO, "lease element(%d).\n", element->locate));
TRC_END;
return 1;
}