mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-05-09 23:30:21 +02:00
Separate fat mutex unlock and wait operations
* libguile/threads.c (fat_mutex_unlock, fat_mutex_wait): Separate wait from unlock. (scm_unlock_mutex, scm_timed_wait_condition_variable): Adapt.
This commit is contained in:
parent
56dd476af7
commit
fc4df456a1
1 changed files with 72 additions and 60 deletions
|
@ -1277,21 +1277,14 @@ typedef struct {
|
|||
#define SCM_CONDVARP(x) SCM_SMOB_PREDICATE (scm_tc16_condvar, x)
|
||||
#define SCM_CONDVAR_DATA(x) ((fat_cond *) SCM_SMOB_DATA (x))
|
||||
|
||||
static int
|
||||
fat_mutex_unlock (SCM mutex, SCM cond,
|
||||
const scm_t_timespec *waittime, int relock)
|
||||
static void
|
||||
fat_mutex_unlock (SCM mutex)
|
||||
{
|
||||
SCM owner;
|
||||
fat_mutex *m = SCM_MUTEX_DATA (mutex);
|
||||
fat_cond *c = NULL;
|
||||
scm_i_thread *t = SCM_I_CURRENT_THREAD;
|
||||
int err = 0, ret = 0;
|
||||
|
||||
scm_i_scm_pthread_mutex_lock (&m->lock);
|
||||
|
||||
owner = m->owner;
|
||||
|
||||
if (!scm_is_eq (owner, t->handle))
|
||||
if (!scm_is_eq (m->owner, SCM_I_CURRENT_THREAD->handle))
|
||||
{
|
||||
if (m->level == 0)
|
||||
{
|
||||
|
@ -1305,9 +1298,39 @@ fat_mutex_unlock (SCM mutex, SCM cond,
|
|||
}
|
||||
}
|
||||
|
||||
if (! (SCM_UNBNDP (cond)))
|
||||
if (m->level > 0)
|
||||
m->level--;
|
||||
if (m->level == 0)
|
||||
/* Change the owner of MUTEX. */
|
||||
m->owner = unblock_from_queue (m->waiting);
|
||||
|
||||
scm_i_pthread_mutex_unlock (&m->lock);
|
||||
}
|
||||
|
||||
static int
|
||||
fat_mutex_wait (SCM cond, SCM mutex, const scm_t_timespec *waittime)
|
||||
{
|
||||
c = SCM_CONDVAR_DATA (cond);
|
||||
fat_cond *c = SCM_CONDVAR_DATA (cond);
|
||||
fat_mutex *m = SCM_MUTEX_DATA (mutex);
|
||||
scm_i_thread *t = SCM_I_CURRENT_THREAD;
|
||||
int err = 0, ret = 0;
|
||||
|
||||
scm_i_scm_pthread_mutex_lock (&m->lock);
|
||||
|
||||
if (!scm_is_eq (m->owner, t->handle))
|
||||
{
|
||||
if (m->level == 0)
|
||||
{
|
||||
scm_i_pthread_mutex_unlock (&m->lock);
|
||||
scm_misc_error (NULL, "mutex not locked", SCM_EOL);
|
||||
}
|
||||
else if (m->kind != FAT_MUTEX_UNOWNED)
|
||||
{
|
||||
scm_i_pthread_mutex_unlock (&m->lock);
|
||||
scm_misc_error (NULL, "mutex not locked by current thread", SCM_EOL);
|
||||
}
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
int brk = 0;
|
||||
|
@ -1341,8 +1364,7 @@ fat_mutex_unlock (SCM mutex, SCM cond,
|
|||
|
||||
if (brk)
|
||||
{
|
||||
if (relock)
|
||||
scm_lock_mutex_timed (mutex, SCM_UNDEFINED, SCM_UNDEFINED);
|
||||
scm_lock_mutex (mutex);
|
||||
t->block_asyncs--;
|
||||
break;
|
||||
}
|
||||
|
@ -1354,18 +1376,6 @@ fat_mutex_unlock (SCM mutex, SCM cond,
|
|||
|
||||
scm_i_scm_pthread_mutex_lock (&m->lock);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (m->level > 0)
|
||||
m->level--;
|
||||
if (m->level == 0)
|
||||
/* Change the owner of MUTEX. */
|
||||
m->owner = unblock_from_queue (m->waiting);
|
||||
|
||||
scm_i_pthread_mutex_unlock (&m->lock);
|
||||
ret = 1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -1379,7 +1389,9 @@ SCM_DEFINE (scm_unlock_mutex, "unlock-mutex", 1, 0, 0, (SCM mx),
|
|||
{
|
||||
SCM_VALIDATE_MUTEX (1, mx);
|
||||
|
||||
return scm_from_bool (fat_mutex_unlock (mx, SCM_UNDEFINED, NULL, 0));
|
||||
fat_mutex_unlock (mx);
|
||||
|
||||
return SCM_BOOL_T;
|
||||
}
|
||||
#undef FUNC_NAME
|
||||
|
||||
|
@ -1480,7 +1492,7 @@ SCM_DEFINE (scm_timed_wait_condition_variable, "wait-condition-variable", 2, 1,
|
|||
waitptr = &waittime;
|
||||
}
|
||||
|
||||
return fat_mutex_unlock (mx, cv, waitptr, 1) ? SCM_BOOL_T : SCM_BOOL_F;
|
||||
return scm_from_bool (fat_mutex_wait (cv, mx, waitptr));
|
||||
}
|
||||
#undef FUNC_NAME
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue