Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. /*
  2. * Synchronization primitives.
  3. * See synch.h for specifications of the functions.
  4. */
  5.  
  6. #include <types.h>
  7. #include <lib.h>
  8. #include <synch.h>
  9. #include <thread.h>
  10. #include <curthread.h>
  11. #include <machine/spl.h>
  12.  
  13. ////////////////////////////////////////////////////////////
  14. //
  15. // Semaphore.
  16.  
  17. struct semaphore *
  18. sem_create(const char *namearg, int initial_count)
  19. {
  20. struct semaphore *sem;
  21.  
  22. assert(initial_count >= 0);
  23.  
  24. sem = kmalloc(sizeof(struct semaphore));
  25. if (sem == NULL) {
  26. return NULL;
  27. }
  28.  
  29. sem->name = kstrdup(namearg);
  30. if (sem->name == NULL) {
  31. kfree(sem);
  32. return NULL;
  33. }
  34.  
  35. sem->count = initial_count;
  36. return sem;
  37. }
  38.  
  39. void
  40. sem_destroy(struct semaphore *sem)
  41. {
  42. int spl;
  43. assert(sem != NULL);
  44.  
  45. spl = splhigh();
  46. assert(thread_hassleepers(sem)==0);
  47. splx(spl);
  48.  
  49. /*
  50. * Note: while someone could theoretically start sleeping on
  51. * the semaphore after the above test but before we free it,
  52. * if they're going to do that, they can just as easily wait
  53. * a bit and start sleeping on the semaphore after it's been
  54. * freed. Consequently, there's not a whole lot of point in
  55. * including the kfrees in the splhigh block, so we don't.
  56. */
  57.  
  58. kfree(sem->name);
  59. kfree(sem);
  60. }
  61.  
  62. void
  63. P(struct semaphore *sem)
  64. {
  65. int spl;
  66. assert(sem != NULL);
  67.  
  68. /*
  69. * May not block in an interrupt handler.
  70. *
  71. * For robustness, always check, even if we can actually
  72. * complete the P without blocking.
  73. */
  74. assert(in_interrupt==0);
  75.  
  76. spl = splhigh();
  77. while (sem->count==0) {
  78. thread_sleep(sem);
  79. }
  80. assert(sem->count>0);
  81. sem->count--;
  82. splx(spl);
  83. }
  84.  
  85. void
  86. V(struct semaphore *sem)
  87. {
  88. int spl;
  89. assert(sem != NULL);
  90. spl = splhigh();
  91. sem->count++;
  92. assert(sem->count>0);
  93. thread_wakeup(sem);
  94. splx(spl);
  95. }
  96.  
  97. ////////////////////////////////////////////////////////////
  98. //
  99. // Lock.
  100.  
  101. struct lock *
  102. lock_create(const char *name)
  103. {
  104. struct lock *lock;
  105.  
  106. lock = kmalloc(sizeof(struct lock));
  107. if (lock == NULL) {
  108. return NULL;
  109. }
  110.  
  111. lock->name = kstrdup(name);
  112. if (lock->name == NULL) {
  113. kfree(lock);
  114. return NULL;
  115. }
  116.  
  117. // add stuff here as needed
  118. lock -> threadLocker = NULL;
  119. lock -> name = name; //initially sets the name to name
  120. return lock;
  121. }
  122.  
  123. // Destroys the thread
  124. void
  125. lock_destroy(struct lock *lock)
  126. {
  127. int spl;
  128. spl = splhigh();
  129. assert(lock != NULL);
  130.  
  131. // add stuff here as needed
  132. splx(spl);
  133.  
  134. kfree(lock->name);
  135. kfree(lock -> threadLocker); //frees the volatile threadlocker
  136. kfree(lock);
  137. }
  138.  
  139. //void
  140. //lock_acquire(struct lock *lock)
  141. //{
  142. // // Write this
  143. //
  144. // (void)lock; // suppress warning until code gets written
  145. //}
  146.  
  147. void
  148. lock_acquire(struct lock *lock)
  149. {
  150. // You can aqquire a lock
  151. int spl;
  152. spl = splhigh(); //disable global interrupts, enters kernel mode
  153. //There's something in the threadLocker, spinlock and puts it in queue
  154. while(lock -> threadLocker){
  155. thread_sleep(lock);
  156. }
  157. //You can aqquire a lock
  158. lock -> threadLocker = curthread;
  159.  
  160. splx(spl); //returns to user mode
  161. }
  162.  
  163. void
  164. lock_release(struct lock *lock)
  165. {
  166. // Write this
  167. int spl;
  168. spl = splhigh(); //disable global interrupts
  169.  
  170.  
  171. //checks if current thread holds the lock and lock is not null
  172. if(lock != NULL && lock_do_i_hold(lock)){
  173. kfree(lock->name);
  174. kfree(lock -> threadLocker); //frees the volatile threadlocker
  175. }
  176. splx(spl);
  177. // (void)lock; // suppress warning until code gets written
  178. }
  179.  
  180. int
  181. lock_do_i_hold(struct lock *lock)
  182. {
  183. // Write this
  184.  
  185. if(lock != NULL && lock -> threadLocker == curthread)
  186. return 1;
  187. return 0; // dummy until code gets written
  188. //(void)lock; // suppress warning until code gets written
  189. }
  190.  
  191. ////////////////////////////////////////////////////////////
  192. //
  193. // CV
  194.  
  195.  
  196. struct cv *
  197. cv_create(const char *name)
  198. {
  199. struct cv *cv;
  200.  
  201. cv = kmalloc(sizeof(struct cv));
  202. if (cv == NULL) {
  203. return NULL;
  204. }
  205.  
  206. cv->name = kstrdup(name);
  207. if (cv->name==NULL) {
  208. kfree(cv);
  209. return NULL;
  210. }
  211.  
  212. // add stuff here as needed
  213.  
  214. return cv;
  215. }
  216.  
  217. void
  218. cv_destroy(struct cv *cv)
  219. {
  220. assert(cv != NULL);
  221.  
  222. // add stuff here as needed
  223.  
  224. kfree(cv->name);
  225. kfree(cv);
  226. }
  227.  
  228. void
  229. cv_wait(struct cv *cv, struct lock *lock)
  230. {
  231. // Write this
  232. (void)cv; // suppress warning until code gets written
  233. (void)lock; // suppress warning until code gets written
  234. }
  235.  
  236. void
  237. cv_signal(struct cv *cv, struct lock *lock)
  238. {
  239. // Write this
  240. (void)cv; // suppress warning until code gets written
  241. (void)lock; // suppress warning until code gets written
  242. }
  243.  
  244. void
  245. cv_broadcast(struct cv *cv, struct lock *lock)
  246. {
  247. // Write this
  248. (void)cv; // suppress warning until code gets written
  249. (void)lock; // suppress warning until code gets written
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement