SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 120 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /*
  3.  * Function to initialize any fields added in PART 3. You SHOULD NOT
  4.  * initialize anything you added in PART 1 or PART 2.
  5.  */
  6. void spool_init_cv1(spool_t* sp) {
  7.     pthread_cond_init(&sp->con_cv, NULL);
  8.     sp->shutdown = 0;
  9. }
  10.  
  11. /*
  12.  * Function used to notify all threads that a shutdown has occurred. This
  13.  * function will only be called after all produces have finished.
  14.  */
  15. void spool_shutdown_efficient(spool_t* sp) {
  16.     sp->shutdown = 1;
  17.     pthread_cond_broadcast(&sp->con_cv);
  18. }
  19.  
  20. /*
  21.  * Function used to implement insertion in a thread safe manner. It
  22.  * should use spool_insert_unchecked once it confirms there is room
  23.  * in the spooler. This function should always insert the item.
  24.  */
  25. void spool_insert_cv1(spool_t* sp, char* item) {
  26.     pthread_mutex_lock(&sp->buf_lock);
  27.     while (spool_full(sp)) {
  28.         pthread_cond_wait(&sp->con_cv, &sp->buf_lock);
  29.     }
  30.     spool_insert_unchecked(sp, item);
  31.     pthread_cond_signal(&sp->con_cv);
  32.     pthread_mutex_unlock(&sp->buf_lock);
  33. }
  34.  
  35. /*
  36.  * Function used to implement removal in a thread safe manner. It
  37.  * should use spool_remove_unchecked once it confirms there is data
  38.  * in the spooler. This function should only return NULL if a shutdown
  39.  * has been issued and otherwise must return an element.
  40.  */
  41. char* spool_remove_cv1(spool_t* sp) {
  42.   pthread_mutex_lock(&sp->buf_lock);
  43.   while (spool_empty(sp) && !sp->shutdown) {
  44.        pthread_cond_wait(&sp->con_cv, &sp->buf_lock);
  45.   }
  46.   if (sp->shutdown) {
  47.     pthread_mutex_unlock(&sp->buf_lock);
  48.     return NULL;
  49.   }
  50.   char* item = spool_remove_unchecked(sp);
  51.   pthread_cond_signal(&sp->con_cv);
  52.   pthread_mutex_unlock(&sp->buf_lock);
  53.   return item;
  54. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top