Improve queue interface
This commit is contained in:
parent
e27740688a
commit
9c6902ed57
3 changed files with 19 additions and 19 deletions
|
@ -25,19 +25,19 @@ typedef struct {
|
|||
} queue;
|
||||
|
||||
// create and return the queue
|
||||
queue * queue_factory(void);
|
||||
queue * queue_create(void);
|
||||
|
||||
// destory the queue (free all the memory associate with the que even the data)
|
||||
void queue_destroy(queue * que);
|
||||
|
||||
|
||||
// enque the data into queue
|
||||
// queue_push the data into queue
|
||||
// data is expected to a pointer to a heap allocated memory
|
||||
int enque(queue * que, void * data);
|
||||
int queue_push(queue * que, void * data);
|
||||
|
||||
// return the data from the que (FIFO)
|
||||
// and free up all the internally allocated memory
|
||||
// but the user have to free the returning data pointer
|
||||
void * deque(queue * que);
|
||||
void * queue_pop(queue * que);
|
||||
|
||||
#endif /* !R3_QUEUE_H */
|
||||
|
|
10
src/queue.c
10
src/queue.c
|
@ -11,7 +11,7 @@ pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
|||
/**
|
||||
* create and return a new queue
|
||||
**/
|
||||
queue * queue_factory()
|
||||
queue * queue_create()
|
||||
{
|
||||
queue * new_queue = malloc(sizeof(queue));
|
||||
if(new_queue == NULL) {
|
||||
|
@ -60,7 +60,7 @@ void queue_destroy(queue * que)
|
|||
* que is a queue pointer
|
||||
* data is a heap allocated memory pointer
|
||||
*/
|
||||
int enque(queue * que, void * data)
|
||||
int queue_push(queue * que, void * data)
|
||||
{
|
||||
queue_node * new_node = malloc(sizeof(queue_node));
|
||||
if(new_node == NULL) {
|
||||
|
@ -85,9 +85,9 @@ int enque(queue * que, void * data)
|
|||
return 0;
|
||||
}
|
||||
|
||||
void * deque(queue * que)
|
||||
void * queue_pop(queue * que)
|
||||
{
|
||||
// print("Entered to deque\n");
|
||||
// print("Entered to queue_pop\n");
|
||||
if (que == NULL) {
|
||||
// print("que is null exiting...\n");
|
||||
return NULL;
|
||||
|
@ -115,7 +115,7 @@ void * deque(queue * que)
|
|||
// print("Freeing _node@ %p", _node);
|
||||
free(_node);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
// print("Exiting deque\n");
|
||||
// print("Exiting queue_pop\n");
|
||||
return data;
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ struct _stru {
|
|||
queue * q;
|
||||
};
|
||||
|
||||
void * func(void * arg)
|
||||
void * producer_thread(void * arg)
|
||||
{
|
||||
struct _stru * args = (struct _stru *) arg;
|
||||
int number = args->number;
|
||||
|
@ -34,19 +34,19 @@ void * func(void * arg)
|
|||
for(i = 0; i < number; i++) {
|
||||
char * message = malloc(16);
|
||||
snprintf(message, 15, "rand: %d", rand());
|
||||
enque(q, (void *)message);
|
||||
queue_push(q, (void *)message);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
void * func_d(void * args)
|
||||
void * consumer_thread(void * args)
|
||||
{
|
||||
queue * q = (queue *) args;
|
||||
|
||||
void * data;
|
||||
while((data = deque(q)) != NULL) {
|
||||
while((data = queue_pop(q)) != NULL) {
|
||||
char * string = (char *)data;
|
||||
free(data);
|
||||
}
|
||||
|
@ -55,16 +55,16 @@ void * func_d(void * args)
|
|||
|
||||
START_TEST (test_queue)
|
||||
{
|
||||
queue * q = queue_factory();
|
||||
enque(q, (void*) 1);
|
||||
int i = (int) deque(q);
|
||||
queue * q = queue_create();
|
||||
queue_push(q, (void*) 1);
|
||||
int i = (int) queue_pop(q);
|
||||
ck_assert_int_eq(i, 1);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_queue_threads)
|
||||
{
|
||||
queue * q = queue_factory();
|
||||
queue * q = queue_create();
|
||||
pthread_t threads[number_of_threads];
|
||||
pthread_t thread_d[number_of_threads_d];
|
||||
|
||||
|
@ -74,11 +74,11 @@ START_TEST (test_queue_threads)
|
|||
arg[i].number = number_of_iters;
|
||||
arg[i].thread_no = i;
|
||||
arg[i].q = q;
|
||||
pthread_create(threads+i, NULL, func, (void *)&arg[i]);
|
||||
pthread_create(threads+i, NULL, producer_thread, (void *)&arg[i]);
|
||||
}
|
||||
|
||||
for(i = 0; i < number_of_threads_d; i++) {
|
||||
pthread_create(thread_d+i, NULL, func_d, (void *)q);
|
||||
pthread_create(thread_d+i, NULL, consumer_thread, (void *)q);
|
||||
}
|
||||
|
||||
for(i = 0; i < number_of_threads; i++) {
|
||||
|
|
Loading…
Reference in a new issue