Introduce gsl_shell_state and improve factorization - gsl-shell.git - gsl-shell

index : gsl-shell.git
gsl-shell
summary refs log tree commit diff
diff options
context:
space:
mode:
authorFrancesco Abbate <francesco.bbt@gmail.com>2012年03月29日 17:23:35 +0200
committerFrancesco Abbate <francesco.bbt@gmail.com>2012年04月01日 22:57:19 +0200
commit5c80bf0af85627c0e62d0f097e9ed6679f6bed73 (patch)
tree53c88940049bef966c8994cf59f831c26061a8c2
parente255825367665691584ae44755df5f2882a25c66 (diff)
downloadgsl-shell-5c80bf0af85627c0e62d0f097e9ed6679f6bed73.tar.gz
Introduce gsl_shell_state and improve factorization
Diffstat
-rw-r--r--agg-plot/canvas-window-cpp.h 8
-rw-r--r--agg-plot/canvas-window.cpp 20
-rw-r--r--agg-plot/lua-graph.cpp 18
-rw-r--r--agg-plot/window-cpp.h 3
-rw-r--r--agg-plot/window.cpp 9
-rw-r--r--fox-gui/Makefile 2
-rw-r--r--fox-gui/fx_console.h 2
-rw-r--r--fox-gui/gsl_shell_interp.cpp 82
-rw-r--r--fox-gui/gsl_shell_interp.h 29
-rw-r--r--fox-gui/gsl_shell_thread.cpp 39
-rw-r--r--fox-gui/gsl_shell_thread.h 9
-rw-r--r--fox-gui/gsl_shell_window.cpp 8
-rw-r--r--fox-gui/gsl_shell_window.h 2
-rw-r--r--gsl-shell-jit.c 28
-rw-r--r--gsl-shell.h 13
-rw-r--r--lua-graph.h 2
-rw-r--r--lua-gsl.h 17
-rw-r--r--lua-gsl/Makefile 2
-rw-r--r--lua-gsl/fatal.c (renamed from fox-gui/fatal.cpp)3
-rw-r--r--lua-gsl/fatal.h (renamed from fox-gui/fatal.h)6
-rw-r--r--lua-gsl/lua-gsl.c 32
-rw-r--r--lua-gsl/strpp.h (renamed from agg-plot/strpp.h)9
22 files changed, 188 insertions, 155 deletions
diff --git a/agg-plot/canvas-window-cpp.h b/agg-plot/canvas-window-cpp.h
index 36696c2e..bad27eba 100644
--- a/agg-plot/canvas-window-cpp.h
+++ b/agg-plot/canvas-window-cpp.h
@@ -16,6 +16,7 @@ extern "C" {
#include "defs.h"
#include "canvas.h"
#include "utils.h"
+#include "lua-gsl.h"
class canvas_window : public platform_support_ext {
protected:
@@ -25,6 +26,7 @@ protected:
agg::trans_affine m_matrix;
pthread_t m_thread;
+ gsl_shell_state* m_gsl_shell;
public:
@@ -40,9 +42,10 @@ public:
enum win_status_e status;
- canvas_window(agg::rgba& bgcol) :
+ canvas_window(gsl_shell_state* gs, agg::rgba& bgcol) :
platform_support_ext(agg::pix_format_bgr24, true),
- m_canvas(NULL), m_bgcolor(bgcol), m_matrix(), status(not_ready)
+ m_canvas(NULL), m_bgcolor(bgcol), m_matrix(), m_gsl_shell(gs),
+ status(not_ready)
{ };
virtual ~canvas_window()
@@ -55,6 +58,7 @@ public:
virtual void on_resize(int sx, int sy);
void shutdown_close();
+ gsl_shell_state* state() { return m_gsl_shell; }
bool start_new_thread (std::auto_ptr<thread_info>& inf);
diff --git a/agg-plot/canvas-window.cpp b/agg-plot/canvas-window.cpp
index 8d1f1b75..bcec7ac7 100644
--- a/agg-plot/canvas-window.cpp
+++ b/agg-plot/canvas-window.cpp
@@ -105,14 +105,16 @@ canvas_thread_function (void *_inf)
win->unlock();
- pthread_mutex_lock (gsl_shell_shutdown_mutex);
- if (!gsl_shell_shutting_down)
+ gsl_shell_state* gs = win->state();
+
+ pthread_mutex_lock (&gs->shutdown_mutex);
+ if (!gs->is_shutting_down)
{
- GSL_SHELL_LOCK();
- window_index_remove (inf->L, inf->window_id);
- GSL_SHELL_UNLOCK();
+ pthread_mutex_lock(&gs->exec_mutex);
+ window_index_remove (gs->L, inf->window_id);
+ pthread_mutex_unlock(&gs->exec_mutex);
}
- pthread_mutex_unlock (gsl_shell_shutdown_mutex);
+ pthread_mutex_unlock (&gs->shutdown_mutex);
return NULL;
}
@@ -125,9 +127,11 @@ canvas_window::shutdown_close()
{
close_request();
unlock();
- pthread_mutex_unlock (gsl_shell_shutdown_mutex);
+
+ gsl_shell_state* gs = this->m_gsl_shell;
+ pthread_mutex_unlock (&gs->shutdown_mutex);
pthread_join(m_thread, NULL);
- pthread_mutex_lock (gsl_shell_shutdown_mutex);
+ pthread_mutex_lock (&gs->shutdown_mutex);
}
else
{
diff --git a/agg-plot/lua-graph.cpp b/agg-plot/lua-graph.cpp
index 6c3c7bc9..32174bde 100644
--- a/agg-plot/lua-graph.cpp
+++ b/agg-plot/lua-graph.cpp
@@ -59,13 +59,15 @@ register_graph (lua_State *L)
}
void
-lua_close_with_graph (lua_State* L)
+gsl_shell_close_with_graph (struct gsl_shell_state* gs)
{
- pthread_mutex_lock (gsl_shell_shutdown_mutex);
- gsl_shell_shutting_down = 1;
- GSL_SHELL_LOCK();
- graph_close_windows(L);
- lua_close(L);
- pthread_mutex_unlock (gsl_shell_shutdown_mutex);
- GSL_SHELL_UNLOCK();
+ pthread_mutex_lock (&gs->shutdown_mutex);
+ gs->is_shutting_down = 1;
+ pthread_mutex_lock(&gs->exec_mutex);
+ graph_close_windows(gs->L);
+ lua_close(gs->L);
+ pthread_mutex_unlock(&gs->shutdown_mutex);
+ pthread_mutex_unlock(&gs->exec_mutex);
+
+ gsl_shell_close(gs);
}
diff --git a/agg-plot/window-cpp.h b/agg-plot/window-cpp.h
index 02f72987..73025489 100644
--- a/agg-plot/window-cpp.h
+++ b/agg-plot/window-cpp.h
@@ -60,7 +60,8 @@ private:
ref::node* m_tree;
public:
- window(agg::rgba& bgcol) : canvas_window(bgcol), m_tree(0)
+ window(gsl_shell_state* gs, agg::rgba bgcol= colors::white):
+ canvas_window(gs, bgcol), m_tree(0)
{
this->split(".");
};
diff --git a/agg-plot/window.cpp b/agg-plot/window.cpp
index 4a412b7b..f48a725c 100644
--- a/agg-plot/window.cpp
+++ b/agg-plot/window.cpp
@@ -423,7 +423,14 @@ void window::start (lua_State *L, gslshell::ret_status& st)
int
window_new (lua_State *L)
{
- window *win = push_new_object<window>(L, GS_WINDOW, colors::white);
+ lua_getfield(L, LUA_REGISTRYINDEX, "__gsl_shell");
+ gsl_shell_state* gs = (gsl_shell_state*) lua_touserdata(L, -1);
+ lua_pop(L, 1);
+
+ if (unlikely(gs == NULL))
+ return luaL_error(L, "cannot create window: not a GSL Shell state");
+
+ window *win = push_new_object<window>(L, GS_WINDOW, gs);
const char *spec = lua_tostring (L, 1);
gslshell::ret_status st;
diff --git a/fox-gui/Makefile b/fox-gui/Makefile
index 49b14b44..3447f170 100644
--- a/fox-gui/Makefile
+++ b/fox-gui/Makefile
@@ -35,7 +35,7 @@ LIBS += $(LUADIR)/src/libluajit.a $(GSH_BASE_DIR)/agg-plot/libaggplot.a $(GSH_BA
INCLUDES += $(PTHREADS_CFLAGS)
LIBS += $(AGG_LIBS) $(PTHREADS_LIBS) $(GSL_LIBS)
-FOXGUI_SRC_FILES = fatal.cpp fx_console.cpp redirect.cpp gsl_shell_interp.cpp gsl_shell_thread.cpp gsl_shell_window.cpp gsl-shell-fox.cpp
+FOXGUI_SRC_FILES = fx_console.cpp redirect.cpp gsl_shell_interp.cpp gsl_shell_thread.cpp gsl_shell_window.cpp gsl-shell-fox.cpp
FOXGUI_OBJ_FILES := $(FOXGUI_SRC_FILES:%.cpp=%.o)
DEP_FILES := $(FOXGUI_SRC_FILES:%.cpp=.deps/%.P)
diff --git a/fox-gui/fx_console.h b/fox-gui/fx_console.h
index c76b650c..d654113e 100644
--- a/fox-gui/fx_console.h
+++ b/fox-gui/fx_console.h
@@ -44,6 +44,8 @@ public:
public:
virtual void create();
+ void stop() { m_engine.stop(); }
+
long on_key_press(FXObject*,FXSelector,void*);
long on_read_input(FXObject*,FXSelector,void*);
diff --git a/fox-gui/gsl_shell_interp.cpp b/fox-gui/gsl_shell_interp.cpp
index e71c1f67..fa5aa7da 100644
--- a/fox-gui/gsl_shell_interp.cpp
+++ b/fox-gui/gsl_shell_interp.cpp
@@ -1,7 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <signal.h>
#define luajit_c
@@ -12,14 +11,11 @@ extern "C" {
#include "luajit.h"
}
+#include "gsl_shell_interp.h"
#include "lua-gsl.h"
#include "lua-graph.h"
-
-#include "gsl_shell_interp.h"
#include "fatal.h"
-lua_State *globalL = NULL;
-
static void stderr_message(const char *pname, const char *msg)
{
if (pname) fprintf(stderr, "%s: ", pname);
@@ -38,16 +34,6 @@ static int stderr_report(lua_State *L, int status)
return status;
}
-static void lstop(lua_State *L, lua_Debug *ar)
-{
- (void)ar; /* unused arg. */
- lua_sethook(L, NULL, 0, 0);
- /* Avoid luaL_error -- a C hook doesn't add an extra frame. */
- luaL_where(L, 0);
- lua_pushfstring(L, "%sinterrupted!", lua_tostring(L, -1));
- lua_error(L);
-}
-
static int traceback(lua_State *L)
{
if (!lua_isstring(L, 1)) /* 'message' not a string? */
@@ -68,22 +54,13 @@ static int traceback(lua_State *L)
return 1;
}
-static void laction(int i)
-{
- signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
- terminate process (default action) */
- lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
-}
-
static int docall(lua_State *L, int narg, int clear)
{
int status;
int base = lua_gettop(L) - narg; /* function index */
lua_pushcfunction(L, traceback); /* push traceback function */
lua_insert(L, base); /* put it under chunk and args */
- signal(SIGINT, laction);
status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
- signal(SIGINT, SIG_DFL);
lua_remove(L, base); /* remove traceback function */
/* force a complete garbage collection in case of errors */
if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
@@ -125,50 +102,31 @@ static int incomplete(lua_State *L, int status)
return 0; /* else... */
}
-gsl_shell::~gsl_shell()
-{
- if (m_lua_state)
- {
- lua_close_with_graph(m_lua_state);
- gsl_shell_close();
- }
-}
-
void gsl_shell::init()
{
- lua_State *L;
- int status;
+ int status = lua_cpcall(this->L, pinit, NULL);
- GSL_SHELL_LOCK();
- gsl_shell_init();
- L = lua_open(); /* create state */
- globalL = L;
-
- if (unlikely(L == NULL))
- fatal_exception("cannot create state: not enough memory");
-
- status = lua_cpcall(L, pinit, NULL);
-
- if (unlikely(report(L, status)))
+ if (unlikely(stderr_report(this->L, status)))
{
- lua_close(L);
+ lua_close(this->L);
fatal_exception("cannot initialize Lua state");
}
+}
- m_lua_state = L;
+void gsl_shell::close()
+{
+ gsl_shell_close_with_graph(this);
}
-int gsl_shell::report(lua_State* L, int status)
+
+int gsl_shell::error_report(int status)
{
+ lua_State* L = this->L;
if (status && !lua_isnil(L, -1))
{
const char *msg = lua_tostring(L, -1);
if (msg == NULL) msg = "(error object is not a string)";
-
- int np = snprintf(m_error_msg, ERROR_MSG_MAX_LENGTH, "%s", msg);
- if (np >= ERROR_MSG_MAX_LENGTH)
- m_error_msg[ERROR_MSG_MAX_LENGTH - 1] = 0;
-
+ m_error_msg = msg;
lua_pop(L, 1);
}
return status;
@@ -176,8 +134,7 @@ int gsl_shell::report(lua_State* L, int status)
int gsl_shell::exec(const char *line)
{
- pthread::auto_lock lock(m_interp);
- lua_State* L = m_lua_state;
+ lua_State* L = this->L;
int status = luaL_loadbuffer(L, line, strlen(line), "=<user input>");
@@ -192,18 +149,7 @@ int gsl_shell::exec(const char *line)
lua_gc(L, LUA_GCCOLLECT, 0);
}
- report(L, status);
- GSL_SHELL_UNLOCK();
+ error_report(status);
return (status == 0 ? eval_success : eval_error);
}
-
-void gsl_shell::lock()
-{
- GSL_SHELL_LOCK();
-}
-
-void gsl_shell::unlock()
-{
- GSL_SHELL_UNLOCK();
-}
diff --git a/fox-gui/gsl_shell_interp.h b/fox-gui/gsl_shell_interp.h
index d7be98e9..f31b6d15 100644
--- a/fox-gui/gsl_shell_interp.h
+++ b/fox-gui/gsl_shell_interp.h
@@ -1,34 +1,31 @@
#ifndef GSL_SHELL_INTERP_H
#define GSL_SHELL_INTERP_H
-#include "defs.h"
-#include "pthreadpp.h"
-
extern "C" {
-#include "lua.h"
+#include <lua.h>
}
-class gsl_shell {
- enum { ERROR_MSG_MAX_LENGTH = 128 };
+#include "defs.h"
+#include "pthreadpp.h"
+#include "lua-gsl.h"
+#include "strpp.h"
+
+class gsl_shell : public gsl_shell_state {
public:
enum eval_result_e { eval_success, eval_error, incomplete_input };
- gsl_shell(): m_lua_state(0) { }
- ~gsl_shell();
+ gsl_shell() { gsl_shell_open(this); }
+ ~gsl_shell() { }
void init();
+ void close();
int exec(const char* line);
- const char* error_msg() const { return m_error_msg; }
-
- void lock();
- void unlock();
+ const char* error_msg() const { return m_error_msg.cstr(); }
private:
- int report(lua_State* L, int status);
+ int error_report(int status);
- lua_State* m_lua_state;
- pthread::mutex m_interp;
- char m_error_msg[ERROR_MSG_MAX_LENGTH];
+ str m_error_msg;
};
#endif
diff --git a/fox-gui/gsl_shell_thread.cpp b/fox-gui/gsl_shell_thread.cpp
index 11329836..9f9080ba 100644
--- a/fox-gui/gsl_shell_thread.cpp
+++ b/fox-gui/gsl_shell_thread.cpp
@@ -10,6 +10,7 @@ void *
luajit_eval_thread (void *userdata)
{
gsl_shell_thread* eng = (gsl_shell_thread*) userdata;
+ eng->lock();
eng->init();
eng->run();
pthread_exit (NULL);
@@ -34,6 +35,12 @@ start_interp_thread (gsl_shell_thread* eng)
return 0;
}
+gsl_shell_thread::gsl_shell_thread():
+ m_status(starting), m_redirect(4096), m_line_pending(0),
+ m_exit_request(false)
+{
+}
+
gsl_shell_thread::~gsl_shell_thread()
{
m_redirect.stop();
@@ -52,21 +59,44 @@ gsl_shell_thread::run()
{
m_eval.lock();
m_status = ready;
+
this->unlock();
m_eval.wait();
+ this->lock();
+
+ if (m_exit_request)
+ {
+ m_eval.unlock();
+ break;
+ }
const char* line = m_line_pending;
m_line_pending = NULL;
m_status = busy;
m_eval.unlock();
- this->lock();
m_eval_status = this->exec(line);
fputc(eot_character, stdout);
fflush(stdout);
}
+
+ this->unlock();
+ this->close();
+
+ m_eval.lock();
+ m_eval.signal();
+ m_eval.unlock();
+}
+
+void
+gsl_shell_thread::stop()
+{
+ pthread::auto_lock lock(m_eval);
+ m_exit_request = true;
+ m_eval.signal();
+ m_eval.wait();
}
void
@@ -86,10 +116,3 @@ gsl_shell_thread::read(char* buffer, unsigned buffer_size)
{
return m_redirect.read(buffer, buffer_size);
}
-
-bool gsl_shell_thread::is_ready()
-{
- pthread::auto_lock lock(m_eval);
- bool is_ready = (m_status == ready);
- return is_ready;
-}
diff --git a/fox-gui/gsl_shell_thread.h b/fox-gui/gsl_shell_thread.h
index 1e0c9c4e..1763de32 100644
--- a/fox-gui/gsl_shell_thread.h
+++ b/fox-gui/gsl_shell_thread.h
@@ -14,25 +14,28 @@ public:
enum engine_status_e { starting, ready, busy, terminated };
enum { eot_character = 0x04 };
- gsl_shell_thread(): m_status(starting), m_redirect(4096), m_line_pending(0) {}
+ gsl_shell_thread();
~gsl_shell_thread();
void input(const char* line);
void start();
void run();
- bool is_ready();
+ void stop();
+
+ void lock() { pthread_mutex_lock(&this->exec_mutex); }
+ void unlock() { pthread_mutex_unlock(&this->exec_mutex); }
int read(char* buffer, unsigned buffer_size);
int eval_status() const { return m_eval_status; }
private:
-
engine_status_e m_status;
stdout_redirect m_redirect;
pthread::cond m_eval;
const char* m_line_pending;
int m_eval_status;
+ bool m_exit_request;
};
#endif
diff --git a/fox-gui/gsl_shell_window.cpp b/fox-gui/gsl_shell_window.cpp
index 74a6c3d1..56f4ebd9 100644
--- a/fox-gui/gsl_shell_window.cpp
+++ b/fox-gui/gsl_shell_window.cpp
@@ -7,6 +7,8 @@
#endif
FXDEFMAP(gsl_shell_window) gsl_shell_window_map[]={
+ FXMAPFUNC(SEL_CLOSE, 0, gsl_shell_window::on_close),
+
};
FXIMPLEMENT(gsl_shell_window,FXMainWindow,gsl_shell_window_map,ARRAYNUMBER(gsl_shell_window_map))
@@ -36,3 +38,9 @@ void gsl_shell_window::create()
FXMainWindow::create();
show(PLACEMENT_SCREEN);
}
+
+long gsl_shell_window::on_close(FXObject* obj, FXSelector sel, void* ptr)
+{
+ m_text->stop();
+ return FXMainWindow::close(FALSE);
+}
diff --git a/fox-gui/gsl_shell_window.h b/fox-gui/gsl_shell_window.h
index 0e388358..80dc8dcf 100644
--- a/fox-gui/gsl_shell_window.h
+++ b/fox-gui/gsl_shell_window.h
@@ -18,6 +18,8 @@ public:
virtual void create();
+ long on_close(FXObject* obj, FXSelector sel, void* ptr);
+
protected:
gsl_shell_window() {}
diff --git a/gsl-shell-jit.c b/gsl-shell-jit.c
index 99b8b2db..0ee07f60 100644
--- a/gsl-shell-jit.c
+++ b/gsl-shell-jit.c
@@ -38,6 +38,7 @@
#include "lualib.h"
#include "luajit.h"
#include "lua-gsl.h"
+#include "gsl-shell.h"
#include "completion.h"
#include "lua-graph.h"
@@ -96,7 +97,8 @@ static void my_freeline (lua_State *L, char *b) { }
#endif
-static lua_State *globalL = NULL;
+lua_State *globalL = NULL;
+struct gsl_shell_state gsl_shell[1];
static const char *progname = LUA_PROGNAME;
static void gsl_shell_openlibs(lua_State *L)
@@ -285,9 +287,9 @@ static int pushline (lua_State *L, int firstline) {
size_t l;
const char *prmt = get_prompt(L, firstline);
- GSL_SHELL_UNLOCK();
+ pthread_mutex_unlock(&gsl_shell->exec_mutex);
b = my_readline(L, buffer, prmt);
- GSL_SHELL_LOCK();
+ pthread_mutex_lock(&gsl_shell->exec_mutex);
if (b == NULL)
return 0; /* no input */
@@ -650,22 +652,18 @@ int main(int argc, char **argv)
#ifdef USE_READLINE
initialize_readline();
#endif
- gsl_shell_init();
+ gsl_shell_open(gsl_shell);
+
+ pthread_mutex_lock(&gsl_shell->exec_mutex);
- GSL_SHELL_LOCK();
- lua_State *L = lua_open(); /* create state */
- if (L == NULL) {
- l_message(argv[0], "cannot create state: not enough memory");
- return EXIT_FAILURE;
- }
s.argc = argc;
s.argv = argv;
- status = lua_cpcall(L, pmain, &s);
- report(L, status);
- GSL_SHELL_UNLOCK();
+ status = lua_cpcall(gsl_shell->L, pmain, &s);
+ report(gsl_shell->L, status);
+
+ pthread_mutex_unlock(&gsl_shell->exec_mutex);
- lua_close_with_graph(L);
- gsl_shell_close();
+ gsl_shell_close_with_graph(gsl_shell);
return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
}
diff --git a/gsl-shell.h b/gsl-shell.h
new file mode 100644
index 00000000..92a1d8a5
--- /dev/null
+++ b/gsl-shell.h
@@ -0,0 +1,13 @@
+#ifndef GSL_SHELL_INCLUDED
+#define GSL_SHELL_INCLUDED
+
+#include "defs.h"
+#include <lua.h>
+
+__BEGIN_DECLS
+
+extern lua_State *globalL;
+
+__END_DECLS
+
+#endif
diff --git a/lua-graph.h b/lua-graph.h
index 4c43d4d3..e7bb621f 100644
--- a/lua-graph.h
+++ b/lua-graph.h
@@ -9,7 +9,7 @@ __BEGIN_DECLS
extern void graph_close_windows (lua_State *L);
extern void register_graph (lua_State *L);
-extern void lua_close_with_graph (lua_State* L);
+extern void gsl_shell_close_with_graph (struct gsl_shell_state* gs);
__END_DECLS
diff --git a/lua-gsl.h b/lua-gsl.h
index b944acb1..b5f8de0e 100644
--- a/lua-gsl.h
+++ b/lua-gsl.h
@@ -6,20 +6,21 @@
#include "defs.h"
__BEGIN_DECLS
+
#include <lua.h>
-extern pthread_mutex_t gsl_shell_mutex[1];
-extern pthread_mutex_t gsl_shell_shutdown_mutex[1];
-extern volatile int gsl_shell_shutting_down;
+struct gsl_shell_state {
+ lua_State *L;
+ pthread_mutex_t exec_mutex;
+ pthread_mutex_t shutdown_mutex;
+ int is_shutting_down;
+};
-extern void gsl_shell_init ();
-extern void gsl_shell_close ();
+extern void gsl_shell_open (struct gsl_shell_state *gs);
+extern void gsl_shell_close (struct gsl_shell_state *gs);
extern int luaopen_gsl (lua_State *L);
__END_DECLS
-#define GSL_SHELL_LOCK() pthread_mutex_lock (gsl_shell_mutex)
-#define GSL_SHELL_UNLOCK() pthread_mutex_unlock (gsl_shell_mutex)
-
#endif
diff --git a/lua-gsl/Makefile b/lua-gsl/Makefile
index 3ff4f7cb..386803f8 100644
--- a/lua-gsl/Makefile
+++ b/lua-gsl/Makefile
@@ -29,7 +29,7 @@ LUADIR = $(GSH_BASE_DIR)/luajit2
INCLUDES += -I$(LUADIR)/src -I$(GSH_BASE_DIR)
-LUAGSL_SRC_FILES = gs-types.c lua-utils.c lua-gsl.c str.c
+LUAGSL_SRC_FILES = gs-types.c lua-utils.c lua-gsl.c str.c fatal.c
LUAGSL_OBJ_FILES := $(LUAGSL_SRC_FILES:%.c=%.o)
DEP_FILES := $(LUAGSL_SRC_FILES:%.c=.deps/%.P)
diff --git a/fox-gui/fatal.cpp b/lua-gsl/fatal.c
index 6e121b33..da739527 100644
--- a/fox-gui/fatal.cpp
+++ b/lua-gsl/fatal.c
@@ -2,9 +2,12 @@
#include <stdio.h>
#include <stdlib.h>
+#include "fatal.h"
+
void
fatal_exception(const char* msg)
{
fputs(msg, stderr);
+ fputs("\n", stderr);
abort();
}
diff --git a/fox-gui/fatal.h b/lua-gsl/fatal.h
index 771362e2..27814349 100644
--- a/fox-gui/fatal.h
+++ b/lua-gsl/fatal.h
@@ -1,6 +1,12 @@
#ifndef MY_FATAL_H
#define MY_FATAL_H
+#include "defs.h"
+
+__BEGIN_DECLS
+
extern void fatal_exception(const char* msg) __attribute__ ((noreturn));
+__END_DECLS
+
#endif
diff --git a/lua-gsl/lua-gsl.c b/lua-gsl/lua-gsl.c
index 2c2c28e4..836fbf96 100644
--- a/lua-gsl/lua-gsl.c
+++ b/lua-gsl/lua-gsl.c
@@ -27,16 +27,29 @@
#include "lua-gsl.h"
#include "gs-types.h"
#include "lua-utils.h"
+#include "fatal.h"
-pthread_mutex_t gsl_shell_mutex[1];
-pthread_mutex_t gsl_shell_shutdown_mutex[1];
-volatile int gsl_shell_shutting_down = 0;
+void
+gsl_shell_open (struct gsl_shell_state *gs)
+{
+ gs->L = lua_open(); /* create state */
+
+ if (unlikely(gs->L == NULL))
+ fatal_exception("cannot create state: not enough memory");
+
+ lua_pushlightuserdata(gs->L, (void*) gs);
+ lua_setfield(gs->L, LUA_REGISTRYINDEX, "__gsl_shell");
+
+ pthread_mutex_init (&gs->exec_mutex, NULL);
+ pthread_mutex_init (&gs->shutdown_mutex, NULL);
+ gs->is_shutting_down = 0;
+}
void
-gsl_shell_init ()
+gsl_shell_close (struct gsl_shell_state *gs)
{
- pthread_mutex_init (gsl_shell_mutex, NULL);
- pthread_mutex_init (gsl_shell_shutdown_mutex, NULL);
+ pthread_mutex_destroy (&gs->exec_mutex);
+ pthread_mutex_destroy (&gs->shutdown_mutex);
}
int
@@ -49,10 +62,3 @@ luaopen_gsl (lua_State *L)
return 0;
}
-
-void
-gsl_shell_close ()
-{
- pthread_mutex_destroy (gsl_shell_mutex);
- pthread_mutex_destroy (gsl_shell_shutdown_mutex);
-}
diff --git a/agg-plot/strpp.h b/lua-gsl/strpp.h
index f1fdfe80..a3ca0a2e 100644
--- a/agg-plot/strpp.h
+++ b/lua-gsl/strpp.h
@@ -11,11 +11,18 @@ public:
~str() { str_free(this); }
- const str& operator = (const str& s) {
+ const str& operator= (const str& s)
+ {
str_copy(this, &s);
return *this;
}
+ const str& operator= (const char* s)
+ {
+ str_copy_c(this, s);
+ return *this;
+ }
+
const char* cstr() const { return CSTR(this); }
void append(const str& s, int sep = 0) { str_append(this, &s, sep); }
generated by cgit v1.2.3 (git 2.39.1) at 2025年09月17日 18:31:36 +0000

AltStyle によって変換されたページ (->オリジナル) /