Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Function to initialize any fields added in PART 3. You SHOULD NOT
- * initialize anything you added in PART 1 or PART 2.
- */
- void spool_init_cv1(spool_t* sp) {
- pthread_cond_init(&sp->con_cv, NULL);
- sp->shutdown = 0;
- }
- /*
- * Function used to notify all threads that a shutdown has occurred. This
- * function will only be called after all produces have finished.
- */
- void spool_shutdown_efficient(spool_t* sp) {
- sp->shutdown = 1;
- pthread_cond_broadcast(&sp->con_cv);
- }
- /*
- * Function used to implement insertion in a thread safe manner. It
- * should use spool_insert_unchecked once it confirms there is room
- * in the spooler. This function should always insert the item.
- */
- void spool_insert_cv1(spool_t* sp, char* item) {
- pthread_mutex_lock(&sp->buf_lock);
- while (spool_full(sp)) {
- pthread_cond_wait(&sp->con_cv, &sp->buf_lock);
- }
- spool_insert_unchecked(sp, item);
- pthread_cond_signal(&sp->con_cv);
- pthread_mutex_unlock(&sp->buf_lock);
- }
- /*
- * Function used to implement removal in a thread safe manner. It
- * should use spool_remove_unchecked once it confirms there is data
- * in the spooler. This function should only return NULL if a shutdown
- * has been issued and otherwise must return an element.
- */
- char* spool_remove_cv1(spool_t* sp) {
- pthread_mutex_lock(&sp->buf_lock);
- while (spool_empty(sp) && !sp->shutdown) {
- pthread_cond_wait(&sp->con_cv, &sp->buf_lock);
- }
- if (sp->shutdown) {
- pthread_mutex_unlock(&sp->buf_lock);
- return NULL;
- }
- char* item = spool_remove_unchecked(sp);
- pthread_cond_signal(&sp->con_cv);
- pthread_mutex_unlock(&sp->buf_lock);
- return item;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement