Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/include/proc/thread.h

    rdf58e44 r7faabb7  
    4040#include <time/timeout.h>
    4141#include <cpu.h>
     42#include <synch/rwlock.h>
    4243#include <synch/spinlock.h>
    4344#include <adt/avl.h>
     
    4748#include <proc/uarg.h>
    4849#include <udebug/udebug.h>
    49 #include <sysinfo/abi.h>
    50 
    51 #define THREAD_STACK_SIZE   STACK_SIZE
    52 #define THREAD_NAME_BUFLEN  20
    53 
    54 extern const char *thread_states[];
     50
     51#define THREAD_STACK_SIZE       STACK_SIZE
     52#define THREAD_NAME_BUFLEN      20
     53
     54extern char *thread_states[];
    5555
    5656/* Thread flags */
     
    6060 * When using this flag, the caller must set cpu in the thread_t
    6161 * structure manually before calling thread_ready (even on uniprocessor).
    62  *
    63  */
    64 #define THREAD_FLAG_WIRED  (1 << 0)
    65 
     62 */
     63#define THREAD_FLAG_WIRED       (1 << 0)
    6664/** Thread was migrated to another CPU and has not run yet. */
    67 #define THREAD_FLAG_STOLEN  (1 << 1)
    68 
     65#define THREAD_FLAG_STOLEN      (1 << 1)
    6966/** Thread executes in userspace. */
    70 #define THREAD_FLAG_USPACE  (1 << 2)
    71 
     67#define THREAD_FLAG_USPACE      (1 << 2)
    7268/** Thread will be attached by the caller. */
    73 #define THREAD_FLAG_NOATTACH  (1 << 3)
     69#define THREAD_FLAG_NOATTACH    (1 << 3)
     70
     71/** Thread states. */
     72typedef enum {
     73        /** It is an error, if thread is found in this state. */
     74        Invalid,
     75        /** State of a thread that is currently executing on some CPU. */
     76        Running,
     77        /** Thread in this state is waiting for an event. */
     78        Sleeping,
     79        /** State of threads in a run queue. */
     80        Ready,
     81        /** Threads are in this state before they are first readied. */
     82        Entering,
     83        /** After a thread calls thread_exit(), it is put into Exiting state. */
     84        Exiting,
     85        /** Threads that were not detached but exited are Lingering. */
     86        Lingering
     87} state_t;
    7488
    7589/** Thread structure. There is one per thread. */
    7690typedef struct thread {
    77         link_t rq_link;  /**< Run queue link. */
    78         link_t wq_link;  /**< Wait queue link. */
    79         link_t th_link;  /**< Links to threads within containing task. */
    80        
     91        link_t rq_link;         /**< Run queue link. */
     92        link_t wq_link;         /**< Wait queue link. */
     93        link_t th_link;         /**< Links to threads within containing task. */
     94
    8195        /** Threads linkage to the threads_tree. */
    8296        avltree_node_t threads_tree_node;
     
    86100         * Protects the whole thread structure except list links above.
    87101         */
    88         IRQ_SPINLOCK_DECLARE(lock);
    89        
     102        SPINLOCK_DECLARE(lock);
     103
    90104        char name[THREAD_NAME_BUFLEN];
    91        
     105
    92106        /** Function implementing the thread. */
    93         void (*thread_code)(void *);
     107        void (* thread_code)(void *);
    94108        /** Argument passed to thread_code() function. */
    95109        void *thread_arg;
    96        
    97         /**
    98          * From here, the stored context is restored
    99          * when the thread is scheduled.
     110
     111        /**
     112         * From here, the stored context is restored when the thread is
     113         * scheduled.
    100114         */
    101115        context_t saved_context;
    102        
    103         /**
    104          * From here, the stored timeout context
    105          * is restored when sleep times out.
     116        /**
     117         * From here, the stored timeout context is restored when sleep times
     118         * out.
    106119         */
    107120        context_t sleep_timeout_context;
    108        
    109         /**
    110          * From here, the stored interruption context
    111          * is restored when sleep is interrupted.
     121        /**
     122         * From here, the stored interruption context is restored when sleep is
     123         * interrupted.
    112124         */
    113125        context_t sleep_interruption_context;
    114        
     126
    115127        /** If true, the thread can be interrupted from sleep. */
    116128        bool sleep_interruptible;
     
    120132        timeout_t sleep_timeout;
    121133        /** Flag signalling sleep timeout in progress. */
    122         volatile bool timeout_pending;
    123        
     134        volatile int timeout_pending;
     135
    124136        /**
    125137         * True if this thread is executing copy_from_uspace().
     
    127139         */
    128140        bool in_copy_from_uspace;
    129        
    130141        /**
    131142         * True if this thread is executing copy_to_uspace().
     
    138149         * thread_exit() before returning to userspace.
    139150         */
    140         bool interrupted;
    141        
    142         /**
    143          * If true, the scheduler will print a stack trace
    144          * to the kernel console upon scheduling this thread.
    145          */
    146         bool btrace;
     151        bool interrupted;                       
    147152       
    148153        /** If true, thread_join_timeout() cannot be used on this thread. */
     
    152157        /** Link used in the joiner_head list. */
    153158        link_t joiner_link;
    154        
     159
    155160        fpu_context_t *saved_fpu_context;
    156161        int fpu_context_exists;
    157        
     162
    158163        /*
    159164         * Defined only if thread doesn't run.
     
    162167         */
    163168        int fpu_context_engaged;
    164        
     169
     170        rwlock_type_t rwlock_holder_type;
     171
     172        /** Callback fired in scheduler before the thread is put asleep. */
     173        void (* call_me)(void *);
     174        /** Argument passed to call_me(). */
     175        void *call_me_with;
     176
    165177        /** Thread's state. */
    166178        state_t state;
    167179        /** Thread's flags. */
    168         unsigned int flags;
     180        int flags;
    169181       
    170182        /** Thread's CPU. */
     
    172184        /** Containing task. */
    173185        task_t *task;
    174        
     186
    175187        /** Ticks before preemption. */
    176188        uint64_t ticks;
    177189       
    178190        /** Thread accounting. */
    179         uint64_t ucycles;
    180         uint64_t kcycles;
     191        uint64_t cycles;
    181192        /** Last sampled cycle. */
    182193        uint64_t last_cycle;
    183         /** Thread doesn't affect accumulated accounting. */
     194        /** Thread doesn't affect accumulated accounting. */   
    184195        bool uncounted;
    185        
     196
    186197        /** Thread's priority. Implemented as index to CPU->rq */
    187198        int priority;
     
    191202        /** Architecture-specific data. */
    192203        thread_arch_t arch;
    193        
     204
    194205        /** Thread's kernel stack. */
    195206        uint8_t *kstack;
    196        
     207
    197208#ifdef CONFIG_UDEBUG
    198209        /** Debugging stuff */
    199210        udebug_thread_t udebug;
    200 #endif /* CONFIG_UDEBUG */
     211#endif
     212
    201213} thread_t;
    202214
     
    207219 *
    208220 */
    209 IRQ_SPINLOCK_EXTERN(threads_lock);
     221SPINLOCK_EXTERN(threads_lock);
    210222
    211223/** AVL tree containing all threads. */
     
    213225
    214226extern void thread_init(void);
    215 extern thread_t *thread_create(void (*)(void *), void *, task_t *,
    216     unsigned int, const char *, bool);
    217 extern void thread_attach(thread_t *, task_t *);
    218 extern void thread_ready(thread_t *);
     227extern thread_t *thread_create(void (* func)(void *), void *arg, task_t *task,
     228    int flags, char *name, bool uncounted);
     229extern void thread_attach(thread_t *t, task_t *task);
     230extern void thread_ready(thread_t *t);
    219231extern void thread_exit(void) __attribute__((noreturn));
    220232
    221233#ifndef thread_create_arch
    222 extern void thread_create_arch(thread_t *);
    223 #endif
    224 
     234extern void thread_create_arch(thread_t *t);
     235#endif
    225236#ifndef thr_constructor_arch
    226 extern void thr_constructor_arch(thread_t *);
    227 #endif
    228 
     237extern void thr_constructor_arch(thread_t *t);
     238#endif
    229239#ifndef thr_destructor_arch
    230 extern void thr_destructor_arch(thread_t *);
    231 #endif
    232 
    233 extern void thread_sleep(uint32_t);
    234 extern void thread_usleep(uint32_t);
     240extern void thr_destructor_arch(thread_t *t);
     241#endif
     242
     243extern void thread_sleep(uint32_t sec);
     244extern void thread_usleep(uint32_t usec);
    235245
    236246#define thread_join(t) \
    237247        thread_join_timeout((t), SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE)
    238 
    239 extern int thread_join_timeout(thread_t *, uint32_t, unsigned int);
    240 extern void thread_detach(thread_t *);
    241 
    242 extern void thread_print_list(bool);
    243 extern void thread_destroy(thread_t *, bool);
    244 extern thread_t *thread_find_by_id(thread_id_t);
    245 extern void thread_update_accounting(bool);
    246 extern bool thread_exists(thread_t *);
    247 extern void thread_stack_trace(thread_id_t);
     248extern int thread_join_timeout(thread_t *t, uint32_t usec, int flags);
     249extern void thread_detach(thread_t *t);
     250
     251extern void thread_register_call_me(void (* call_me)(void *),
     252    void *call_me_with);
     253extern void thread_print_list(void);
     254extern void thread_destroy(thread_t *t);
     255extern void thread_update_accounting(void);
     256extern bool thread_exists(thread_t *t);
    248257
    249258/** Fpu context slab cache. */
     
    251260
    252261/* Thread syscall prototypes. */
    253 extern sysarg_t sys_thread_create(uspace_arg_t *, char *, size_t,
    254     thread_id_t *);
    255 extern sysarg_t sys_thread_exit(int);
    256 extern sysarg_t sys_thread_get_id(thread_id_t *);
    257 extern sysarg_t sys_thread_usleep(uint32_t);
     262extern unative_t sys_thread_create(uspace_arg_t *uspace_uarg,
     263    char *uspace_name, size_t name_len, thread_id_t *uspace_thread_id);
     264extern unative_t sys_thread_exit(int uspace_status);
     265extern unative_t sys_thread_get_id(thread_id_t *uspace_thread_id);
    258266
    259267#endif
Note: See TracChangeset for help on using the changeset viewer.