author | Francesco Abbate <francesco.bbt@gmail.com> | 2010年07月10日 16:09:32 +0200 |
---|---|---|
committer | Francesco Abbate <francesco.bbt@gmail.com> | 2010年07月10日 16:09:32 +0200 |
commit | 3bb87ad6eaae77b17a0f76675cbe90fd6047b075 (patch) | |
tree | df5b85b50fae85905133d48ea95b95399899e47d | |
parent | de267b3fa4c88df329be0fe557a052285ecf834a (diff) | |
download | gsl-shell-3bb87ad6eaae77b17a0f76675cbe90fd6047b075.tar.gz |
-rw-r--r-- | agg-plot/lua-plot.cpp | 1 | ||||
-rw-r--r-- | agg-plot/plot-window.cpp | 96 | ||||
-rw-r--r-- | agg-plot/plot-window.h | 8 |
diff --git a/agg-plot/lua-plot.cpp b/agg-plot/lua-plot.cpp index 3322662f..68d71559 100644 --- a/agg-plot/lua-plot.cpp +++ b/agg-plot/lua-plot.cpp @@ -463,6 +463,7 @@ agg_plot_show (lua_State *L) pthread_attr_destroy (attr); mlua_window_unref(L, p->id); p->id = -1; + AGG_UNLOCK(); return luaL_error(L, "error creating thread."); } diff --git a/agg-plot/plot-window.cpp b/agg-plot/plot-window.cpp index 2110ea28..91bacd31 100644 --- a/agg-plot/plot-window.cpp +++ b/agg-plot/plot-window.cpp @@ -7,6 +7,7 @@ extern "C" { #include "platform/agg_platform_support.h" +#include "gsl-shell.h" #include "plot-window.h" #include "lua-cpp-utils.h" #include "lua-utils.h" @@ -22,6 +23,7 @@ extern "C" { static void * win_thread_function (void *_win); static int plot_window_new (lua_State *L); + static int plot_window_free (lua_State *L); }; static const struct luaL_Reg plotwin_functions[] = { @@ -29,16 +31,22 @@ static const struct luaL_Reg plotwin_functions[] = { {NULL, NULL} }; +static const struct luaL_Reg agg_window_methods[] = { + {"__gc", plot_window_free}, + {NULL, NULL} +}; -class the_application : public agg::platform_support +pthread_mutex_t window_mutex[1]; + +class window_app : public agg::platform_support { public: - the_application(agg::pix_format_e format, bool flip_y) : + window_app(agg::pix_format_e format, bool flip_y) : agg::platform_support(format, flip_y) { }; - virtual ~the_application() { }; + virtual ~window_app() { }; virtual void on_draw() { @@ -49,18 +57,17 @@ public: }; class plot_window { - private: - enum win_status_e { not_ready, running, closed }; - - the_application *m_app; - int m_lua_id; - enum win_status_e m_status; + window_app *m_app; public: + enum win_status_e { not_ready, starting, running, error, closed }; + + int id; + enum win_status_e status; plot_window() : - m_app(NULL), m_lua_id(-1), m_status(not_ready) + m_app(NULL), id(-1), status(not_ready) { }; @@ -70,31 +77,43 @@ public: delete m_app; }; - void set_id(int id) { m_lua_id = id; }; - int id() const { return m_lua_id; }; - - void close() { m_status = closed; }; - void set_running() { m_status = plot_window::running; }; - - void main_loop() + void start() { - m_app = new the_application(agg::pix_format_bgr24, true); + WINDOW_LOCK(); + + m_app = new window_app(agg::pix_format_bgr24, true); m_app->caption("GSL shell plot"); if (m_app->init(780, 400, agg::window_resize)) { + this->status = plot_window::running; + + WINDOW_UNLOCK(); m_app->run(); - m_status = plot_window::closed; - // gsl_shell_unref_plot (m_lua_id); + + WINDOW_LOCK(); + this->status = plot_window::closed; + WINDOW_UNLOCK(); + + GSL_SHELL_LOCK(); + gsl_shell_unref_plot (this->id); + GSL_SHELL_UNLOCK(); } + + WINDOW_UNLOCK(); } + + static plot_window *check (lua_State *L, int index); }; void * win_thread_function (void *_win) { printf("debugging threads!!\n"); + + platform_support_prepare(); + plot_window *win = (plot_window *) _win; - win->main_loop(); + win->start(); return NULL; } @@ -103,8 +122,9 @@ plot_window_new (lua_State *L) { plot_window *win = new(L, GS_AGG_WINDOW) plot_window; - int id = mlua_window_ref(L, 1); - win->set_id(id); + WINDOW_LOCK(); + + win->id = mlua_window_ref(L, 1); pthread_attr_t attr[1]; pthread_t win_thread[1]; @@ -114,21 +134,45 @@ plot_window_new (lua_State *L) if (pthread_create(win_thread, attr, win_thread_function, (void*) win)) { + mlua_window_unref(L, win->id); + pthread_attr_destroy (attr); - mlua_window_unref(L, win->id()); + win->status = plot_window::error; + WINDOW_UNLOCK(); + luaL_error(L, "error creating thread"); } pthread_attr_destroy (attr); - win->set_running(); + win->status = plot_window::starting; + WINDOW_UNLOCK(); return 1; } +int +plot_window_free (lua_State *L) +{ + plot_window *win = plot_window::check (L, 1); + printf("freying plot window\n"); + win->~plot_window(); + return 0; +} + +plot_window * +plot_window::check (lua_State *L, int index) +{ + return (plot_window *) gs_check_userdata (L, index, GS_AGG_WINDOW); +} + void plot_window_register (lua_State *L) { - platform_support_prepare(); + pthread_mutex_init (window_mutex, NULL); + + luaL_newmetatable (L, GS_METATABLE(GS_AGG_WINDOW)); + luaL_register (L, NULL, agg_window_methods); + lua_pop (L, 1); /* gsl module registration */ luaL_register (L, NULL, plotwin_functions); diff --git a/agg-plot/plot-window.h b/agg-plot/plot-window.h index a0b95b5f..2a7baf11 100644 --- a/agg-plot/plot-window.h +++ b/agg-plot/plot-window.h @@ -1,12 +1,20 @@ #ifndef PLOT_WINDOW_H #define PLOT_WINDOW_H +#include <pthread.h> + #include "defs.h" __BEGIN_DECLS extern void plot_window_register (lua_State *L); +extern pthread_mutex_t window_mutex[1]; + +#define WINDOW_LOCK() pthread_mutex_lock (window_mutex); +#define WINDOW_UNLOCK() pthread_mutex_unlock (window_mutex); + __END_DECLS + #endif |