This section describes the synchronisation primitives used by ALMO-MKH, namely the barriers used during the parallel kernel initialization, and the locks used to protect concurrent access to the shared kernel data structures. [[PageOutline]] == A) General principles == Most kernel data structures are shared: they can be concurrently accessed by several threads. These threads can be specialized kernel threads , such as the DEV threads or the RPC threads, or can be user threads, running in kernel mode after a syscall. There exist actually two levels of sharing: * some structures are locally shared: they can only be accessed by threads running in the cluster containing the shared structure. Examples are (i) the Schedulers (associated to the cores in a given cluster), or the Physical Pages Manager (PPM) allocator in a given cluster, or the Virtual Memory Manager (associated to a given process descriptor in a given cluster. * some structures are globally shared: they can be concurrently by any thread running in any cluster. Examples are the waiting queues associated to the Chdevs (channel devices, distributed on all clusters, or the kernel distributed Virtual File System, that is also distributed on all clusters. ALMOS-MKH defines three types of locks to implement exclusive access to these shared structures: ''busylocks'', ''queuelocks'', and ''rwlocks''. == B) busylocks & remote_busylocks == The '''busylock''' (local) and '''remote_busylock''' (global) are low-level locks implementing a busy-waiting policy for the calling threads. If the lock is already taken by another thread, the calling thread keep polling the lock until success. They are used to protect exclusive higher level synchronisation primitives (such as the ''queuelocks'' or ''rwlocks'' described below) , or ''simple'' data-structure where the access time is small and can be bounded. A thread holding a busy lock cannot reschedule. To enforce this rule, the ''busylock_acquire()'' function enter' a critical section before taking the lock, and saves the SR value in the busy lock descriptor. The thread holding the busy lock exit the critical section when it calls the ''busylock_release()'' function that releases the lock and restores the SR state. Each time a thread acquire a busy lock, it increments a busylocks counter in the thread descriptor, and decrements is when it releases the lock. The scheduler makes a kernel panic if the current thread busylocks counter is not nul when it executes the ''sched_yield()'' function. To avoid starvation, the ''busylock_acquire()'' function uses a ticket policy: the calling thread makes an atomic increment on a "ticket" allocator in lock descriptor, and keep polling the "current" value until current == ticket. To release the lock, the ''busylock_release()'' function increments the "current" value in lock descriptor. == C) queuelock & remote_queuelock == The '''queuelock''' (local) and '''remote_queuelock''' (global) are higher level locks implementing a descheduling policy, with registration in a waiting queue. If the lock is already taken by another thread, the calling thread register in a (local or trans-cluster) waiting queue rooted in the queuelock, and deschedules. The first thread T registered in the waiting thread is re-activated by the thread T' holding the lock when T' release the lock. It is used to protect complex structures, where the access can require to get exclusive access to one (or more) other shared resources. The queue lock descriptor itself contains a busylock, that is used by the ''queuelock_acquire()'' and ''queue lock_release()'' functions to protect exclusive access to the queue lock state. A thread holding a queuelock can deschedule, and no special checking is done by the scheduler. == D) rwlocks & remote_rwlocks == The '''rwlock''' (local) and '''remote_rwlock''' (global) support several simultaneous read accesses, but only one write access to a given shared object. As for queue locks, both readers and writers take the associated busylock before accessing or updating the rwlock state, and releases the busylock after rwlock state update. * when a reader try to access the object, it increments the readers "count" when the lock is not "taken" by a writer. It registers in the "rd_root" waiting queue, blocks, and deschedules when the lock is taken. * when a writer try to take the rwlock, it check the "taken" field. If the lock is already taken, or if the number of readers is non zero, it registers in the "wr_root" waiting queue, blocks, and deschedules. It set "taken" otherwise. * when a reader completes its access, it decrement the readers "count", unblock the the first waiting writer if there is no other readers, and unblock all waiting readers if there no write request. * when a writer completes its access, it reset the "taken" field, releases the first waiting writer if queue non empty, or releases all waiting readers if no writer. == E) Locks debug infra-structure ==