-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfdSet.c
More file actions
112 lines (84 loc) · 1.99 KB
/
fdSet.c
File metadata and controls
112 lines (84 loc) · 1.99 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
/*
* 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 <sys/select.h>
#include <pthread.h>
#include <errno.h>
#include "fdSet.h"
#include "utils.h"
/**
* @file fdSet.h
*
* @author Alessandro Meschi
*
* @date 7 Maggio 2019
*
* @brief Contiene le definizioni delle funzioni di inizializzazione,
* gestione e distruzione della struttura @ref fdSet
* @see fdSet.h
*/
/*-----------------DEFINIZIONE FUNZIONI GESTIONE FDSET-----------------*/
int initializeSet()
{
int test;
masterSet.maxFd = 0;
test = pthread_mutex_init(&(masterSet.setLock), NULL);
if (test != 0) {errno=test; return -1;}
FD_ZERO(&(masterSet.master));
return 0;
}
int INset(int fd)
{
if (fd <= 0) {errno = EINVAL; return -1;}
LOCK(masterSet.setLock)
FD_SET(fd, &(masterSet.master));
if(fd > masterSet.maxFd)
masterSet.maxFd = fd;
UNLOCK(masterSet.setLock)
return 0;
}
int OUTset(int fd)
{
if (fd <= 0) {errno = EINVAL; return -1;}
LOCK(masterSet.setLock)
FD_CLR(fd, &(masterSet.master));
if(fd == masterSet.maxFd)
{
for(int i=(masterSet.maxFd-1); i>=0; i--)
{
if (FD_ISSET(i, &(masterSet.master)))
{
masterSet.maxFd = i;
break;
}
}
}
UNLOCK(masterSet.setLock)
return 0;
}
int saveSet(fd_set* backSet, int* saveMaxFd)
{
LOCK(masterSet.setLock)
*backSet = masterSet.master;
*saveMaxFd = masterSet.maxFd;
UNLOCK(masterSet.setLock);
return 0;
}
int destroySet()
{
int test;
test = pthread_mutex_destroy(&(masterSet.setLock));
if(test != 0){errno = test; return -1;}
return 0;
}
/*---------------------------------------------------------------------*/