Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.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. if(lock->lock_thread != NULL) {
  151. //put in queue
  152. }
  153. else {
  154. lock->lock_thread = curthread;
  155. }
  156. splx(spl);
  157. //(void)lock; // suppress warning until code gets written
  158. }
  159.  
  160. void
  161. lock_release(struct lock *lock)
  162. {
  163. int spl = splhigh();
  164. //if lock is NULL exit the program
  165. assert(lock != NULL);
  166. lock->lock_thread = NULL;
  167. thread_wakeup(lock);
  168. splx(spl);
  169. //(void)lock; // suppress warning until code gets written
  170. }
  171.  
  172. int
  173. lock_do_i_hold(struct lock *lock)
  174. {
  175. assert(lock != NULL);
  176. if(lock->lock_thread == curthread)
  177. return 1;
  178.  
  179. return 0;
  180. }
  181.  
  182. //struct lock *
  183. //lock_create(const char *name)
  184. //{
  185. // struct lock *lock;
  186. //
  187. // lock = kmalloc(sizeof(struct lock));
  188. // if (lock == NULL) {
  189. // return NULL;
  190. // }
  191. //
  192. // lock->name = kstrdup(name);
  193. // if (lock->name == NULL) {
  194. // kfree(lock);
  195. // return NULL;
  196. // }
  197. //
  198. // // add stuff here as needed
  199. // lock -> threadLocker = NULL;
  200. // lock -> name = name; //initially sets the name to name
  201. // return lock;
  202. //}
  203. //
  204. //// Destroys the thread
  205. //void
  206. //lock_destroy(struct lock *lock)
  207. //{
  208. // int spl;
  209. // spl = splhigh();
  210. // assert(lock != NULL);
  211. //
  212. // // add stuff here as needed
  213. // splx(spl);
  214. //
  215. // kfree(lock->name);
  216. // kfree(lock -> threadLocker); //frees the volatile threadlocker
  217. // kfree(lock);
  218. //}
  219. //
  220. //void
  221. //lock_acquire(struct lock *lock)
  222. //{
  223. // // You can aqquire a lock
  224. // int spl;
  225. // spl = splhigh(); //disable global interrupts, enters kernel mode
  226. // //There's something in the threadLocker, spinlock and puts it in queue
  227. // while(lock -> threadLocker){
  228. // thread_sleep(lock);
  229. // }
  230. // //You can aqquire a lock
  231. // lock -> threadLocker = curthread;
  232. //
  233. // splx(spl); //returns to user mode
  234. //}
  235. //
  236. //void
  237. //lock_release(struct lock *lock)
  238. //{
  239. // // Write this
  240. // int spl;
  241. // spl = splhigh(); //disable global interrupts
  242. //
  243. //
  244. // //checks if current thread holds the lock and lock is not null
  245. // if(lock != NULL && lock_do_i_hold(lock)){
  246. // kfree(lock->name);
  247. // kfree(lock -> threadLocker); //frees the volatile threadlocker
  248. // }
  249. // splx(spl);
  250. //// (void)lock; // suppress warning until code gets written
  251. //}
  252. //
  253. //int
  254. //lock_do_i_hold(struct lock *lock)
  255. //{
  256. // // Write this
  257. //
  258. // if(lock != NULL && lock -> threadLocker == curthread)
  259. // return 1;
  260. // return 0; // dummy until code gets written
  261. // //(void)lock; // suppress warning until code gets written
  262. //}
  263.  
  264. ////////////////////////////////////////////////////////////
  265. //
  266. // CV
  267.  
  268.  
  269. struct cv *
  270. cv_create(const char *name)
  271. {
  272. struct cv *cv;
  273.  
  274. cv = kmalloc(sizeof(struct cv));
  275. if (cv == NULL) {
  276. return NULL;
  277. }
  278.  
  279. cv->name = kstrdup(name);
  280. if (cv->name==NULL) {
  281. kfree(cv);
  282. return NULL;
  283. }
  284.  
  285. // add stuff here as needed
  286.  
  287. return cv;
  288. }
  289.  
  290. void
  291. cv_destroy(struct cv *cv)
  292. {
  293. assert(cv != NULL);
  294.  
  295. // add stuff here as needed
  296.  
  297. kfree(cv->name);
  298. kfree(cv);
  299. }
  300.  
  301. void
  302. cv_wait(struct cv *cv, struct lock *lock)
  303. {
  304. // Write this
  305. (void)cv; // suppress warning until code gets written
  306. (void)lock; // suppress warning until code gets written
  307. }
  308.  
  309. void
  310. cv_signal(struct cv *cv, struct lock *lock)
  311. {
  312. // Write this
  313. (void)cv; // suppress warning until code gets written
  314. (void)lock; // suppress warning until code gets written
  315. }
  316.  
  317. void
  318. cv_broadcast(struct cv *cv, struct lock *lock)
  319. {
  320. // Write this
  321. (void)cv; // suppress warning until code gets written
  322. (void)lock; // suppress warning until code gets written
  323. }
  324.  
  325. //Queue
  326.  
  327. struct queue*
  328. create_queue() {
  329. struct queue *q = (struct queue*) kmalloc(sizeof(struct queue));
  330. q-> front = NULL;
  331. return q;
  332. }
  333.  
  334. struct node*
  335. create_node(struct thread *calling_thread) {
  336. struct node *new_node = (struct node*) kmalloc(sizeof(struct node));
  337. new_node->next = NULL;
  338. new_node->data = calling_thread;
  339. return new_node;
  340. }
  341.  
  342. void
  343. enqueue(struct queue *q, struct thread * calling_thread) {
  344. struct node *new_node = create_node(calling_thread);
  345.  
  346. if(q->rear == NULL) {
  347. q->front = new_node;
  348. q->rear = new_node;
  349. }
  350. else {
  351. q->rear->next = new_node;
  352. q->rear = new_node;
  353. }
  354.  
  355. }
  356.  
  357. struct thread*
  358. dequeue(struct queue *q) {
  359. if(q->front == NULL) {
  360. return NULL;
  361. }
  362. struct node* last_node = q->front;
  363. q->front = q->front->next;
  364.  
  365. if(q->front == NULL) {
  366. q->rear = NULL;
  367. }
  368. return last_node;
  369. return NULL;
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement