1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-04-30 11:50:28 +02:00

* null-threads.h (scm_null_mutex, scm_null_mutex_init,

scm_null_mutex_lock, scm_null_mutex_unlock,
scm_null_mutex_destroy, scm_null_condvar, scm_null_condvar_init,
scm_null_condvar_wait, scm_null_condvar_signal,
scm_null_condvar_destroy): Removed.
(scm_mutex_init, scm_mutex_lock, scm_mutex_unlock, scm_cond_init,
scm_cond_wait, scm_cond_signal, scm_cond_broadcast,
scm_cond_destory): Do not define, they are now deprecated and
handled by threads.{h,c}.

* null-threads.c (scm_null_mutex, scm_null_cond): Define here.
(scm_threads_init): Create smobs here, using the appropriate
sizes.
(block): Removed, now unused.
(scm_c_thread_exited_p): New.
(scm_null_mutex_init, scm_null_mutex_lock, scm_null_mutex_unlock,
scm_null_mutex_destroy, scm_null_condvar_init,
scm_null_condvar_wait, scm_null_condvar_signal,
scm_null_condvar_destroy): Removed and updated users to do their
task directly.
(scm_try_mutex, timeval_subtract,
scm_timed_wait_condition_variable,
scm_broadcast_condition_variable): New.
(scm_wait_condition_variable): Removed.
This commit is contained in:
Marius Vollmer 2002-10-27 20:12:27 +00:00
parent 79cd5b8eda
commit 4b9154e73e
2 changed files with 125 additions and 115 deletions

View file

