-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
623 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ _CPack_Packages/* | |
*.deb | ||
!.gitignore | ||
src/zblacklist | ||
src/ztee |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#include "queue.h" | ||
#include <pthread.h> | ||
//queue_lock used for push and pop | ||
|
||
pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER; | ||
pthread_cond_t queue_empty = PTHREAD_COND_INITIALIZER; | ||
|
||
queue* queue_init() | ||
{ | ||
//call with queue_init(&queue); | ||
queue *p = malloc(sizeof(queue)); | ||
p->front = NULL; | ||
p->back = NULL; | ||
p->size = 0; | ||
return p; | ||
} | ||
|
||
int is_empty (queue *my_queue) | ||
{ | ||
if (my_queue->front == NULL && my_queue->front == NULL) return 1; | ||
return 0; | ||
} | ||
|
||
void push_back(char* data, queue *my_queue) | ||
{ | ||
node *new_node = malloc(sizeof(node)); | ||
new_node->prev = NULL; | ||
new_node->next = NULL; | ||
new_node->data = strdup(data); | ||
|
||
pthread_mutex_lock(&queue_lock); | ||
if (is_empty(my_queue)) { | ||
my_queue->front = new_node; | ||
my_queue->back = new_node; | ||
} else { | ||
my_queue->back->next = (struct node*)new_node; | ||
new_node->prev = (struct node*)my_queue->back; | ||
my_queue->back = new_node; | ||
} | ||
my_queue->size++; | ||
pthread_cond_signal(&queue_empty); | ||
pthread_mutex_unlock(&queue_lock); | ||
} | ||
|
||
node* pop_front(queue *my_queue) | ||
{ | ||
pthread_mutex_lock(&queue_lock); | ||
|
||
while (is_empty(my_queue)) { | ||
pthread_cond_wait(&queue_empty, &queue_lock); | ||
} | ||
node *temp = my_queue->front; | ||
my_queue->front = (node*)temp->next; | ||
if (my_queue->front != NULL) { | ||
my_queue->front->prev = NULL; | ||
} | ||
my_queue->size--; | ||
pthread_mutex_unlock(&queue_lock); | ||
return temp; | ||
} | ||
|
||
node* get_front(queue *my_queue) | ||
{ | ||
pthread_mutex_lock(&queue_lock); | ||
|
||
while (is_empty(my_queue)) { | ||
pthread_cond_wait(&queue_empty, &queue_lock); | ||
} | ||
|
||
node *temp = malloc(sizeof(node)); | ||
temp = my_queue->front; | ||
pthread_mutex_unlock(&queue_lock); | ||
return temp; | ||
} | ||
|
||
node* get_back(queue *my_queue) | ||
{ | ||
pthread_mutex_lock(&queue_lock); | ||
|
||
while (is_empty(my_queue)) { | ||
pthread_cond_wait(&queue_empty, &queue_lock); | ||
} | ||
|
||
node *temp = malloc(sizeof(node)); | ||
temp = my_queue->back; | ||
pthread_mutex_unlock(&queue_lock); | ||
return temp; | ||
} | ||
|
||
void delete_queue(queue *my_queue) | ||
{ | ||
while (!is_empty(my_queue)) { | ||
pop_front(my_queue); | ||
} | ||
} | ||
|
||
void check_queue(queue *my_queue) | ||
{ | ||
node *temp = my_queue->front; | ||
while(temp){ | ||
temp = (node*)temp->next; | ||
} | ||
} | ||
|
||
int get_size(queue *my_queue) | ||
{ | ||
int buffer_size; | ||
pthread_mutex_lock(&queue_lock); | ||
buffer_size = my_queue->size; | ||
pthread_mutex_unlock(&queue_lock); | ||
return buffer_size; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <pthread.h> | ||
|
||
typedef struct queue_node { | ||
char* data; | ||
struct node *prev; | ||
struct node *next; | ||
} node; | ||
|
||
typedef struct ztee_queue { | ||
node *front; | ||
node *back; | ||
int size; | ||
} queue; | ||
|
||
queue* queue_init (); | ||
int is_empty (queue *my_queue); | ||
void push_back (char* data, queue *my_queue); | ||
node* pop_front (queue *my_queue); | ||
node* get_front (queue *my_queue); | ||
node* get_back (queue *my_queue); | ||
void delete_queue (queue *my_queue); | ||
void check_queue (queue *my_queue); | ||
int get_size (queue *my_queue); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.