File: wait.cc

package info (click to toggle)
crossroads 2.65-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,664 kB
  • ctags: 355
  • sloc: cpp: 4,212; perl: 1,658; xml: 269; makefile: 186; sh: 46
file content (79 lines) | stat: -rw-r--r-- 2,062 bytes parent folder | download | duplicates (2)
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
#include "fdset"

void Fdset::wait(bool wait_read, bool wait_write) {
    PROFILE("Fdset::wait");

    struct timeval tv, *tvp;

    // No fd's? Nothing to wait for.
    if (set.size() < 1)
	throw Error("Internal jam in Fdset::wait(): no fd's to wait for");

    // Prepare select sets.
    FD_ZERO (&readset);
    FD_ZERO (&writeset);
    FD_ZERO (&exceptset);
    for (unsigned i = 0; i < set.size(); i++) {
	FD_SET (set[i], &readset);
	FD_SET (set[i], &writeset);
	FD_SET (set[i], &exceptset);
	debugmsg(Mstr("About to wait for fd ") + Mstr(set[i]) + "\n");
    }

    // Prepare timout specifier.
    if (tsec) {
	tv.tv_sec = tsec;
	tv.tv_usec = 0;
	tvp = &tv;
	debugmsg(Mstr("Waiting limitation: ") + Mstr(tsec) + "\n");
    } else {
	tvp = 0;
	debugmsg(Mstr("No waiting limitation\n"));
    }

    // Run the select.
    if (select (FD_SETSIZE,
		wait_read ? &readset : 0,
		wait_write ? &writeset : 0,
		&exceptset, tvp) < 0) {
	if (errno != EINTR)
	    throw Error(string("Select failure: failed to wait: ") +
			strerror(errno));
	FD_ZERO(&readset);
	FD_ZERO(&writeset);
	FD_ZERO(&exceptset);
	return;
    }

    // Check for exceptions.
    for (unsigned i = 0; i < set.size(); i++)
	if (FD_ISSET (set[i], &exceptset)) {
	    ostringstream o;
	    o << "Exception on fd/socket " << int(set[i]);
	    throw Error(o.str());
	}

    // More debugging: What has become readable, what has become
    // writeable, also state if no change was seen
    if (config.debug()) {
	bool statechanged = false;
	for (unsigned int i = 0; i < FD_SETSIZE; i++) {
	    if (FD_ISSET(i, &readset)) {
		_debugmsg(Mstr("Fd ") + Mstr(i) + " is readable\n");
		statechanged = true;
	    }
	    if (FD_ISSET(i, &writeset)) {
		_debugmsg(Mstr("Fd ") + Mstr(i) + " is writeable\n");
		statechanged = true;
	    }
	}
	if (!statechanged) {
	    ostringstream o;
	    o << "Select timeout: neither of the fd's ";
	    for (unsigned int i = 0; i < set.size(); i++)
		o << set[i] << ' ';
	    o << "has shown activity in " << tsec << " sec\n";
	    _debugmsg(o.str());
	}	    
    }
}