Repository navigation
Expand file tree
/
Copy pathclp.c
More file actions
217 lines (192 loc) · 4.57 KB
/
Copy pathclp.c
File metadata and controls
217 lines (192 loc) · 4.57 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
///
// The main CLP module.
//
// CLP is a Lua library for bulding lightweight parallel processes
// based on the concepts of Communicating Sequential Processes.
//
// @module clp
// @author Tiago Salmito
// @license MIT
// @copyright Tiago Salmito - 2014
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "clp.h"
#include "marshal.h"
#include "process.h"
#include "pool.h"
#include "threading.h"
#include "lualib.h"
#include "lauxlib.h"
pool_t clp_defaultpool=NULL;
///
//Get the CLP library version string.
//
//@function version
//
//@treturn string the current version
static int clp_version(lua_State * L) {
lua_pushliteral(L,CLP_VERSION);
return 1;
}
///
// Get the current time sice epoch with a non-specified precision.
// @function now
// @treturn number the current time since epoch
static int clp_gettime(lua_State * L) {
lua_pushnumber(L,now_secs());
return 1;
}
///
// Get the current process
//
// @function self
// @return[1] `process` the current running process
// @return[2] nil if it was called from outside of a process
// @see process
static int clp_getself(lua_State *L) {
lua_pushliteral(L,CLP_INSTANCE_KEY);
lua_gettable(L, LUA_REGISTRYINDEX);
if(!lua_isnil(L,-1)) {
instance_t i=lua_touserdata(L,-1);
lua_pop(L,1);
clp_buildprocess(L,i->process);
}
return 1;
}
///
// Get the number of available processors
//
// @function cpus
//
// @treturn int the number of processors
static int get_cpus() {
#ifdef _WIN32
#ifndef _SC_NPROCESSORS_ONLN
SYSTEM_INFO info;
GetSystemInfo(&info);
#define sysconf(a) info.dwNumberOfProcessors
#define _SC_NPROCESSORS_ONLN
#endif
#endif
#ifdef _SC_NPROCESSORS_ONLN
long nprocs = -1;
nprocs = sysconf(_SC_NPROCESSORS_ONLN);
if (nprocs >= 1)
return nprocs;
#endif
return 0;
}
static int clp_setmetatable(lua_State *L) {
lua_pushvalue(L,2);
lua_setmetatable(L,1);
lua_pushvalue(L,1);
return 1;
}
static int clp_getmetatable(lua_State *L) {
if(lua_type(L,1)==LUA_TSTRING) {
const char *tname=lua_tostring(L,1);
luaL_getmetatable(L,tname);
} else {
lua_getmetatable (L,1);
}
return 1;
}
static int clp_cpus(lua_State *L) {
lua_pushnumber(L,get_cpus());
return 1;
}
static int clp_loadlibs(lua_State *L) {
luaL_openlibs(L);
return 0;
}
static void clp_require(lua_State *L, const char *lib, lua_CFunction func) {
#if LUA_VERSION_NUM < 502
lua_getglobal(L,"require");
lua_pushstring(L, lib);
lua_call(L,1,1);
#else
luaL_requiref(L, lib, func, 0);
#endif
}
CLP_EXPORTAPI int luaopen_clp_event(lua_State *L);
CLP_EXPORTAPI int luaopen_clp_scheduler(lua_State *L);
CLP_EXPORTAPI int luaopen_clp_process(lua_State *L);
CLP_EXPORTAPI int luaopen_clp_pool(lua_State *L);
CLP_EXPORTAPI int luaopen_clp_channel(lua_State *L);
static const struct luaL_Reg LuaExportFunctions[] = {
{"version",clp_version},
{"now",clp_gettime},
{"cpus",clp_cpus},
{"getmetatable",clp_getmetatable},
{"setmetatable",clp_setmetatable},
{"self",clp_getself},
{"openlibs",clp_loadlibs},
{NULL,NULL}
};
///
// Creates a new channel.
//
// This function is an alias to the @{channel.new} funcion.
//
// @function chan
//
// @see channel.new
///
// Creates a new process.
//
// This function is an alias to the @{process.new} funcion.
//
// @function spawn
// @see process.new
///
//
// This field holds a reference to the default pool of the process.
//
// Every @{process} belongs this pool upon creation.
//
// @field pool (@{pool}) The default pool.
//
// @see pool
CLP_EXPORTAPI int luaopen_clp(lua_State *L) {
lua_newtable(L);
clp_require(L,"clp.pool",luaopen_clp_pool);
lua_getfield(L,-1,"new");
if(!clp_defaultpool) {
lua_pushvalue(L,-1);
lua_call(L,0,1);
clp_defaultpool=clp_topool(L,-1);
lua_getfield(L,-1,"add");
lua_pushvalue(L,-2);
lua_call(L,1,0);
} else {
clp_buildpool(L,clp_defaultpool);
}
lua_setfield(L,-4,"pool");
//lua_setfield(L,-3,"pool");
lua_pop(L,2);
clp_require(L,"clp.event",luaopen_clp_event);
lua_setfield(L,-2,"event");
clp_require(L,"clp.scheduler",luaopen_clp_scheduler);
lua_setfield(L,-2,"scheduler");
clp_require(L,"clp.channel",luaopen_clp_channel);
lua_getfield(L,-1,"new");
lua_pushvalue(L,-1);
lua_setfield(L,-4,"channel"); //deprecated
lua_setfield(L,-3,"chan");
lua_pop(L,1);
clp_require(L,"clp.process",luaopen_clp_process);
lua_getfield(L,-1,"new");
lua_setfield(L,-3,"spawn");
lua_pop(L,1);
lua_newtable(L);
luaL_loadstring(L,"return function() return require'clp' end");
lua_setfield (L, -2,"__persist");
lua_setmetatable(L,-2);
#if LUA_VERSION_NUM < 502
luaL_register(L, NULL, LuaExportFunctions);
#else
luaL_setfuncs(L, LuaExportFunctions, 0);
#endif
return 1;
};