@ -49,14 +49,28 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
#include <time.h> #include <time.h>
#include <stdio.h>
void *scm_null_threads_data; void *scm_null_threads_data;
static SCM main_thread; static SCM main_thread;
typedef struct {
int level;
} scm_null_mutex;
typedef struct {
int signalled;
} scm_null_cond;
void void
scm_threads_init (SCM_STACKITEM *i) scm_threads_init (SCM_STACKITEM *i)
{ {
scm_tc16_thread = scm_make_smob_type ("thread", 0);
scm_tc16_mutex = scm_make_smob_type ("mutex", sizeof (scm_null_mutex));
scm_tc16_condvar = scm_make_smob_type ("condition-variable",
sizeof (scm_null_cond));
main_thread = scm_permanent_object (scm_cell (scm_tc16_thread, 0)); main_thread = scm_permanent_object (scm_cell (scm_tc16_thread, 0));
scm_null_threads_data = NULL; scm_null_threads_data = NULL;
} }
@ -147,124 +161,104 @@ scm_join_thread (SCM thread)
} }
#undef FUNC_NAME #undef FUNC_NAME
int
scm_c_thread_exited_p (SCM thread)
#define FUNC_NAME s_scm_thread_exited_p
{
return 0;
}
#undef FUNC_NAME
SCM SCM
scm_yield (void) scm_yield (void)
{ {
return SCM_BOOL_T; return SCM_BOOL_T;
} }
/* Block until a new async might have been queued.
*/
static void
block ()
{
select (0, NULL, NULL, NULL, NULL);
}
int
scm_null_mutex_init (scm_null_mutex *m)
{
m->locked = 0;
return 0;
}
int
scm_null_mutex_lock (scm_null_mutex *m)
{
while (m->locked)
{
block ();
SCM_ASYNC_TICK;
}
m->locked = 1;
return 1;
}
int
scm_null_mutex_unlock (scm_null_mutex *m)
{
if (m->locked == 0)
return 0;
m->locked = 0;
return 1;
}
int
scm_null_mutex_destroy (scm_null_mutex *m)
{
return 1;
}
SCM SCM
scm_make_mutex (void) scm_make_mutex (void)
{ {
SCM m = scm_make_smob (scm_tc16_mutex); SCM m = scm_make_smob (scm_tc16_mutex);
scm_null_mutex_init (SCM_MUTEX_DATA(m)); scm_null_mutex *mx = SCM_MUTEX_DATA(m);
mx->level = 0;
return m; return m;
} }
SCM SCM
scm_lock_mutex (SCM m) scm_lock_mutex (SCM m)
{ {
scm_null_mutex *mx;
SCM_ASSERT (SCM_MUTEXP (m), m, SCM_ARG1, s_lock_mutex); SCM_ASSERT (SCM_MUTEXP (m), m, SCM_ARG1, s_lock_mutex);
scm_null_mutex_lock (SCM_MUTEX_DATA(m)); mx = SCM_MUTEX_DATA(m);
mx->level++;
return SCM_BOOL_T; return SCM_BOOL_T;
} }
SCM
scm_try_mutex (SCM m)
{
return scm_lock_mutex (m); /* will always succeed right away. */
}
SCM SCM
scm_unlock_mutex (SCM m) scm_unlock_mutex (SCM m)
{ {
scm_null_mutex *mx;
SCM_ASSERT (SCM_MUTEXP (m), m, SCM_ARG1, s_unlock_mutex); SCM_ASSERT (SCM_MUTEXP (m), m, SCM_ARG1, s_unlock_mutex);
if (!scm_null_mutex_unlock (SCM_MUTEX_DATA(m))) mx = SCM_MUTEX_DATA(m);
if (mx->level == 0)
scm_misc_error (s_unlock_mutex, "mutex is not locked", SCM_EOL); scm_misc_error (s_unlock_mutex, "mutex is not locked", SCM_EOL);
mx->level--;
return SCM_BOOL_T; return SCM_BOOL_T;
} }
int
scm_null_condvar_init (scm_null_condvar *c)
{
c->signalled = 0;
return 0;
}
int
scm_null_condvar_wait (scm_null_condvar *c, scm_null_mutex *m)
{
scm_null_mutex_unlock (m);
while (!c->signalled)
{
block ();
SCM_ASYNC_TICK;
}
scm_null_mutex_lock (m);
c->signalled = 0;
return 0;
}
int
scm_null_condvar_signal (scm_null_condvar *c)
{
c->signalled = 1;
return 0;
}
int
scm_null_condvar_destroy (scm_null_condvar *c)
{
return 1;
}
SCM SCM
scm_make_condition_variable (void) scm_make_condition_variable (void)
{ {
scm_null_cond *cv;
SCM c = scm_make_smob (scm_tc16_condvar); SCM c = scm_make_smob (scm_tc16_condvar);
scm_null_condvar_init (SCM_CONDVAR_DATA (c)); cv = SCM_CONDVAR_DATA (c);
cv->signalled = 0;
return c; return c;
} }
SCM /* Subtract the `struct timeval' values X and Y,
scm_wait_condition_variable (SCM c, SCM m) storing the result in RESULT. Might modify Y.
Return 1 if the difference is negative or zero, otherwise 0. */
static int
timeval_subtract (result, x, y)
struct timeval *result, *x, *y;
{ {
/* Perform the carry for the later subtraction by updating Y. */
if (x->tv_usec < y->tv_usec) {
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
y->tv_usec -= 1000000 * nsec;
y->tv_sec += nsec;
}
if (x->tv_usec - y->tv_usec > 1000000) {
int nsec = (x->tv_usec - y->tv_usec) / 1000000;
y->tv_usec += 1000000 * nsec;
y->tv_sec -= nsec;
}
/* Compute the time remaining to wait.
`tv_usec' is certainly positive. */
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_usec = x->tv_usec - y->tv_usec;
/* Return 1 if result is negative or zero. */
return x->tv_sec < y->tv_sec
|| (result->tv_sec == 0 && result->tv_usec == 0);
}
SCM
scm_timed_wait_condition_variable (SCM c, SCM m, SCM t)
#define FUNC_NAME s_wait_condition_variable
{
scm_null_cond *cv;
struct timeval waittime;
SCM_ASSERT (SCM_CONDVARP (c), SCM_ASSERT (SCM_CONDVARP (c),
c, c,
SCM_ARG1, SCM_ARG1,
@ -273,21 +267,67 @@ scm_wait_condition_variable (SCM c, SCM m)
m, m,
SCM_ARG2, SCM_ARG2,
s_wait_condition_variable); s_wait_condition_variable);
scm_null_condvar_wait (SCM_CONDVAR_DATA (c), SCM_MUTEX_DATA (m)); if (!SCM_UNBNDP (t))
{
if (SCM_CONSP (t))
{
SCM_VALIDATE_UINT_COPY (3, SCM_CAR(t), waittime.tv_sec);
SCM_VALIDATE_UINT_COPY (3, SCM_CDR(t), waittime.tv_usec);
}
else
{
SCM_VALIDATE_UINT_COPY (3, t, waittime.tv_sec);
waittime.tv_usec = 0;
}
}
cv = SCM_CONDVAR_DATA (c);
scm_unlock_mutex (m);
while (!cv->signalled)
{
if (SCM_UNBNDP (t))
select (0, NULL, NULL, NULL, NULL);
else
{
struct timeval now, then, diff;
then = waittime;
gettimeofday (&now, NULL);
if (timeval_subtract (&diff, &then, &now))
break;
select (0, NULL, NULL, NULL, &diff);
}
SCM_ASYNC_TICK;
}
scm_lock_mutex (m);
if (cv->signalled)
{
cv->signalled = 0;
return SCM_BOOL_T; return SCM_BOOL_T;
}
return SCM_BOOL_F;
} }
#undef FUNC_NAME
SCM SCM
scm_signal_condition_variable (SCM c) scm_signal_condition_variable (SCM c)
{ {
scm_null_cond *cv;
SCM_ASSERT (SCM_CONDVARP (c), SCM_ASSERT (SCM_CONDVARP (c),
c, c,
SCM_ARG1, SCM_ARG1,
s_signal_condition_variable); s_signal_condition_variable);
scm_null_condvar_signal (SCM_CONDVAR_DATA (c)); cv = SCM_CONDVAR_DATA (c);
cv->signalled = 1;
return SCM_BOOL_T; return SCM_BOOL_T;
} }
SCM
scm_broadcast_condition_variable (SCM c)
{
return scm_signal_condition_variable (c); /* only one thread anyway. */
}
unsigned long unsigned long
scm_thread_usleep (unsigned long usec) scm_thread_usleep (unsigned long usec)
{ {

View file

@ -54,36 +54,6 @@
#define SCM_CRITICAL_SECTION_END #define SCM_CRITICAL_SECTION_END
#define SCM_THREAD_SWITCHING_CODE #define SCM_THREAD_SWITCHING_CODE
typedef struct scm_null_mutex {
int locked;
} scm_null_mutex;
SCM_API int scm_null_mutex_init (scm_null_mutex *);
SCM_API int scm_null_mutex_lock (scm_null_mutex *);
SCM_API int scm_null_mutex_unlock (scm_null_mutex *);
SCM_API int scm_null_mutex_destroy (scm_null_mutex *);
typedef scm_null_mutex scm_t_mutex;
#define scm_mutex_init scm_null_mutex_init
#define scm_mutex_lock scm_null_mutex_lock
#define scm_mutex_unlock scm_null_mutex_unlock
typedef struct scm_null_condvar {
int signalled;
} scm_null_condvar;
SCM_API int scm_null_condvar_init (scm_null_condvar *);
SCM_API int scm_null_condvar_wait (scm_null_condvar *, scm_null_mutex *);
SCM_API int scm_null_condvar_signal (scm_null_condvar *);
SCM_API int scm_null_condvar_destroy (scm_null_condvar *);
typedef scm_null_condvar scm_t_cond;
#define scm_cond_init scm_null_condvar_init
#define scm_cond_wait scm_null_condvar_wait
#define scm_cond_signal scm_null_condvar_signal
#define scm_cond_broadcast scm_null_condvar_signal /* yes */
#define scm_cond_destroy scm_null_condvar_destroy
SCM_API void *scm_null_threads_data; SCM_API void *scm_null_threads_data;
#define SCM_THREAD_LOCAL_DATA (scm_null_threads_data) #define SCM_THREAD_LOCAL_DATA (scm_null_threads_data)