2
This commit is contained in:
45
server-exp2/taskline.c
Normal file
45
server-exp2/taskline.c
Normal file
@@ -0,0 +1,45 @@
|
||||
/* task pool management fucntions */
|
||||
/* task_pool_init */
|
||||
|
||||
#include "common.h"
|
||||
#include "taskline.h"
|
||||
void task_line_init(task_line_t *tlp, int n)
|
||||
{
|
||||
tlp->taskp = calloc(n, sizeof(task_t));
|
||||
tlp->cnt = n; /* socks holds max of n items */
|
||||
tlp->inpos= tlp->outpos = 0; /* Empty socks iff inpos== outpos */
|
||||
sem_init(&tlp->mutex, 0, 1); /* Binary semaphore for locking */
|
||||
sem_init(&tlp->avail, 0, tlp->cnt);/* Initially, socks has cnt empty cell */
|
||||
sem_init(&tlp->ready, 0, 0); /* Initially, socks has zero data items */
|
||||
}
|
||||
|
||||
/* Clean up task line */
|
||||
void task_line_deinit(task_line_t *tlp)
|
||||
{
|
||||
free(tlp->taskp);
|
||||
}
|
||||
|
||||
/* Insert item onto the rear of task line */
|
||||
void task_insert (task_line_t *tlp, task_t item)
|
||||
{ sem_wait(&tlp->avail); /* Wait for available cell */
|
||||
sem_wait(&tlp->mutex); /* Lock the shared variable inpos pointer */
|
||||
tlp->taskp[tlp->inpos] = item; /* Insert the item */
|
||||
tlp->inpos =(tlp-> inpos +1)%(tlp->cnt); /* adjuset inpos point */
|
||||
sem_post(&tlp->mutex); /* Unlock the buffer */
|
||||
sem_post(&tlp->ready); /* Announce available item */
|
||||
}
|
||||
|
||||
|
||||
/* Remove and return the first item from task_pool */
|
||||
task_t task_remove(task_line_t *tlp)
|
||||
{
|
||||
task_t item;
|
||||
sem_wait(&tlp->ready); /* Wait for available item */
|
||||
sem_wait(&tlp->mutex); /* Lock the shared pointer variable tp->outpos */
|
||||
item = tlp->taskp[tlp->outpos]; /* Remove the item */
|
||||
tlp->outpos=(tlp->outpos+1)%(tlp->cnt); /* adjuset outpos point */
|
||||
sem_post(&tlp->avail); /* Announce available slot */
|
||||
sem_post(&tlp->mutex); /* Unlock the shared pointer variable tp->outpos */
|
||||
return item;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user