forked from sahaRatul/sela
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacketqueue.c
More file actions
65 lines (54 loc) · 1.33 KB
/
packetqueue.c
File metadata and controls
65 lines (54 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <pthread.h>
#include <malloc.h>
#include "packetqueue.h"
#define MAX_PACKET_COUNT 250
#define MIN_PACKET_COUNT 50
void PacketQueueInit(PacketList *list)
{
list->first=NULL;
list->last=NULL;
list->num_packets=0;
list->total_packets_count=0;
pthread_cond_init(&list->cond,NULL);
pthread_mutex_init(&list->mutex,NULL);
}
void PacketQueueDestroy(PacketList *list)
{
pthread_cond_destroy(&list->cond);
pthread_mutex_destroy(&list->mutex);
}
void PacketQueuePut(PacketList *list,PacketNode *packet)
{
pthread_mutex_lock(&list->mutex);
//Lock when number of packets reaches 200
while(list->num_packets==MAX_PACKET_COUNT)
pthread_cond_wait(&list->cond,&list->mutex);
if(list->num_packets==0)
{
list->first=packet;
list->last=packet;
list->last->next=NULL;
list->num_packets+=1;
}
else
{
list->last->next=packet;
list->last=list->last->next;
list->num_packets+=1;
}
list->total_packets_count+=1;
pthread_mutex_unlock(&list->mutex);
}
PacketNode *PacketQueueGet(PacketList *list)
{
PacketNode *packet;
pthread_mutex_lock(&list->mutex);
packet=list->first;
list->first=list->first->next;
list->num_packets-=1;
//Unlock PacketQueuePut if number of packets is <= MIN_PACKET_COUNT
if(list->num_packets<=MIN_PACKET_COUNT)
pthread_cond_signal(&list->cond);
pthread_mutex_unlock(&list->mutex);
return packet;
}