~donmcc/ood

ref: 1182412fd4ece0cb55c4b3ebefee78b22c8c15bd ood/src/libood/ood/select_sets.c -rw-r--r-- 1.9 KiB
1182412fDon McCaughey Add `ood_halt()` function. 7 months ago
                                                                                
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
#include "select_sets.h"

#include <assert.h>
#include <errno.h>
#include "ood.h"


void
ood_select_sets_clear(struct ood_select_sets *select_sets)
{
    assert(select_sets);
    select_sets->max_fd = 0;
    FD_ZERO(&select_sets->read);
    FD_ZERO(&select_sets->write);
    FD_ZERO(&select_sets->error);
}


void
ood_select_sets_add_errorable(struct ood_select_sets *select_sets, int socket)
{
    select_sets->max_fd = ood_max(select_sets->max_fd, socket);
    FD_SET(socket, &select_sets->error);
}


void
ood_select_sets_add_reader(struct ood_select_sets *select_sets, int socket)
{
    select_sets->max_fd = ood_max(select_sets->max_fd, socket);
    FD_SET(socket, &select_sets->read);
}


void
ood_select_sets_add_writer(struct ood_select_sets *select_sets, int socket)
{
    select_sets->max_fd = ood_max(select_sets->max_fd, socket);
    FD_SET(socket, &select_sets->write);
}


bool
ood_select_sets_has_error(struct ood_select_sets *select_sets, int socket)
{
    return FD_ISSET(socket, &select_sets->error);
}


bool
ood_select_sets_is_readable(struct ood_select_sets *select_sets, int socket)
{
    return FD_ISSET(socket, &select_sets->read);
}


bool
ood_select_sets_is_writable(struct ood_select_sets *select_sets, int socket)
{
    return FD_ISSET(socket, &select_sets->write);
}


enum ood_result
ood_select_sets_select(struct ood_select_sets *select_sets, unsigned timout_millis, bool *interrupted_out)
{
    int end_fd = select_sets->max_fd + 1;
    struct timeval timeout = {
            .tv_sec=0,
            .tv_usec=timout_millis * 1000,
    };
    int result = select(end_fd, &select_sets->read, &select_sets->write, &select_sets->error, &timeout);
    if (result >= 0) {
        *interrupted_out = false;
        return ood_okay;
    }
    if (EINTR == errno) {
        *interrupted_out = true;
        return ood_okay;
    } else {
        *interrupted_out = false;
        return ood_error;
    }
}