static int QPSolver_run(lua_State *L) { SVQP2 *qp = (SVQP2*)luaT_checkudata(L, 1, QPSolver_id); luaL_argcheck(L, lua_isfunction(L, 2) || luaT_isudata(L, 2, torch_Tensor_id), 2, "function or Tensor expected"); if(lua_isfunction(L, 2)) { bool initialize = luaT_optboolean(L, 3, true); bool finalize = luaT_optboolean(L, 4, true); qp->Aclosure = L; qp->Afunction = QPSolver_luaClosure; int result = qp->run(initialize, finalize); if(result == -1) luaL_error(L, qp->errmsg); return 0; } else { bool initialize = luaT_optboolean(L, 4, true); bool finalize = luaT_optboolean(L, 5, true); THTensor *data = (THTensor*)luaT_checkudata(L, 2, torch_Tensor_id); luaL_argcheck(L, data->nDimension == 2, 2, "2D Tensor expected"); luaL_argcheck(L, data->size[1] == qp->n, 2, "invalid size"); struct QPSolver_cClosureParams stuff; lua_getfield(L, 3, "__eval"); bool isCKernel = lua_islightuserdata(L, -1); lua_pop(L, 1); if(isCKernel) { double (*func)(THTensor*, THTensor*, void *) = ( double (*)(THTensor*, THTensor*, void *))luaT_getfieldchecklightudata(L, 3, "__eval"); void *params = luaT_getfieldchecklightudata(L, 3, "__params"); stuff.func = func; stuff.params = params; } else { lua_getfield(L, 3, "eval"); lua_pushvalue(L, 3); stuff.func = NULL; stuff.params = NULL; } stuff.L = L; stuff.data = data; stuff.x1 = THTensor_new(); stuff.x2 = THTensor_new(); luaT_pushudata(L, stuff.x1, torch_Tensor_id); /* auto dealloc */ luaT_pushudata(L, stuff.x2, torch_Tensor_id); /* auto dealloc */ qp->Aclosure = &stuff; qp->Afunction = (stuff.func ? QPSolver_cClosure : QPSolver_cLuaClosure); int result = qp->run(initialize, finalize); if(result == -1) luaL_error(L, qp->errmsg); return 0; } }
int flock_open(lua_State *L) { const char *file_name = lua_tostring(L, 1); int no_block = luaT_optboolean(L, 2, 0); int flags = O_CLOEXEC | O_RDWR; if (!no_block) { flags |= O_CREAT; } int fd = open(file_name, flags, S_IRUSR | S_IWUSR); if (fd < 0) { if (errno == ENOENT || errno == EACCES) return 0; return LUA_HANDLE_ERROR(L, errno); } flags = LOCK_EX; if (no_block) { flags |= LOCK_NB; } int ret = flock(fd, flags); if (ret < 0) { close(fd); if ((flags & LOCK_NB) && (errno == EWOULDBLOCK)) return 0; return LUA_HANDLE_ERROR(L, errno); } int *handle = lua_newuserdata(L, sizeof(int)); *handle = fd; luaL_getmetatable(L, "ipc.flock"); lua_setmetatable(L, -2); return 1; }
static int torch_PipeFile_new(lua_State *L) { const char *name = luaL_checkstring(L, 1); const char *mode = luaL_optstring(L, 2, "r"); int isQuiet = luaT_optboolean(L, 3, 0); THFile *self = THPipeFile_new(name, mode, isQuiet); luaT_pushudata(L, self, "torch.PipeFile"); return 1; }
int workqueue_read(lua_State *L) { context_t *context = (context_t *)lua_touserdata(L, 1); workqueue_t *workqueue = context->workqueue; if (!workqueue) return LUA_HANDLE_ERROR_STR(L, "workqueue is not open"); int doNotBlock = luaT_optboolean(L, 2, 0); if (workqueue->owner_thread == pthread_self()) { return workqueue_queue_read(L, &workqueue->answers, doNotBlock); } else { return workqueue_queue_read(L, &workqueue->questions, doNotBlock); } }
static int torch_DiskFile_new(lua_State *L) { const char *name = luaL_checkstring(L, 1); const char *mode = luaL_optstring(L, 2, "r"); int isQuiet = luaT_optboolean(L, 3, 0); int isReadable; int isWritable; FILE *handle; DiskFile *file; luaL_argcheck(L, torch_DiskFile_c_mode(mode, &isReadable, &isWritable), 2, "file mode should be 'r','w' or 'rw'"); if( isReadable && isWritable ) { handle = fopen(name, "r+b"); if(!handle) { handle = fopen(name, "wb"); if(handle) { fclose(handle); handle = fopen(name, "r+b"); } } } else handle = fopen(name, (isReadable ? "rb" : "wb")); if(!handle) { if(isQuiet) return 0; else luaL_error(L, "cannot open <%s> in mode %c%c", name, (isReadable ? 'r' : ' '), (isWritable ? 'w' : ' ')); } file = luaT_alloc(L, sizeof(DiskFile)); file->handle = handle; file->flags.isQuiet = isQuiet; file->flags.isReadable = isReadable; file->flags.isWritable = isWritable; file->isNativeEncoding = 1; file->flags.isBinary = 0; file->flags.isAutoSpacing = 1; file->flags.hasError = 0; file->name = luaT_alloc(L, strlen(name)+1); strcpy(file->name, name); luaT_pushudata(L, file, torch_DiskFile_id); return 1; }