| 1 | 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. |
| 2 | |
| 3 | [[PageOutline]] |
| 4 | |
| 5 | == A) General principles == |
| 6 | |
| 7 | 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: |
| 8 | * 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. |
| 9 | * 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 |
| 10 | devices, distributed on all clusters, or the kernel distributed Virtual File System, that is also distributed on all clusters. |
| 11 | |
| 12 | |
| 13 | ALMOS-MKH defines three types of locks to implement exclusive access to these shared structures: ''busylocks'', ''queuelocks'', and ''rwlocks''. |
| 14 | |
| 15 | == B) busylocks & remote_busylocks == |
| 16 | |
| 17 | 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. |
| 18 | |
| 19 | 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. |
| 20 | The scheduler makes a kernel panic if the current thread busylocks counter is not nul when it executes the ''sched_yield()'' function. |
| 21 | |
| 22 | 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. |
| 23 | |
| 24 | == C) queuelock & remote_queuelock == |
| 25 | |
| 26 | 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. |
| 27 | |
| 28 | 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 |
| 29 | queue lock state. |
| 30 | |
| 31 | A thread holding a queuelock can deschedule, and no special checking is done by the scheduler. |
| 32 | |
| 33 | == D) rwlocks & remote_rwlocks == |
| 34 | |
| 35 | The '''rwlock''' (local) and '''remote_rwlock''' (global) support several simultaneous read accesses, but only one write access to a given shared object. |
| 36 | As for queue locks, both readers and writers take the associated busylock before accessing or updating the rwlock state, and releases the busylock after |
| 37 | rwlock state update. |
| 38 | * 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. |
| 39 | * 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. |
| 40 | * 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. |
| 41 | * 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. |
| 42 | |
| 43 | == E) Locks debug infra-structure == |
| 44 | |
| 45 | |
| 46 | |