==== Keeping pointer to current thread ==== http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/uts/intel/asm/thread.h -- thread pointer context: extern __inline__ struct _kthread *threadp(void) { void *__value; #if defined(__amd64) __asm__ __volatile__( "movq %%gs:0x18,%0" /* CPU_THREAD */ : "=r" (__value)); #elif defined(__i386) __asm__ __volatile__( "movl %%gs:0x10,%0" /* CPU_THREAD */ : "=r" (__value)); #else #error "port me" #endif return (__value); } (extern __inline__ explained: http://publib.boulder.ibm.com/infocenter/compbgpl/v9v111/index.jsp?topic=/com.ibm.xlcpp9.bg.doc/language_ref/cplr243.htm -- "If you specify the __inline__ keyword, with the trailing underscores, the compiler uses the GNU C semantics for inline functions. In contrast to the C99 semantics, a function defined as __inline__ provides an external definition only; a function defined as static __inline__ provides an inline definition with internal linkage (as in C99); and a function defined as extern __inline__, when compiled with optimization enabled, allows the co-existence of an inline and external definition of the same function. For more information on the GNU C implementation of inline functions, see the GCC documentation, available at http://gcc.gnu.org/onlinedocs/." Why all this? See http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/uts/intel/ia32/ml/i86_subr.s#2381 ) http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/uts/common/sys/thread.h#528 extern kthread_t *threadp(void); /* inline, returns thread pointer */ #define curthread (threadp()) /* current thread pointer */ #define curproc (ttoproc(curthread)) /* current process pointer */ #define curproj (ttoproj(curthread)) /* current project pointer */ #define curzone (curproc->p_zone) /* current zone pointer */ cf: in getpid() code: int64_t getpid(void) { rval_t r; proc_t *p; p = ttoproc(curthread); r.r_val1 = p->p_pid; if (p->p_flag & SZONETOP) r.r_val2 = curproc->p_zone->zone_zsched->p_pid; else r.r_val2 = p->p_ppid; return (r.r_vals); } extern struct _kthread t0; /* the scheduler thread */ extern kmutex_t pidlock; /* global process lock */ ==== Mutex Locks ==== http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/uts/intel/ia32/ml/lock_prim.s -- x86 implementation in ASM http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/uts/intel/sys/mutex_impl.h -- data strcutures to implement the spin/adaptive duality http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/uts/common/os/mutex.c -- theory behind the spin/adaptive locks explained See Chapter 17.5, especially 17.5.2 for detailed explanation: http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_6/CH06-2.html#HEADING2-286 -- CMPXCHG explained: cmpxchg operand1, operand2 if ({al/ax/eax} = operand1) then zero := 1 ;Set the zero flag operand1 := operand2 else zero := 0 ;Clear the zero flag {al/ax/eax} := operand1 endif ========================================================================== Scheduler visits: Linux: task_struct OpenSolaris: kthread_t Per process: proc_t, kthread_t, kwlp_t (see Fig. 2.3) Linux: lower priorities have precedence, 0 is most critical OpenSolaris: higher priorities have precedence, highest are most critical 0-59 Time Shared, Interactive, Fixed, Fair Share Scheduler 60-99 System Class 100-159 Real-Time (note real-time higher than system threads) 160-169 Low level Interrupts