Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 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.  
  102. struct lock *
  103. lock_create(const char *name)
  104. {
  105. struct lock *lock;
  106.  
  107. lock = kmalloc(sizeof(struct lock));
  108. if (lock == NULL) {
  109. return NULL;
  110. }
  111.  
  112. lock->name = kstrdup(name);
  113. if (lock->name == NULL) {
  114. kfree(lock);
  115. return NULL;
  116. }
  117. lock->lock_thread = NULL;
  118. return lock;
  119. }
  120.  
  121. void
  122. lock_destroy(struct lock *lock)
  123. {
  124. // if lock is NULL exit the program
  125. assert(lock != NULL);
  126. kfree(lock->name);
  127. kfree(lock);
  128. }
  129.  
  130.  
  131. /*
  132. * lock_acquire - Get the lock. Only one thread can hold the lock at the
  133. * same time.
  134. * lock_release - Free the lock. Only the thread holding the lock may do
  135. * this.
  136. * lock_do_i_hold - Return true if the current thread holds the lock;
  137. * false otherwise.
  138. */
  139.  
  140. void
  141. lock_acquire(struct lock *lock)
  142. {
  143. int spl = splhigh();
  144. // if lock is NULL exit the program
  145. assert(lock != NULL);
  146. while (lock->lock_thread != NULL)
  147. {
  148. thread_sleep(lock);
  149. }
  150. lock->lock_thread = curthread;
  151. splx(spl);
  152. //(void)lock; // suppress warning until code gets written
  153. }
  154.  
  155. void
  156. lock_release(struct lock *lock)
  157. {
  158. int spl = splhigh();
  159. //if lock is NULL exit the program
  160. assert(lock != NULL);
  161. lock->lock_thread = NULL;
  162. thread_wakeup(lock);
  163. splx(spl);
  164. //(void)lock; // suppress warning until code gets written
  165. }
  166.  
  167. int
  168. lock_do_i_hold(struct lock *lock)
  169. {
  170. assert(lock != NULL);
  171. if(lock->lock_thread == curthread)
  172. return 1;
  173.  
  174. return 0;
  175. }
  176.  
  177.  
  178.  
  179.  
  180. //struct lock *
  181. //lock_create(const char *name)
  182. //{
  183. // struct lock *lock;
  184. //
  185. // lock = kmalloc(sizeof(struct lock));
  186. // if (lock == NULL) {
  187. // return NULL;
  188. // }
  189. //
  190. // lock->name = kstrdup(name);
  191. // if (lock->name == NULL) {
  192. // kfree(lock);
  193. // return NULL;
  194. // }
  195. //
  196. // // add stuff here as needed
  197. // lock -> threadLocker = NULL;
  198. // lock -> name = name; //initially sets the name to name
  199. // return lock;
  200. //}
  201. //
  202. //// Destroys the thread
  203. //void
  204. //lock_destroy(struct lock *lock)
  205. //{
  206. // int spl;
  207. // spl = splhigh();
  208. // assert(lock != NULL);
  209. //
  210. // // add stuff here as needed
  211. // splx(spl);
  212. //
  213. // kfree(lock->name);
  214. // kfree(lock -> threadLocker); //frees the volatile threadlocker
  215. // kfree(lock);
  216. //}
  217. //
  218. //void
  219. //lock_acquire(struct lock *lock)
  220. //{
  221. // // You can aqquire a lock
  222. // int spl;
  223. // spl = splhigh(); //disable global interrupts, enters kernel mode
  224. // //There's something in the threadLocker, spinlock and puts it in queue
  225. // while(lock -> threadLocker){
  226. // thread_sleep(lock);
  227. // }
  228. // //You can aqquire a lock
  229. // lock -> threadLocker = curthread;
  230. //
  231. // splx(spl); //returns to user mode
  232. //}
  233. //
  234. //void
  235. //lock_release(struct lock *lock)
  236. //{
  237. // // Write this
  238. // int spl;
  239. // spl = splhigh(); //disable global interrupts
  240. //
  241. //
  242. // //checks if current thread holds the lock and lock is not null
  243. // if(lock != NULL && lock_do_i_hold(lock)){
  244. // kfree(lock->name);
  245. // kfree(lock -> threadLocker); //frees the volatile threadlocker
  246. // }
  247. // splx(spl);
  248. //// (void)lock; // suppress warning until code gets written
  249. //}
  250. //
  251. //int
  252. //lock_do_i_hold(struct lock *lock)
  253. //{
  254. // // Write this
  255. //
  256. // if(lock != NULL && lock -> threadLocker == curthread)
  257. // return 1;
  258. // return 0; // dummy until code gets written
  259. // //(void)lock; // suppress warning until code gets written
  260. //}
  261.  
  262. ////////////////////////////////////////////////////////////
  263. //
  264. // CV
  265.  
  266.  
  267. struct cv *
  268. cv_create(const char *name)
  269. {
  270. struct cv *cv;
  271.  
  272. cv = kmalloc(sizeof(struct cv));
  273. if (cv == NULL) {
  274. return NULL;
  275. }
  276.  
  277. cv->name = kstrdup(name);
  278. if (cv->name==NULL) {
  279. kfree(cv);
  280. return NULL;
  281. }
  282.  
  283. // add stuff here as needed
  284.  
  285. return cv;
  286. }
  287.  
  288. void
  289. cv_destroy(struct cv *cv)
  290. {
  291. assert(cv != NULL);
  292.  
  293. // add stuff here as needed
  294.  
  295. kfree(cv->name);
  296. kfree(cv);
  297. }
  298.  
  299. void
  300. cv_wait(struct cv *cv, struct lock *lock)
  301. {
  302. // Write this
  303. (void)cv; // suppress warning until code gets written
  304. (void)lock; // suppress warning until code gets written
  305. }
  306.  
  307. void
  308. cv_signal(struct cv *cv, struct lock *lock)
  309. {
  310. // Write this
  311. (void)cv; // suppress warning until code gets written
  312. (void)lock; // suppress warning until code gets written
  313. }
  314.  
  315. void
  316. cv_broadcast(struct cv *cv, struct lock *lock)
  317. {
  318. // Write this
  319. (void)cv; // suppress warning until code gets written
  320. (void)lock; // suppress warning until code gets written
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement