lock mechanism

This commit is contained in:
Roberto Ierusalimschy 1994-11-08 17:56:39 -02:00
parent aa7b1fcec4
commit 2cf954b8ae
4 changed files with 88 additions and 5 deletions

View File

@ -3,11 +3,13 @@
** TecCGraf - PUC-Rio
*/
char *rcs_fallback="$Id: $";
char *rcs_fallback="$Id: fallback.c,v 1.1 1994/11/07 15:20:56 roberto Exp roberto $";
#include <stdio.h>
#include <stdlib.h>
#include "fallback.h"
#include "opcode.h"
#include "lua.h"
@ -49,3 +51,66 @@ void luaI_orderFB (void)
lua_error("unexpected type at comparison");
}
/*
** Lock routines
*/
static Object *lockArray = NULL;
static int lockSize = 0;
int lua_lock (lua_Object object)
{
int i;
int oldSize;
if (lua_isnil(object))
return -1;
for (i=0; i<lockSize; i++)
if (tag(&lockArray[i]) == LUA_T_NIL)
{
lockArray[i] = *luaI_Address(object);
return i;
}
/* no more empty spaces */
oldSize = lockSize;
if (lockArray == NULL)
{
lockSize = 10;
lockArray = (Object *)malloc(lockSize);
}
else
{
lockSize = 3*oldSize/2 + 5;
lockArray = (Object *)realloc(lockArray, lockSize);
}
if (lockArray == NULL)
{
lockSize = 0;
lua_error("lock - not enough memory");
}
for (i=oldSize; i<lockSize; i++)
tag(&lockArray[i]) = LUA_T_NIL;
lockArray[oldSize] = *luaI_Address(object);
return oldSize;
}
void lua_unlock (int ref)
{
tag(&lockArray[ref]) = LUA_T_NIL;
}
Object *luaI_getlocked (int ref)
{
return &lockArray[ref];
}
void luaI_travlock (void (*fn)(Object *))
{
int i;
for (i=0; i<lockSize; i++)
fn(&lockArray[i]);
}

View File

@ -1,16 +1,20 @@
/*
** $Id: $
** $Id: fallback.h,v 1.1 1994/11/07 15:20:56 roberto Exp roberto $
*/
#ifndef fallback_h
#define fallback_h
#include "opcode.h"
void luaI_errorFB (void);
void luaI_indexFB (void);
void luaI_gettableFB (void);
void luaI_arithFB (void);
void luaI_concatFB (void);
void luaI_orderFB (void);
Object *luaI_getlocked (int ref);
void luaI_travlock (void (*fn)(Object *));
#endif

6
lua.h
View File

@ -2,7 +2,7 @@
** LUA - Linguagem para Usuarios de Aplicacao
** Grupo de Tecnologia em Computacao Grafica
** TeCGraf - PUC-Rio
** $Id: lua.h,v 3.3 1994/11/07 16:34:44 roberto Exp $
** $Id: lua.h,v 3.4 1994/11/07 18:27:39 roberto Exp roberto $
*/
@ -60,6 +60,10 @@ lua_Object lua_getsubscript (void);
int lua_type (lua_Object object);
int lua_lock (lua_Object object);
lua_Object lua_getlocked (int ref);
void lua_unlock (int ref);
/* for lua 1.1 */

View File

@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio
*/
char *rcs_opcode="$Id: opcode.c,v 3.4 1994/11/07 16:34:44 roberto Exp roberto $";
char *rcs_opcode="$Id: opcode.c,v 3.5 1994/11/07 18:27:39 roberto Exp $";
#include <stdio.h>
#include <stdlib.h>
@ -545,14 +545,24 @@ void *lua_getuserdata (lua_Object object)
else return (uvalue(Address(object)));
}
lua_Object lua_getlocked (int ref)
{
adjustC(0);
*(top++) = *luaI_getlocked(ref);
CBase++; /* incorporate object in the stack */
return Ref(top-1);
}
/*
** Get a global object. Return the object handle or NULL on error.
*/
lua_Object lua_getglobal (char *name)
{
int n = lua_findsymbol(name);
if (n < 0) return 0;
adjustC(0);
*(top++) = s_object(n);
CBase++; /* incorporate object in the stack */
return Ref(top-1);
}