-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincome.c
More file actions
215 lines (163 loc) · 4.29 KB
/
income.c
File metadata and controls
215 lines (163 loc) · 4.29 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Progetto del corso di LSO 2017/2018
*
* Dipartimento di Informatica Università di Pisa
* Docenti: Prencipe, Torquati
*
* Autore: Alessandro Meschi
* Matricola: 525658
* Email: alessandro.meschi@icloud.com
*
* Questo programma è, in ogni sua parte, opera originale dell'autore.
*/
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include "config.h"
#include "errors.h"
#include "utils.h"
#include "income.h"
/**
* @file income.c
*
* @author Alessandro Meschi
*
* @date 7 Maggio 2019
*
* @brief Contiene la definizione delle funzioni di inizializzazione, gestione e
* distruzione della struttura @ref incomeList
*/
/*---------------DEFINIZIONE FUNZIONI INCOME LIST----------------------*/
int initializeIncomeList()
{
int test;
test = pthread_mutex_init(&(service.incomeLock), NULL);
if ( test == -1 )
{
errno = test;
CHECK_FATAL_ERROR(-1, "Inizializzando la lock relativa alla lista di servizio");
}
test = pthread_cond_init(&(service.incomeCond), NULL);
if ( test == -1 )
{
errno = test;
CHECK_FATAL_ERROR(-1, "Inizializzando la variabile condizione relativa alla lista di servizio");
}
service.length = 0;
service.head = NULL;
return 0;
}
int pushIncome(int fd)
{
if(fd <= 0){errno = EINVAL; return -1;}
int test;
income_t* iterator;
income_t* new;
new = (income_t*)malloc(sizeof(income_t));
CHECK_NULL_FATAL_ERROR(new, "Allocando memoria per il nuovo nodo della coda di servizio")
new->fd = fd;
new->next = NULL;
LOCK(service.incomeLock);
if(service.head == NULL)
service.head = new;
else
{
iterator = service.head;
while(iterator->next != NULL)
iterator = iterator->next;
iterator->next = new;
}
service.length++;
UNLOCK(service.incomeLock);
test = pthread_cond_signal(&(service.incomeCond));
if(test != 0)
{
errno = test;
CHECK_FATAL_ERROR(-1, "Segnalando la presenza di un nuovo nodo nella coda di servizio")
}
return 0;
}
int popIncome()
{
int test;
int retValue;
income_t* tmp = NULL;
LOCK(service.incomeLock);
while(service.length == 0)
{
test = pthread_cond_wait(&(service.incomeCond), &(service.incomeLock));
if(test != 0)
{
errno = test;
CHECK_FATAL_ERROR(-1, "Aspettando un nuovo nodo nella coda di servizio");
}
}
if(service.head == NULL){errno = EPERM; return -1;}
retValue = service.head->fd;
if(retValue != INCOME_LIST_TERM)
{
tmp = service.head;
service.head = tmp->next;
service.length--;
free(tmp);
}
UNLOCK(service.incomeLock);
return retValue;
}
int breakIncomeList()
{
int test;
income_t* iterator;
income_t* new;
new = (income_t*)malloc(sizeof(income_t));
CHECK_NULL_FATAL_ERROR(new, "Allocando spazio per il nodo di terminazione della coda di servizio");
new->fd = INCOME_LIST_TERM;
new->next = NULL;
LOCK(service.incomeLock);
if(service.head == NULL)
service.head = new;
else
{
iterator = service.head;
while(iterator->next != NULL)
iterator = iterator->next;
iterator->next = new;
}
service.length++;
UNLOCK(service.incomeLock);
test = pthread_cond_broadcast(&(service.incomeCond));
if(test != 0)
{
errno = test;
CHECK_FATAL_ERROR(-1, "Segnalando la presenza del nodo di terminazione nella coda di servizio");
}
return 0;
}
int destroyIncomeList()
{
int test;
income_t* save;
income_t* iterator;
iterator = service.head;
while(iterator != NULL)
{
save = iterator->next;
free(iterator);
iterator = save;
}
test = pthread_mutex_destroy(&(service.incomeLock));
if(test != 0)
{
errno = test;
CHECK_FATAL_ERROR(-1, "Distruggendo la lock relativa alla coda di servizio");
}
test = pthread_cond_destroy(&(service.incomeCond));
if(test != 0)
{
errno = test;
CHECK_FATAL_ERROR(-1, "Distruggendo la variabile condizione relativa alla coda di servizio");
}
return 0;
}
/*---------------------------------------------------------------------*/