The only reason for concurrent execution control on Linux devices is hardware interruption. Kernel code can be preemptible. What is competition? : When two lines are executed [have the opportunity to operate on the same data structure (or hardware resources), the possibility of mixing will always exist. How to avoid competition: When you set up... information & n
The only reason for concurrent execution control on Linux devices is hardware interruption. Kernel code can be preemptible. What is competition? : When two lines are executed [have the opportunity to operate on the same data structure (or hardware resources), the possibility of mixing will always exist. How to avoid competition: remember to avoid sharing resources whenever possible when designing the driver. without concurrent access, there will be no competition. therefore, the kernel code carefully written should have the smallest share. the most obvious application of this idea is to avoid using global variables. if you place a resource on multiple execution lines to find it, there should be a strong reason to do so.
Global variables are not the only way to share resources. Passing a pointer to other code in the kernel creates a new form of sharing. A common technique of access management is locking or mutex, ensuring that only one execution thread can operate on one shared resource at any time. Www.2cto.com when the kernel code creates an object that will be shared by other parts of the kernel, this object must always exist (and function properly) until it knows that there is no external reference to it. Critical section: only one thread can execute code at any given time.
Flag (semaphore): is an integer value, combined with a pair of functions, a process like entering the critical section will call the corresponding flag P, if the flag value is greater than 0, this value will be reduced by one and the process will continue. On the contrary, if the flag value is 0 (or smaller), the process must wait for another process to release the flag. after unlocking the flag, the function will increase the value of the flag. If needed, you can wake up the waiting process. When the flag is initialized to 1, it becomes a mutex lock. Initialization: void sema_init (struct semaphore * sem, int val)/* val indicates the initialization value */mutex lock initialization: DECLARE_MUTEX (name ); /* 1 name is the semaphore variable */DECLARE_MUTEX_LOCKED (name);/* 0 Same as above */dynamic initialization (runtime): www.2cto.com void init_MUTEX (struct semaphore * sem ); void init_MUTEX_LOCKED (struct semaphore * sem); P function becomes down-or a variant of this name: the function is to reduce semaphores by one, and may cause the caller to sleep for a while. after the flag becomes available, then access the shared resources. Void down (struct semaphore * sem);/* decrease the semaphore and wait for the required time */int down_interruptible (struct semaphore * sem);/* interrupted: allows a process waiting for the user space to be interrupted by the user */int down_trylock (struct semaphore * sem);/* Never Sleep */V function: void up (struct semaphore * sem ); /* if an error occurs when you hold a flag, the flag must be released before the error is returned. Note: the flag must be initialized before the device is available to other parts of the system. Read and write semaphore: down_read (struct validation * sem); -------- down_write (struct rm_semaphore * sem) int down_read_trylock (struct validation * sem); ----- down_write_trylock (struct validation * sem ); void up_read (struct rw_semaphore * sem); ---- void up_write. What is the meaning of Completions? Allow one thread to tell another thread that the job has been completed. Create a completion: DDECLARE_COMLETION (my_completion); dynamic creation and initialization: struct completion my_completion; init_completion (& my_completion); simple call: void wait_for_completion (struct completion * c) // if no one completes this task, the result will be a process that cannot be killed. General call: void complete (struct completion * c);/* a waiting process */void complete_all (struct completion * c ); /* multiple waiting processes */www.2cto.com what is the meaning of spinlock? Background: Most locks are implemented by a spin lock mechanism. special case: spin locks can be used in sleep-less code, such as interrupt processing;
Concept: A mutex device that locks and unlocks the lock. if the lock is available, the lock bit is set and the code continues to enter the critical section. on the contrary, if the lock has been acquired by someone else, the code goes into a compact loop and checks the lock repeatedly until it becomes available. this loop is the "spin" part of the spin lock. (Simple form is not a good situation) requirements: Atomic operations; avoid deadlocks on hyper-threading. # Include Initialization definition: spinlock_t my_lock = SPIN_LOCK_UNLOCKED; or use: void spin_lock_init (spinlock_t * lock) during runtime. when entering a critical section, the code must obtain the required lock: void spin_lock (spinlock_t * lock);/* the spin cannot be interrupted until the lock becomes available */release the lock: void spin_unlock (spinlock_t * lock); Principle: sleep; you cannot discard the processor for any reason except for service interruptions (and sometimes not); spin locks must be held as short as possible.
Cause: Kernel preemption; sleep may occur when data is copied from the user space, and any memory allocation operations are performed. after the spin operation is obtained, the code in the interrupt operation needs to obtain the lock again, this will happen permanently. (To avoid this trap, you must disable interruption when holding the spin lock) void spin_lock (spinlock_t * lock); void spin_lock_irqsave (spinlock_t * lock, unsigned long flags); void spin_lock_irq (spinlock_t * lock ); void spin_lock_bh (spinlock_t * lock) www.2cto.com void spin_unlock (spinlock_t * lock); void trim (spinlock_t * lock, unsigned long flags); void trim (spinlock_t * lock ); void spin_unlock_bh (spinlock_t * lock); trap lock: fuzzy rules lock order rules Fine-coarse-granularity lock www.2cto.com atomic variable: void atomic_set (atomic_t * v, int I ); atomic_t v = ATOMIC_INIT (0 );
Set the atomic variable v to the integer I. you can also use a macro to define ATOMIC_INIT to initialize atomic values during compilation. int atomic_read (atomic_t * v); returns the current value of v. void atomic_add (int I, atomic_t * v); add I. the return value is void, because there is an additional overhead to return the new value, and you do not need to know it most of the time. void atomic_sub (int I, atomic_t * v); subtract I from * v. void atomic_inc (atomic_t * v); void atomic_dec (atomic_t * v); increments or decreases an atomic variable. int atomic_inc_and_test (atomic_t * v); int atomic_dec_and_test (atomic_t * v); int atomic_sub_and_test (int I, atomic_t * v );
Perform a specific operation and test the result. if the atomic value is 0 after the operation, the return value is true. Otherwise, the return value is false. note that there is no atomic_add_and_test.int atomic_add_negative (int I, atomic_t * v); add the integer variable I to v. if the result is a negative value, the returned value is true, otherwise it is false. www.2cto.com bit operation: void set_bit (nr, void * addr); set the nr bit in the data item pointed to by addr. void clear_bit (nr, void * addr );
Clears the unsigned long data located at the addr. its semantics is opposite to that of set_bit. void change_bit (nr, void * addr); flip this bit. test_bit (nr, void * addr); this function is the only bit operation that does not need to be atomic; it simply returns the current value of this bit.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service