Skip to content
Snippets Groups Projects
Commit a49ee6fc authored by Fredrik Hübinette (Hubbe)'s avatar Fredrik Hübinette (Hubbe)
Browse files

recursive mutexes implemented through mutex->lock(2)

Rev: src/threads.c:1.92
parent 819b1fc5
No related branches found
No related tags found
No related merge requests found
#include "global.h"
RCSID("$Id: threads.c,v 1.91 1999/03/21 21:34:29 grubba Exp $");
RCSID("$Id: threads.c,v 1.92 1999/04/02 23:23:47 hubbe Exp $");
int num_threads = 1;
int threads_disabled = 0;
......@@ -16,6 +16,7 @@ int threads_disabled = 0;
#include "program.h"
#include "gc.h"
#include "main.h"
#include "module_support.h"
int live_threads = 0;
COND_T live_threads_change;
......@@ -617,14 +618,23 @@ void f_mutex_lock(INT32 args)
{
struct mutex_storage *m;
struct object *o;
INT_TYPE type;
m=THIS_MUTEX;
/* Needs to be cloned here, since create()
* might use threads.
*/
o=clone_object(mutex_key,0);
if(!args || IS_ZERO(sp-args))
if(!args)
type=0;
else
get_all_args("mutex->lock",args,"%i",&type);
switch(type)
{
default:
bad_arg_error("mutex->lock", sp-args, args, 2, "int(0..2)", sp+1-args,
"Unknown mutex locking style: %d\n",type);
case 0:
case 2:
if(m->key && OB2KEY(m->key)->owner == thread_id)
{
THREADS_FPRINTF(0,
......@@ -633,11 +643,21 @@ void f_mutex_lock(INT32 args)
(unsigned int)m,
(unsigned int)OB2KEY(m->key)->mut,
(unsigned int) thread_id));
free_object(o);
error("Recursive mutex locks!\n");
if(type==0) error("Recursive mutex locks!\n");
pop_n_elems(args);
push_int(0);
}
case 1:
break;
}
/* Needs to be cloned here, since create()
* might use threads.
*/
o=clone_object(mutex_key,0);
if(m->key)
{
SWAP_OUT_CURRENT_THREAD();
......@@ -664,24 +684,39 @@ void f_mutex_trylock(INT32 args)
{
struct mutex_storage *m;
struct object *o;
int type;
int i=0;
o=clone_object(mutex_key,0);
m=THIS_MUTEX;
/* No reason to release the interpreter lock here
* since we aren't calling any functions that take time.
*/
if(!args || IS_ZERO(sp-args))
m=THIS_MUTEX;
if(!args)
type=0;
else
get_all_args("mutex->lock",args,"%i",&type);
switch(type)
{
default:
bad_arg_error("mutex->trylock", sp-args, args, 2, "int(0..2)", sp+1-args,
"Unknown mutex locking style: %d\n",type);
case 0:
if(m->key && OB2KEY(m->key)->owner == thread_id)
{
free_object(o);
error("Recursive mutex locks!\n");
}
case 2:
case 1:
break;
}
o=clone_object(mutex_key,0);
if(!m->key)
{
OB2KEY(o)->mut=m;
......@@ -1007,8 +1042,7 @@ void th_init(void)
start_new_program();
ADD_STORAGE(struct mutex_storage);
/* function(int|void:object) */
ADD_FUNCTION("lock",f_mutex_lock,tFunc(tOr(tInt,tVoid),tObj),0);
add_function("lock",f_mutex_lock,"function(int(0..2)|void:object)",0);
/* function(int|void:object) */
ADD_FUNCTION("trylock",f_mutex_trylock,tFunc(tOr(tInt,tVoid),tObj),0);
set_init_callback(init_mutex_obj);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment