wingo pushed a commit to branch wip-whippet in repository guile. commit 823df42d0c91d11fc5f2a76594676513e3cdba81 Author: Andy Wingo <wi...@pobox.com> AuthorDate: Mon Jun 30 16:03:25 2025 +0200
Move scm_cond, scm_mutex definitions to internal header * libguile/threads.c * libguile/threads-internal.h: As it says on the tin! --- libguile/threads-internal.h | 75 +++++++++++++++++++++++++++++++++++++++++ libguile/threads.c | 81 +++------------------------------------------ 2 files changed, 79 insertions(+), 77 deletions(-) diff --git a/libguile/threads-internal.h b/libguile/threads-internal.h index b6132cc1c..dceb8a8ec 100644 --- a/libguile/threads-internal.h +++ b/libguile/threads-internal.h @@ -136,5 +136,80 @@ SCM_INTERNAL SCM_THREAD_LOCAL scm_thread *scm_i_current_thread; SCM_INTERNAL scm_i_pthread_mutex_t scm_i_misc_mutex; +enum scm_mutex_kind { + /* A standard mutex can only be locked once. If you try to lock it + again from the thread that locked it to begin with (the "owner" + thread), it throws an error. It can only be unlocked from the + thread that locked it in the first place. */ + SCM_MUTEX_STANDARD, + /* A recursive mutex can be locked multiple times by its owner. It + then has to be unlocked the corresponding number of times, and like + standard mutexes can only be unlocked by the owner thread. */ + SCM_MUTEX_RECURSIVE, + /* An unowned mutex is like a standard mutex, except that it can be + unlocked by any thread. A corrolary of this behavior is that a + thread's attempt to lock a mutex that it already owns will block + instead of signaling an error, as it could be that some other + thread unlocks the mutex, allowing the owner thread to proceed. + This kind of mutex is a bit strange and is here for use by + SRFI-18. */ + SCM_MUTEX_UNOWNED +}; + +struct scm_mutex { + scm_t_bits tag_and_flags; + /* The thread that owns this mutex, or #f if the mutex is unlocked. */ + SCM owner; + /* Queue of threads waiting for this mutex. */ + SCM waiting; + /* For SCM_MUTEX_RECURSIVE (and only SCM_MUTEX_RECURSIVE), the + recursive lock count. The first lock does not count. */ + int level; + scm_i_pthread_mutex_t lock; +}; + +#define SCM_MUTEXP(x) SCM_HAS_TYP16 (x, scm_tc16_mutex) + +static inline struct scm_mutex* +scm_to_mutex (SCM x) +{ + if (!SCM_MUTEXP (x)) abort (); + return (struct scm_mutex*) SCM_UNPACK_POINTER (x); +} + +static inline SCM +scm_from_mutex (struct scm_mutex *m) +{ + return SCM_PACK_POINTER (m); +} + +static inline enum scm_mutex_kind +scm_mutex_kind (struct scm_mutex *m) +{ + return m->tag_and_flags >> 16; +} + +struct scm_cond { + scm_t_bits tag; + SCM waiting; /* the threads waiting for this condition. */ + /* FIXME: Using one cond with multiple mutexes may race on the waiting + list. */ +}; + +#define SCM_CONDVARP(x) SCM_HAS_TYP16 (x, scm_tc16_condition_variable) + +static inline struct scm_cond* +scm_to_condvar (SCM x) +{ + if (!SCM_CONDVARP (x)) abort (); + return (struct scm_cond*) SCM_UNPACK_POINTER (x); +} + +static inline SCM +scm_from_condvar (struct scm_cond *c) +{ + return SCM_PACK_POINTER (c); +} + #endif /* SCM_THREADS_INTERNAL_H */ diff --git a/libguile/threads.c b/libguile/threads.c index 0226dd750..3fadb1722 100644 --- a/libguile/threads.c +++ b/libguile/threads.c @@ -75,8 +75,6 @@ -#define SCM_MUTEXP(x) SCM_HAS_TYP16 (x, scm_tc16_mutex) -#define SCM_CONDVARP(x) SCM_HAS_TYP16 (x, scm_tc16_condition_variable) #define SCM_VALIDATE_CONDVAR(_pos, _obj) \ SCM_ASSERT_TYPE (SCM_CONDVARP (_obj), (_obj), (_pos), FUNC_NAME, "condvar") #define SCM_VALIDATE_MUTEX(_pos, _obj) \ @@ -942,57 +940,6 @@ SCM_DEFINE (scm_thread_p, "thread?", 1, 0, 0, debugging. */ -enum scm_mutex_kind { - /* A standard mutex can only be locked once. If you try to lock it - again from the thread that locked it to begin with (the "owner" - thread), it throws an error. It can only be unlocked from the - thread that locked it in the first place. */ - SCM_MUTEX_STANDARD, - /* A recursive mutex can be locked multiple times by its owner. It - then has to be unlocked the corresponding number of times, and like - standard mutexes can only be unlocked by the owner thread. */ - SCM_MUTEX_RECURSIVE, - /* An unowned mutex is like a standard mutex, except that it can be - unlocked by any thread. A corrolary of this behavior is that a - thread's attempt to lock a mutex that it already owns will block - instead of signaling an error, as it could be that some other - thread unlocks the mutex, allowing the owner thread to proceed. - This kind of mutex is a bit strange and is here for use by - SRFI-18. */ - SCM_MUTEX_UNOWNED -}; - -struct scm_mutex { - scm_t_bits tag_and_flags; - /* The thread that owns this mutex, or #f if the mutex is unlocked. */ - SCM owner; - /* Queue of threads waiting for this mutex. */ - SCM waiting; - /* For SCM_MUTEX_RECURSIVE (and only SCM_MUTEX_RECURSIVE), the - recursive lock count. The first lock does not count. */ - int level; - scm_i_pthread_mutex_t lock; -}; - -static struct scm_mutex* -scm_to_mutex (SCM x) -{ - if (!SCM_MUTEXP (x)) abort (); - return (struct scm_mutex*) SCM_UNPACK_POINTER (x); -} - -static SCM -scm_from_mutex (struct scm_mutex *m) -{ - return SCM_PACK_POINTER (m); -} - -static enum scm_mutex_kind -mutex_kind (struct scm_mutex *m) -{ - return m->tag_and_flags >> 16; -} - int scm_i_print_mutex (SCM mx, SCM port, scm_print_state *pstate SCM_UNUSED) { @@ -1151,7 +1098,7 @@ SCM_DEFINE (scm_timed_lock_mutex, "lock-mutex", 1, 1, 0, /* Specialized lock_mutex implementations according to the mutex kind. */ - switch (mutex_kind (m)) + switch (scm_mutex_kind (m)) { case SCM_MUTEX_STANDARD: ret = lock_mutex (SCM_MUTEX_STANDARD, m, t, waittime); @@ -1250,7 +1197,7 @@ SCM_DEFINE (scm_unlock_mutex, "unlock-mutex", 1, 0, 0, (SCM mutex), /* Specialized unlock_mutex implementations according to the mutex kind. */ - switch (mutex_kind (m)) + switch (scm_mutex_kind (m)) { case SCM_MUTEX_STANDARD: unlock_mutex (SCM_MUTEX_STANDARD, m, t); @@ -1303,7 +1250,7 @@ SCM_DEFINE (scm_mutex_level, "mutex-level", 1, 0, 0, { SCM_VALIDATE_MUTEX (1, mx); struct scm_mutex *m = scm_to_mutex (mx); - if (mutex_kind (m) == SCM_MUTEX_RECURSIVE) + if (scm_mutex_kind (m) == SCM_MUTEX_RECURSIVE) return scm_from_int (m->level + 1); else if (scm_is_eq (m->owner, SCM_BOOL_F)) return SCM_INUM0; @@ -1328,26 +1275,6 @@ SCM_DEFINE (scm_mutex_locked_p, "mutex-locked?", 1, 0, 0, -struct scm_cond { - scm_t_bits tag; - SCM waiting; /* the threads waiting for this condition. */ - /* FIXME: Using one cond with multiple mutexes may race on the waiting - list. */ -}; - -static struct scm_cond* -scm_to_condvar (SCM x) -{ - if (!SCM_CONDVARP (x)) abort (); - return (struct scm_cond*) SCM_UNPACK_POINTER (x); -} - -static SCM -scm_from_condvar (struct scm_cond *c) -{ - return SCM_PACK_POINTER (c); -} - int scm_i_print_condition_variable (SCM cv, SCM port, scm_print_state *pstate SCM_UNUSED) @@ -1501,7 +1428,7 @@ SCM_DEFINE (scm_timed_wait_condition_variable, "wait-condition-variable", 2, 1, /* Specialized timed_wait implementations according to the mutex kind. */ - switch (mutex_kind (m)) + switch (scm_mutex_kind (m)) { case SCM_MUTEX_STANDARD: ret = timed_wait (SCM_MUTEX_STANDARD, m, c, t, waittime);