Improve queue interface

This commit is contained in:
c9s 2014-05-24 10:20:37 +08:00
parent e27740688a
commit 9c6902ed57
3 changed files with 19 additions and 19 deletions

View file

@ -25,19 +25,19 @@ typedef struct {
} queue; } queue;
// create and return the 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) // destory the queue (free all the memory associate with the que even the data)
void queue_destroy(queue * que); 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 // 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) // return the data from the que (FIFO)
// and free up all the internally allocated memory // and free up all the internally allocated memory
// but the user have to free the returning data pointer // but the user have to free the returning data pointer
void * deque(queue * que); void * queue_pop(queue * que);
#endif /* !R3_QUEUE_H */ #endif /* !R3_QUEUE_H */

View file

@ -11,7 +11,7 @@ pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
/** /**
* create and return a new queue * create and return a new queue
**/ **/
queue * queue_factory() queue * queue_create()
{ {
queue * new_queue = malloc(sizeof(queue)); queue * new_queue = malloc(sizeof(queue));
if(new_queue == NULL) { if(new_queue == NULL) {
@ -60,7 +60,7 @@ void queue_destroy(queue * que)
* que is a queue pointer * que is a queue pointer
* data is a heap allocated memory 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)); queue_node * new_node = malloc(sizeof(queue_node));
if(new_node == NULL) { if(new_node == NULL) {
@ -85,9 +85,9 @@ int enque(queue * que, void * data)
return 0; 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) { if (que == NULL) {
// print("que is null exiting...\n"); // print("que is null exiting...\n");
return NULL; return NULL;
@ -115,7 +115,7 @@ void * deque(queue * que)
// print("Freeing _node@ %p", _node); // print("Freeing _node@ %p", _node);
free(_node); free(_node);
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
// print("Exiting deque\n"); // print("Exiting queue_pop\n");
return data; return data;
} }

View file

@ -22,7 +22,7 @@ struct _stru {
queue * q; queue * q;
}; };
void * func(void * arg) void * producer_thread(void * arg)
{ {
struct _stru * args = (struct _stru *) arg; struct _stru * args = (struct _stru *) arg;
int number = args->number; int number = args->number;
@ -34,19 +34,19 @@ void * func(void * arg)
for(i = 0; i < number; i++) { for(i = 0; i < number; i++) {
char * message = malloc(16); char * message = malloc(16);
snprintf(message, 15, "rand: %d", rand()); snprintf(message, 15, "rand: %d", rand());
enque(q, (void *)message); queue_push(q, (void *)message);
} }
return NULL; return NULL;
} }
void * func_d(void * args) void * consumer_thread(void * args)
{ {
queue * q = (queue *) args; queue * q = (queue *) args;
void * data; void * data;
while((data = deque(q)) != NULL) { while((data = queue_pop(q)) != NULL) {
char * string = (char *)data; char * string = (char *)data;
free(data); free(data);
} }
@ -55,16 +55,16 @@ void * func_d(void * args)
START_TEST (test_queue) START_TEST (test_queue)
{ {
queue * q = queue_factory(); queue * q = queue_create();
enque(q, (void*) 1); queue_push(q, (void*) 1);
int i = (int) deque(q); int i = (int) queue_pop(q);
ck_assert_int_eq(i, 1); ck_assert_int_eq(i, 1);
} }
END_TEST END_TEST
START_TEST (test_queue_threads) START_TEST (test_queue_threads)
{ {
queue * q = queue_factory(); queue * q = queue_create();
pthread_t threads[number_of_threads]; pthread_t threads[number_of_threads];
pthread_t thread_d[number_of_threads_d]; 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].number = number_of_iters;
arg[i].thread_no = i; arg[i].thread_no = i;
arg[i].q = q; 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++) { 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++) { for(i = 0; i < number_of_threads; i++) {