Created
March 26, 2015 02:41
-
-
Save wolfiestyle/e928cc4df37d93506a06 to your computer and use it in GitHub Desktop.
lua sleep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <lua.h> | |
#include <lauxlib.h> | |
#ifdef _WIN32 | |
#include <windows.h> | |
#define EXPORT __declspec(dllexport) | |
#else | |
#include <sys/time.h> | |
#include <unistd.h> | |
#define EXPORT | |
#endif | |
static int lc_sleep(lua_State *L) | |
{ | |
int ms = luaL_checkint(L, 1); | |
if (ms < 0) | |
return luaL_error(L, "argument must be a positive integer"); | |
#ifdef _WIN32 | |
Sleep(ms); | |
#else | |
struct timeval tv; | |
tv.tv_sec = (long)ms / 1000L; | |
tv.tv_usec = (long)ms % 1000L * 1000L; | |
select(0, NULL, NULL, NULL, &tv); | |
#endif | |
return 0; | |
} | |
EXPORT int luaopen_sleep(lua_State *L) | |
{ | |
lua_pushcfunction(L, lc_sleep); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment