klisp

an open source interpreter for the Kernel Programming Language.
git clone http://git.hanabi.in/repos/klisp.git
Log | Files | Refs | README

commit 9ad9c4bd442cca8f5bce81653f785480287f30e6
parent 226e61b5a24d6cdbbc9d9e5ca193acc1dfebd52f
Author: Andres Navarro <canavarro82@gmail.com>
Date:   Wed, 22 Aug 2012 01:52:50 -0300

Added a GIL (Global Interpreter Lock) for synchronization.  It uses the pthread library.

Diffstat:
Msrc/klimits.h | 8++++++++
Msrc/kstate.c | 10+++++++++-
Msrc/kstate.h | 4++++
3 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/src/klimits.h b/src/klimits.h @@ -86,6 +86,14 @@ #define MINREADLINEBUFFER 80 #endif +/* XXX for now ignore the return values */ +#ifndef klisp_lock +#include <pthread.h> +#define klisp_lock(K) ((void) (pthread_mutex_lock(&G(K)->gil))) +#define klisp_unlock(K) ((void) (pthread_mutex_unlock(&G(K)->gil))) +#endif + +/* These were the original defines */ #ifndef klisp_lock #define klisp_lock(K) ((void) 0) #define klisp_unlock(K) ((void) 0) diff --git a/src/kstate.c b/src/kstate.c @@ -17,6 +17,7 @@ #include <stddef.h> #include <setjmp.h> #include <string.h> +#include <pthread.h> #include "klisp.h" #include "klimits.h" @@ -83,6 +84,10 @@ static void f_klispopen (klisp_State *K, void *ud) { ks_tbsize(K) = KS_ITBSIZE; ks_tbidx(K) = 0; /* buffer is empty */ ks_tbuf(K) = (char *)b; + + /* (at least for now) we'll use a non recursive mutex for the GIL */ + /* XXX/TODO check return code */ + pthread_mutex_init(&g->gil, NULL); /* This is here in lua, but in klisp we still need to alloc a bunch of objects: @@ -155,7 +160,10 @@ static void close_state(klisp_State *K) klispM_freemem(K, ks_sbuf(K), ks_ssize(K) * sizeof(TValue)); klispM_freemem(K, ks_tbuf(K), ks_tbsize(K)); /* free string/symbol table */ - klispM_freearray(K, G(K)->strt.hash, G(K)->strt.size, GCObject *); + klispM_freearray(K, g->strt.hash, G(K)->strt.size, GCObject *); + + /* destroy the GIL */ + pthread_mutex_destroy(&g->gil); /* only remaining mem should be of the state struct */ klisp_assert(g->totalbytes == sizeof(KG)); diff --git a/src/kstate.h b/src/kstate.h @@ -13,6 +13,7 @@ #include <stdio.h> #include <setjmp.h> +#include <pthread.h> #include "klimits.h" #include "kobject.h" @@ -129,6 +130,9 @@ typedef struct global_State { /* The main thread */ klisp_State *mainthread; + /* The GIL (Global Interpreter Lock) */ + /* (at least for now) we'll use a non recursive mutex */ + pthread_mutex_t gil; } global_State; struct klisp_State {