Changeset 1871118 in mainline for kernel/generic/include


Ignore:
Timestamp:
2023-02-10T22:59:11Z (3 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
master, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
11d2c983
Parents:
daadfa6
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2023-02-10 22:53:12)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2023-02-10 22:59:11)
Message:

Make thread_t reference counted

This simplifies interaction between various locks and thread
lifespan, which simplifies things. For example, threads_lock can
now simply be a mutex protecting the global it was made for, and
nothing more.

Location:
kernel/generic/include
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/include/lib/refcount.h

    rdaadfa6 r1871118  
    7676}
    7777
     78/**
     79 * Try to upgrade a weak reference.
     80 * Naturally, this is contingent on another form of synchronization being used
     81 * to ensure that the object continues to exist while the weak reference is in
     82 * use.
     83 */
     84static inline bool refcount_try_up(atomic_refcount_t *rc)
     85{
     86        int cnt = atomic_load_explicit(&rc->__cnt, memory_order_relaxed);
     87
     88        while (cnt >= 0) {
     89                if (atomic_compare_exchange_weak_explicit(&rc->__cnt, &cnt, cnt + 1,
     90                    memory_order_relaxed, memory_order_relaxed)) {
     91                        return true;
     92                }
     93        }
     94
     95        return false;
     96}
     97
    7898static inline bool refcount_unique(atomic_refcount_t *rc)
    7999{
  • kernel/generic/include/proc/thread.h

    rdaadfa6 r1871118  
    7070/** Thread structure. There is one per thread. */
    7171typedef struct thread {
     72        atomic_refcount_t refcount;
     73
    7274        link_t rq_link;  /**< Run queue link. */
    7375        link_t wq_link;  /**< Wait queue link. */
     
    7779        odlink_t lthreads;
    7880
     81        /**
     82         * If true, the thread is terminating.
     83         * It will not go to sleep in interruptible synchronization functions
     84         * and will call thread_exit() before returning to userspace.
     85         */
     86        volatile bool interrupted;
     87
     88        /** Waitq for thread_join_timeout(). */
     89        waitq_t join_wq;
     90
    7991        /** Lock protecting thread structure.
    8092         *
    81          * Protects the whole thread structure except list links above.
     93         * Protects the whole thread structure except fields listed above.
    8294         */
    8395        IRQ_SPINLOCK_DECLARE(lock);
     
    133145         */
    134146        bool in_copy_to_uspace;
    135 
    136         /**
    137          * If true, the thread will not go to sleep at all and will call
    138          * thread_exit() before returning to userspace.
    139          */
    140         bool interrupted;
    141 
    142         /** If true, thread_join_timeout() cannot be used on this thread. */
    143         bool detached;
    144         /** Waitq for thread_join_timeout(). */
    145         waitq_t join_wq;
    146         /** Link used in the joiner_head list. */
    147         link_t joiner_link;
    148147
    149148#ifdef CONFIG_FPU
     
    219218extern void thread_interrupt(thread_t *, bool);
    220219
     220static inline thread_t *thread_ref(thread_t *thread)
     221{
     222        refcount_up(&thread->refcount);
     223        return thread;
     224}
     225
     226static inline thread_t *thread_try_ref(thread_t *thread)
     227{
     228        if (refcount_try_up(&thread->refcount))
     229                return thread;
     230        else
     231                return NULL;
     232}
     233
     234extern void thread_put(thread_t *);
     235
    221236#ifndef thread_create_arch
    222237extern errno_t thread_create_arch(thread_t *, thread_flags_t);
     
    236251extern errno_t thread_join(thread_t *);
    237252extern errno_t thread_join_timeout(thread_t *, uint32_t, unsigned int);
    238 extern void thread_detach(thread_t *);
    239253
    240254extern void thread_print_list(bool);
    241 extern void thread_destroy(thread_t *, bool);
    242255extern thread_t *thread_find_by_id(thread_id_t);
    243256extern size_t thread_count(void);
     
    245258extern thread_t *thread_next(thread_t *);
    246259extern void thread_update_accounting(bool);
    247 extern bool thread_exists(thread_t *);
     260extern thread_t *thread_try_get(thread_t *);
    248261
    249262extern void thread_migration_disable(void);
  • kernel/generic/include/synch/waitq.h

    rdaadfa6 r1871118  
    4343typedef enum {
    4444        WAKEUP_FIRST = 0,
    45         WAKEUP_ALL
     45        WAKEUP_ALL,
     46        WAKEUP_CLOSE,
    4647} wakeup_mode_t;
    4748
Note: See TracChangeset for help on using the changeset viewer.