Annotation of kernel/kern/thread.h, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
                      3:  *
                      4:  * @APPLE_LICENSE_HEADER_START@
                      5:  * 
                      6:  * Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
                      7:  * Reserved.  This file contains Original Code and/or Modifications of
                      8:  * Original Code as defined in and that are subject to the Apple Public
                      9:  * Source License Version 1.1 (the "License").  You may not use this file
                     10:  * except in compliance with the License.  Please obtain a copy of the
                     11:  * License at http://www.apple.com/publicsource and read it before using
                     12:  * this file.
                     13:  * 
                     14:  * The Original Code and all software distributed under the License are
                     15:  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
                     16:  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
                     17:  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
                     18:  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
                     19:  * License for the specific language governing rights and limitations
                     20:  * under the License.
                     21:  * 
                     22:  * @APPLE_LICENSE_HEADER_END@
                     23:  */
                     24: 
                     25: /* 
                     26:  * Mach Operating System
                     27:  * Copyright (c) 1993-1987 Carnegie Mellon University
                     28:  * All Rights Reserved.
                     29:  * 
                     30:  * Permission to use, copy, modify and distribute this software and its
                     31:  * documentation is hereby granted, provided that both the copyright
                     32:  * notice and this permission notice appear in all copies of the
                     33:  * software, derivative works or modified versions, and any portions
                     34:  * thereof, and that both notices appear in supporting documentation.
                     35:  * 
                     36:  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
                     37:  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
                     38:  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
                     39:  * 
                     40:  * Carnegie Mellon requests users of this software to return to
                     41:  * 
                     42:  *  Software Distribution Coordinator  or  [email protected]
                     43:  *  School of Computer Science
                     44:  *  Carnegie Mellon University
                     45:  *  Pittsburgh PA 15213-3890
                     46:  * 
                     47:  * any improvements or extensions that they make and grant Carnegie Mellon
                     48:  * the rights to redistribute these changes.
                     49:  */
                     50: /*
                     51:  *     File:   thread.h
                     52:  *     Author: Avadis Tevanian, Jr.
                     53:  *
                     54:  *     This file contains the structure definitions for threads.
                     55:  *
                     56:  */
                     57: 
                     58: #ifndef        _KERN_THREAD_H_
                     59: #define _KERN_THREAD_H_
                     60: 
                     61: #import <mach/features.h>
                     62: 
                     63: #include <mach/boolean.h>
                     64: #include <mach/thread_info.h>
                     65: #include <mach/thread_status.h>
                     66: #include <mach/machine/vm_types.h>
                     67: #include <mach/message.h>
                     68: #include <mach/port.h>
                     69: #include <mach/vm_prot.h>
                     70: #include <kern/ast.h>
                     71: #include <kern/cpu_number.h>
                     72: #include <kern/queue.h>
                     73: #include <kern/processor.h>
                     74: #include <kern/sched_prim.h>   /* event_t, continuation_t */
                     75: #include <kern/time_out.h>
                     76: #include <kern/timer.h>
                     77: #include <kern/lock.h>
                     78: #include <kern/sched.h>
                     79: #include <kern/task.h>         /* for current_space(), current_map() */
                     80: #include <machine/thread.h>
                     81: #if defined(_KERNEL)
                     82: #include <ipc/ipc_kmsg_queue.h>
                     83: #endif /* _KERNEL */
                     84: 
                     85: struct thread {
                     86:        /* Run queues */
                     87:        queue_chain_t   links;          /* current run queue links */
                     88:        run_queue_t     runq;           /* run queue p is on SEE BELOW */
                     89: /*
                     90:  *     NOTE:   The runq field in the thread structure has an unusual
                     91:  *     locking protocol.  If its value is RUN_QUEUE_NULL, then it is
                     92:  *     locked by the thread_lock, but if its value is something else
                     93:  *     (i.e. a run_queue) then it is locked by that run_queue's lock.
                     94:  */
                     95: 
                     96:        /* Task information */
                     97:        task_t          task;           /* Task to which I belong */
                     98:        queue_chain_t   thread_list;    /* list of threads in task */
                     99: 
                    100:        /* Thread bookkeeping */
                    101:        queue_chain_t   pset_threads;   /* list of all threads in proc set*/
                    102: 
                    103:        /* Self-preservation */
                    104:        decl_simple_lock_data(,lock)
                    105:        int             ref_count;      /* number of references to me */
                    106: 
                    107:        /* Hardware state */
                    108:        struct pcb      *pcb;           /* hardware pcb & machine state */
                    109:        vm_offset_t     kernel_stack;   /* accurate only if the thread is
                    110:                                           not swapped and not executing */
                    111:        vm_offset_t     stack_privilege;/* reserved kernel stack */
                    112: 
                    113:        /* Swapping information */
                    114:        void            (*swap_func)(); /* start here after swapin */
                    115: 
                    116:        /* Exception return continuation */
                    117:        void            (*exc_func)(void);
                    118: 
                    119:        /* Blocking information */
                    120:        event_t         wait_event;     /* event we are waiting on */
                    121:        int             suspend_count;  /* internal use only */
                    122:        kern_return_t   wait_result;    /* outcome of wait -
                    123:                                           may be examined by this thread
                    124:                                           WITHOUT locking */
                    125:        boolean_t       wake_active;    /* someone is waiting for this
                    126:                                           thread to become suspended */
                    127:        int             state;          /* Thread state: */
                    128:        char            *wait_mesg;     /* wait mesg */
                    129: /*
                    130:  *     Thread states [bits or'ed]
                    131:  */
                    132: #define TH_WAIT                        0x01    /* thread is queued for waiting */
                    133: #define TH_SUSP                        0x02    /* thread has been asked to stop */
                    134: #define TH_RUN                 0x04    /* thread is running or on runq */
                    135: #define TH_UNINT               0x08    /* thread is waiting uninteruptibly */
                    136: #define        TH_HALTED               0x10    /* thread is halted at clean point ? */
                    137: 
                    138: #define TH_IDLE                        0x80    /* thread is an idle thread */
                    139: 
                    140: #define        TH_SCHED_STATE  (TH_WAIT|TH_SUSP|TH_RUN|TH_UNINT)
                    141: 
                    142: #define        TH_SWAPPED              0x0100  /* thread has no kernel stack */
                    143: #define        TH_SW_COMING_IN         0x0200  /* thread is waiting for kernel stack */
                    144: 
                    145: #define        TH_SWAP_STATE   (TH_SWAPPED | TH_SW_COMING_IN)
                    146: 
                    147:        /* Scheduling information */
                    148:        int             priority;       /* thread's priority */
                    149:        int             max_priority;   /* maximum priority */
                    150:        int             sched_pri;      /* scheduled (computed) priority */
                    151: #if    MACH_FIXPRI
                    152:        int             sched_data;     /* for use by policy */
                    153:        int             policy;         /* scheduling policy */
                    154: #endif /* MACH_FIXPRI */
                    155:        int             depress_priority; /* depressed from this priority */
                    156:        unsigned int    cpu_usage;      /* exp. decaying cpu usage [%cpu] */
                    157:        unsigned int    sched_usage;    /* load-weighted cpu usage [sched] */
                    158:        unsigned int    sched_stamp;    /* last time priority was updated */
                    159:        unsigned int    sleep_time;     /* number of scheduler ticks thread
                    160:                                           has been sleeping, returnable
                    161:                                           via thread_info() */
                    162: 
                    163:        /* VM global variables */
                    164: 
                    165:        vm_offset_t     recover;        /* page fault recovery (copyin/out) */
                    166:        boolean_t       vm_privilege;   /* Can use reserved memory? */
                    167: 
                    168:        /* Compatibility garbage */
                    169:        struct uthread  *_uthread;
                    170:        int             unix_lock;      /* bind to unix_master */
                    171: 
                    172:        /* User-visible scheduling state */
                    173:        int             user_stop_count;        /* outstanding stops */
                    174: 
                    175:        /* IPC data structures */
                    176:        struct thread *ith_next, *ith_prev;
                    177:        mach_msg_return_t ith_state;
                    178:        mach_msg_option_t ith_rcv_option;
                    179:        struct ipc_kmsg *ith_list;
                    180:        union {
                    181:                mach_msg_size_t msize;          /* max size for recvd msg */
                    182:                struct ipc_kmsg *kmsg;          /* received message */
                    183:        } data;
                    184:        mach_port_seqno_t ith_seqno;            /* seqno of recvd message */
                    185: 
                    186:        struct ipc_kmsg_queue ith_messages; 
                    187: 
                    188:        decl_simple_lock_data(, ith_lock_data)
                    189:        struct ipc_port *ith_self;      /* not a right, doesn't hold ref */
                    190:        struct ipc_port *ith_sself;     /* a send right */
                    191:        struct ipc_port *ith_exception; /* a send right */
                    192: #if    MACH_IPC_COMPAT
                    193:        struct ipc_port *ith_reply;     /* a send right */
                    194: #endif /* MACH_IPC_COMPAT */
                    195: 
                    196:        mach_port_t ith_mig_reply;      /* reply port for mig */
                    197:        struct ipc_port *ith_rpc_reply; /* reply port for kernel RPCs */
                    198: 
                    199:        /* State saved when thread's stack is discarded */
                    200:        union {
                    201:                struct {
                    202:                        mach_msg_header_t *msg;
                    203:                        mach_msg_option_t option;
                    204:                        mach_msg_size_t rcv_size;
                    205:                        mach_msg_timeout_t timeout;
                    206:                        mach_port_t notify;
                    207:                        struct ipc_object *object;
                    208:                        struct ipc_mqueue *mqueue;
                    209:                } receive;
                    210:                struct {
                    211:                        struct ipc_port *port;
                    212:                        int exc;
                    213:                        int code;
                    214:                        int subcode;
                    215:                } exception;
                    216:                void *other;            /* catch-all for other state */
                    217:        } saved;
                    218: 
                    219:        /* Timing data structures */
                    220:        timer_data_t    user_timer;     /* user mode timer */
                    221:        timer_data_t    system_timer;   /* system mode timer */
                    222:        timer_save_data_t user_timer_save;  /* saved user timer value */
                    223:        timer_save_data_t system_timer_save;  /* saved sys timer val. */
                    224:        unsigned int    cpu_delta;      /* cpu usage since last update */
                    225:        unsigned int    sched_delta;    /* weighted cpu usage since update */
                    226: 
                    227:        /* Time-outs */
                    228:        timer_elt_data_t timer;         /* timer for thread */
                    229:        timer_elt_data_t depress_timer; /* timer for priority depression */
                    230: 
                    231:        /* Ast/Halt data structures */
                    232:        boolean_t       active;         /* how alive is the thread */
                    233:        int             ast;            /* ast's needed.  See ast.h */
                    234: 
                    235:        /* Processor data structures */
                    236:        processor_set_t processor_set;  /* assigned processor set */
                    237:        processor_t     bound_processor;        /* bound to processor ?*/
                    238: #if    MACH_HOST
                    239:        boolean_t       may_assign;     /* may assignment change? */
                    240:        boolean_t       assign_active;  /* someone waiting for may_assign */
                    241: #endif /* MACH_HOST */
                    242: 
                    243: #if    NCPUS > 1
                    244:        processor_t     last_processor; /* processor this last ran on */
                    245: #endif /* NCPUS > 1 */
                    246: 
                    247:        boolean_t       allocInProgress;/* thread is allocating memory */
                    248: };
                    249: 
                    250: #define        ith_msize       data.msize
                    251: #define        ith_kmsg        data.kmsg
                    252: #define        ith_wait_result wait_result
                    253: 
                    254: #define        ith_msg         saved.receive.msg
                    255: #define        ith_option      saved.receive.option
                    256: #define ith_rcv_size   saved.receive.rcv_size
                    257: #define ith_timeout    saved.receive.timeout
                    258: #define ith_notify     saved.receive.notify
                    259: #define ith_object     saved.receive.object
                    260: #define ith_mqueue     saved.receive.mqueue
                    261: 
                    262: #define        ith_port        saved.exception.port
                    263: #define ith_exc                saved.exception.exc
                    264: #define ith_exc_code   saved.exception.code
                    265: #define ith_exc_subcode        saved.exception.subcode
                    266: 
                    267: #define ith_other      saved.other
                    268: 
                    269: #ifndef        _KERN_KERN_TYPES_H_
                    270: typedef struct thread *thread_t;
                    271: 
                    272: #define THREAD_NULL    ((thread_t) 0)
                    273: 
                    274: typedef        mach_port_t *thread_array_t;
                    275: #endif /* _KERN_KERN_TYPES_H_ */
                    276: 
                    277: 
                    278: extern thread_t                active_threads[NCPUS];  /* active threads */
                    279: extern vm_offset_t     active_stacks[NCPUS];   /* active kernel stacks */
                    280: 
                    281: /*
                    282:  *     User routines
                    283:  */
                    284: 
                    285: extern kern_return_t   thread_create();
                    286: extern kern_return_t   thread_terminate();
                    287: extern kern_return_t   thread_suspend();
                    288: extern kern_return_t   thread_resume();
                    289: extern kern_return_t   thread_abort();
                    290: extern kern_return_t   thread_get_state();
                    291: extern kern_return_t   thread_set_state();
                    292: extern kern_return_t   thread_get_special_port();
                    293: extern kern_return_t   thread_set_special_port();
                    294: extern kern_return_t   thread_info();
                    295: extern kern_return_t   thread_assign();
                    296: extern kern_return_t   thread_assign_default();
                    297: 
                    298: /*
                    299:  *     Kernel-only routines
                    300:  */
                    301: 
                    302: extern void            thread_init();
                    303: extern void            thread_reference();
                    304: extern void            thread_deallocate();
                    305: extern void            thread_hold();
                    306: extern kern_return_t   thread_dowait();
                    307: extern void            thread_release();
                    308: extern void            thread_swappable();
                    309: extern kern_return_t   thread_halt();
                    310: extern void            thread_halt_self();
                    311: extern thread_t                kernel_thread();
                    312: extern void            kernel_thread_noblock();
                    313: 
                    314: extern void            reaper_thread();
                    315: 
                    316: #if    MACH_HOST
                    317: extern void            thread_freeze();
                    318: extern void            thread_doassign();
                    319: extern void            thread_unfreeze();
                    320: #endif /* MACH_HOST */
                    321: 
                    322: /*
                    323:  *     Macro-defined routines
                    324:  */
                    325: 
                    326: #define thread_pcb(th)         ((th)->pcb)
                    327: 
                    328: #define thread_lock(th)                simple_lock(&(th)->lock)
                    329: #define thread_unlock(th)      simple_unlock(&(th)->lock)
                    330: 
                    331: #define thread_lock_try(th)    simple_lock_try(&(th)->lock)
                    332: 
                    333: #define thread_should_halt(thread)     \
                    334:                ((thread)->ast & (AST_HALT|AST_TERMINATE))
                    335: 
                    336: #define ipc_thread_lock(th)    simple_lock(&(th)->ipc_state_lock)
                    337: #define ipc_thread_unlock(th)  simple_unlock(&(th)->ipc_state_lock)
                    338: 
                    339: 
                    340: /*
                    341:  *     Machine specific implementations of the current thread macro
                    342:  *     designate this by defining CURRENT_THREAD.
                    343:  */
                    344: #ifndef        CURRENT_THREAD
                    345: #define current_thread()       (active_threads[cpu_number()])
                    346: #endif /* CURRENT_THREAD */
                    347: 
                    348: #define        current_stack()         (active_stacks[cpu_number()])
                    349: 
                    350: #define        current_task()          (current_thread()->task)
                    351: #define        current_space()         (current_task()->itk_space)
                    352: #define        current_map()           (current_task()->map)
                    353: 
                    354: #endif /* _KERN_THREAD_H_ */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.