#ifndef LITMUS_LOCKING_H #define LITMUS_LOCKING_H #include struct litmus_lock_ops; #ifdef CONFIG_LITMUS_NESTED_LOCKING struct nested_info { struct litmus_lock *lock; struct task_struct *hp_waiter_eff_prio; struct task_struct **hp_waiter_ptr; struct binheap_node hp_binheap_node; }; static inline struct task_struct* top_priority(struct binheap* handle) { if(!binheap_empty(handle)) { return (struct task_struct*)(binheap_top_entry(handle, struct nested_info, hp_binheap_node)->hp_waiter_eff_prio); } return NULL; } void print_hp_waiters(struct binheap_node* n, int depth); #endif #define LOCK_NAME_LEN 16 struct litmus_lock_proc_ops { void (*add)(struct litmus_lock *l); void (*remove)(struct litmus_lock *l); }; /* Generic base struct for LITMUS^RT userspace semaphores. * This structure should be embedded in protocol-specific semaphores. */ struct litmus_lock { struct litmus_lock_ops *ops; int type; int ident; #ifdef CONFIG_LITMUS_NESTED_LOCKING struct nested_info nest; //#ifdef CONFIG_DEBUG_SPINLOCK char cheat_lockdep[2]; struct lock_class_key key; //#endif #endif struct litmus_lock_proc_ops *proc; struct proc_dir_entry *proc_entry; char name[LOCK_NAME_LEN]; }; #ifdef CONFIG_LITMUS_DGL_SUPPORT #define MAX_DGL_SIZE CONFIG_LITMUS_MAX_DGL_SIZE typedef struct dgl_wait_state { struct task_struct *task; /* task waiting on DGL */ struct litmus_lock *locks[MAX_DGL_SIZE]; /* requested locks in DGL */ int size; /* size of the DGL */ int nr_remaining; /* nr locks remainging before DGL is complete */ int last_primary; /* index lock in locks[] that has active priority */ wait_queue_t wq_nodes[MAX_DGL_SIZE]; } dgl_wait_state_t; void wake_or_wait_on_next_lock(dgl_wait_state_t *dgl_wait); void select_next_lock(dgl_wait_state_t* dgl_wait /*, struct litmus_lock* prev_lock*/); void init_dgl_waitqueue_entry(wait_queue_t *wq_node, dgl_wait_state_t* dgl_wait); int dgl_wake_up(wait_queue_t *wq_node, unsigned mode, int sync, void *key); void __waitqueue_dgl_remove_first(wait_queue_head_t *wq, dgl_wait_state_t** dgl_wait, struct task_struct **task); #endif typedef int (*lock_op_t)(struct litmus_lock *l); typedef lock_op_t lock_close_t; typedef lock_op_t lock_lock_t; typedef lock_op_t lock_unlock_t; typedef int (*lock_open_t)(struct litmus_lock *l, void* __user arg); typedef void (*lock_free_t)(struct litmus_lock *l); struct litmus_lock_ops { /* Current task tries to obtain / drop a reference to a lock. * Optional methods, allowed by default. */ lock_open_t open; lock_close_t close; /* Current tries to lock/unlock this lock (mandatory methods). */ lock_lock_t lock; lock_unlock_t unlock; /* The lock is no longer being referenced (mandatory method). */ lock_free_t deallocate; #ifdef CONFIG_LITMUS_NESTED_LOCKING void (*propagate_increase_inheritance)(struct litmus_lock* l, struct task_struct* t, raw_spinlock_t* to_unlock, unsigned long irqflags); void (*propagate_decrease_inheritance)(struct litmus_lock* l, struct task_struct* t, raw_spinlock_t* to_unlock, unsigned long irqflags); #endif #ifdef CONFIG_LITMUS_DGL_SUPPORT raw_spinlock_t* (*get_dgl_spin_lock)(struct litmus_lock *l); int (*dgl_lock)(struct litmus_lock *l, dgl_wait_state_t* dgl_wait, wait_queue_t* wq_node); int (*is_owner)(struct litmus_lock *l, struct task_struct *t); void (*enable_priority)(struct litmus_lock *l, dgl_wait_state_t* dgl_wait); #endif }; /* Nested inheritance can be achieved with fine-grain locking when there is no need for DGL support, presuming locks are acquired in a partial order (no cycles!). However, DGLs allow locks to be acquired in any order. This makes nested inheritance very difficult (we don't yet know a solution) to realize with fine-grain locks, so we use a big lock instead. Code contains both fine-grain and coarse-grain methods together, side-by-side. Each lock operation *IS NOT* surrounded by ifdef/endif to help make code more readable. However, this leads to the odd situation where both code paths appear together in code as if they were both active together. THIS IS NOT REALLY THE CASE! ONLY ONE CODE PATH IS ACTUALLY ACTIVE! Example: lock_global_irqsave(coarseLock, flags); lock_fine_irqsave(fineLock, flags); Reality (coarse): lock_global_irqsave(coarseLock, flags); //lock_fine_irqsave(fineLock, flags); Reality (fine): //lock_global_irqsave(coarseLock, flags); lock_fine_irqsave(fineLock, flags); Be careful when you read code involving nested inheritance. */ #if defined(CONFIG_LITMUS_DGL_SUPPORT) /* DGL requires a big lock to implement nested inheritance */ #define lock_global_irqsave(lock, flags) raw_spin_lock_irqsave((lock), (flags)) #define lock_global(lock) raw_spin_lock((lock)) #define trylock_global_irqsave(lock, flags) raw_spin_trylock_irqsave((lock), (flags)) #define trylock_global(lock) raw_spin_trylock((lock)) #define unlock_global_irqrestore(lock, flags) raw_spin_unlock_irqrestore((lock), (flags)) #define unlock_global(lock) raw_spin_unlock((lock)) /* fine-grain locking are no-ops with DGL support */ #define lock_fine_irqsave(lock, flags) #define lock_fine(lock) #define trylock_fine_irqsave(lock, flags) #define trylock_fine(lock) #define unlock_fine_irqrestore(lock, flags) #define unlock_fine(lock) #elif defined(CONFIG_LITMUS_NESTED_LOCKING) /* Use fine-grain locking when DGLs are disabled. */ /* global locking are no-ops without DGL support */ #define lock_global_irqsave(lock, flags) #define lock_global(lock) #define trylock_global_irqsave(lock, flags) #define trylock_global(lock) #define unlock_global_irqrestore(lock, flags) #define unlock_global(lock) #define lock_fine_irqsave(lock, flags) raw_spin_lock_irqsave((lock), (flags)) #define lock_fine(lock) raw_spin_lock((lock)) #define trylock_fine_irqsave(lock, flags) raw_spin_trylock_irqsave((lock), (flags)) #define trylock_fine(lock) raw_spin_trylock((lock)) #define unlock_fine_irqrestore(lock, flags) raw_spin_unlock_irqrestore((lock), (flags)) #define unlock_fine(lock) raw_spin_unlock((lock)) #endif void suspend_for_lock(void); #endif