pike.git / src / threads.c

version» Context lines:

pike.git/src/threads.c:1:   #include "global.h" - RCSID("$Id: threads.c,v 1.31 1997/09/03 08:07:36 mast Exp $"); + RCSID("$Id: threads.c,v 1.32 1997/09/04 21:51:29 per Exp $");      int num_threads = 1;   int threads_disabled = 0;      #ifdef _REENTRANT   #include "threads.h"   #include "array.h"   #include "object.h"   #include "pike_macros.h"   #include "callback.h"
pike.git/src/threads.c:154:      /* Note:    * No reference is kept to the key object, it is destructed if the    * mutex is destructed. The key pointer is set to zero by the    * key object when the key is destructed.    */      struct mutex_storage   {    COND_T condition; +  MUTEX_T kludge;    struct object *key;   };      struct key_storage   {    struct mutex_storage *mut;    struct object *owner;    int initialized;   };   
pike.git/src/threads.c:179:   {    struct mutex_storage *m;    struct object *o;       pop_n_elems(args);    m=THIS_MUTEX;    /* Needs to be cloned here, since create()    * might use threads.    */    o=clone_object(mutex_key,0); -  mt_lock(& mutex_kluge); +  mt_lock(& m->kludge);    if(m->key && OB2KEY(m->key)->owner == thread_id)    {    THREADS_FPRINTF((stderr, "Recursive LOCK k:%08x, m:%08x(%08x), t:%08x\n",    (unsigned int)OB2KEY(m->key),    (unsigned int)m,    (unsigned int)OB2KEY(m->key)->mut,    (unsigned int) thread_id)); -  mt_unlock(&mutex_kluge); +     free_object(o);    error("Recursive mutex locks!\n");    }       THREADS_ALLOW(); -  while(m->key) co_wait(& m->condition, & mutex_kluge); +  while(m->key) co_wait(& m->condition, & m->kludge);    m->key=o;    OB2KEY(o)->mut=m;    -  mt_unlock(&mutex_kluge); +  mt_unlock(&m->kludge);    THREADS_DISALLOW();    THREADS_FPRINTF((stderr, "LOCK k:%08x, m:%08x(%08x), t:%08x\n",    (unsigned int)OB2KEY(o),    (unsigned int)m,    (unsigned int)OB2KEY(m->key)->mut,    (unsigned int)thread_id));    push_object(o);   }      void f_mutex_trylock(INT32 args)   {    struct mutex_storage *m;    struct object *o;    int i=0;    pop_n_elems(args);       o=clone_object(mutex_key,0);    m=THIS_MUTEX;    -  mt_lock(& mutex_kluge); +        /* No reason to release the interpreter lock here    * since we aren't calling any functions that take time.    */    -  +  mt_lock(& m->kludge);    if(m->key && OB2KEY(m->key)->owner == thread_id)    { -  mt_unlock(&mutex_kluge); +  mt_unlock(&m->kludge);    free_object(o);    error("Recursive mutex locks!\n");    }    if(!m->key)    {    OB2KEY(o)->mut=m;    m->key=o;    i=1;    } -  mt_unlock(&mutex_kluge); +  mt_unlock(&m->kludge);       if(i)    {    push_object(o);    } else {    destruct(o);    free_object(o);    push_int(0);    }   }      void init_mutex_obj(struct object *o)   {    co_init(& THIS_MUTEX->condition); -  +  mt_init(& THIS_MUTEX->kludge);    THIS_MUTEX->key=0;   }      void exit_mutex_obj(struct object *o)   {    if(THIS_MUTEX->key) destruct(THIS_MUTEX->key); -  +  THIS_MUTEX->key=0;    co_destroy(& THIS_MUTEX->condition); -  +  mt_destroy(& THIS_MUTEX->kludge);   }      #define THIS_KEY ((struct key_storage *)(fp->current_storage))   void init_mutex_key_obj(struct object *o)   {    THREADS_FPRINTF((stderr, "KEY k:%08x, o:%08x\n",    (unsigned int)THIS_KEY, (unsigned int)thread_id));    THIS_KEY->mut=0;    THIS_KEY->owner=thread_id;    thread_id->refs++;    THIS_KEY->initialized=1;   }      void exit_mutex_key_obj(struct object *o)   {    THREADS_FPRINTF((stderr, "UNLOCK k:%08x m:(%08x) t:%08x o:%08x\n",    (unsigned int)THIS_KEY,    (unsigned int)THIS_KEY->mut,    (unsigned int)thread_id,    (unsigned int)THIS_KEY->owner)); -  mt_lock(& mutex_kluge); +     if(THIS_KEY->mut)    { -  +  mt_lock(& THIS_KEY->mut->kludge);   #ifdef DEBUG    if(THIS_KEY->mut->key != o)    fatal("Mutex unlock from wrong key %p != %p!\n",THIS_KEY->mut->key,o);   #endif    THIS_KEY->mut->key=0;    if (THIS_KEY->owner) {    free_object(THIS_KEY->owner);    THIS_KEY->owner=0;    }    co_signal(& THIS_KEY->mut->condition);    THIS_KEY->mut=0;    THIS_KEY->initialized=0; -  +  mt_unlock(& THIS_KEY->mut->kludge);    } -  mt_unlock(& mutex_kluge); +    }      #define THIS_COND ((COND_T *)(fp->current_storage))   void f_cond_wait(INT32 args)   {    COND_T *c;    struct object *key;       if(args > 1) pop_n_elems(args - 1);   
pike.git/src/threads.c:318:    struct mutex_storage *mut;       if(sp[-1].type != T_OBJECT)    error("Bad argument 1 to condition->wait()\n");       key=sp[-1].u.object;       if(key->prog != mutex_key)    error("Bad argument 1 to condition->wait()\n");    -  mt_lock(&mutex_kluge); +     mut=OB2KEY(key)->mut; -  if(!mut) -  error("Bad argument 1 to condition->wait()\n"); +  if(!mut) error("Bad argument 1 to condition->wait()\n");    -  +  mt_lock(&mut->kludge);    THREADS_ALLOW(); -  +     /* Unlock mutex */    mut->key=0;    OB2KEY(key)->mut=0;    co_signal(& mut->condition);       /* Wait and allow mutex operations */ -  co_wait(c,&mutex_kluge); +  co_wait(c,&mut->kludge);       if(OB2KEY(key)->initialized)    {    /* Lock mutex */ -  while(mut->key) co_wait(& mut->condition, & mutex_kluge); +  while(mut->key) co_wait(& mut->condition, &mut->kludge);    mut->key=key;    OB2KEY(key)->mut=mut;    } -  mt_unlock(&mutex_kluge); +  mt_unlock(&mut->kludge);    THREADS_DISALLOW();    pop_stack();    } else { -  mt_lock(&mutex_kluge); +     THREADS_ALLOW(); -  +  mt_lock(&mutex_kluge);    co_wait(c, &mutex_kluge);    mt_unlock(&mutex_kluge);    THREADS_DISALLOW();    }   }      void f_cond_signal(INT32 args) { pop_n_elems(args); co_signal(THIS_COND); }   void f_cond_broadcast(INT32 args) { pop_n_elems(args); co_broadcast(THIS_COND); }   void init_cond_obj(struct object *o) { co_init(THIS_COND); }   void exit_cond_obj(struct object *o) { co_destroy(THIS_COND); }