sleeplock files
This commit is contained in:
		
							parent
							
								
									dec637bc59
								
							
						
					
					
						commit
						564a1cf0cb
					
				
					 2 changed files with 66 additions and 0 deletions
				
			
		
							
								
								
									
										56
									
								
								sleeplock.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								sleeplock.c
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,56 @@
 | 
				
			||||||
 | 
					// Sleeping locks
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "types.h"
 | 
				
			||||||
 | 
					#include "defs.h"
 | 
				
			||||||
 | 
					#include "param.h"
 | 
				
			||||||
 | 
					#include "x86.h"
 | 
				
			||||||
 | 
					#include "memlayout.h"
 | 
				
			||||||
 | 
					#include "mmu.h"
 | 
				
			||||||
 | 
					#include "proc.h"
 | 
				
			||||||
 | 
					#include "spinlock.h"
 | 
				
			||||||
 | 
					#include "sleeplock.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					initsleeplock(struct sleeplock *lk, char *name)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  initlock(&lk->lk, "sleep lock");
 | 
				
			||||||
 | 
					  lk->name = name;
 | 
				
			||||||
 | 
					  lk->locked = 0;
 | 
				
			||||||
 | 
					  lk->pid = 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					acquiresleep(struct sleeplock *lk)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  acquire(&lk->lk);
 | 
				
			||||||
 | 
					  while (lk->locked) {
 | 
				
			||||||
 | 
					    sleep(lk, &lk->lk);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  lk->locked = 1;
 | 
				
			||||||
 | 
					  lk->pid = proc->pid;
 | 
				
			||||||
 | 
					  release(&lk->lk);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					releasesleep(struct sleeplock *lk)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  acquire(&lk->lk);
 | 
				
			||||||
 | 
					  lk->locked = 0;
 | 
				
			||||||
 | 
					  lk->pid = 0;
 | 
				
			||||||
 | 
					  wakeup(lk);
 | 
				
			||||||
 | 
					  release(&lk->lk);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int
 | 
				
			||||||
 | 
					holdingsleep(struct sleeplock *lk)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  int r;
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  acquire(&lk->lk);
 | 
				
			||||||
 | 
					  r = lk->locked;
 | 
				
			||||||
 | 
					  release(&lk->lk);
 | 
				
			||||||
 | 
					  return r;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
							
								
								
									
										10
									
								
								sleeplock.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								sleeplock.h
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,10 @@
 | 
				
			||||||
 | 
					// Long-term locks for processes
 | 
				
			||||||
 | 
					struct sleeplock {
 | 
				
			||||||
 | 
					  uint locked;       // Is the lock held?
 | 
				
			||||||
 | 
					  struct spinlock lk; // spinlock protecting this sleep lock
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  // For debugging:
 | 
				
			||||||
 | 
					  char *name;        // Name of lock.
 | 
				
			||||||
 | 
					  int pid;           // Process holding lock
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue