File: selectloop.cc

package info (click to toggle)
apt 0.3.10slink11
  • links: PTS
  • area: main
  • in suites: slink
  • size: 2,136 kB
  • ctags: 2,891
  • sloc: cpp: 22,235; sh: 1,656; makefile: 338; perl: 209
file content (268 lines) | stat: -rw-r--r-- 7,025 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// -*- mode: cpp; mode: fold -*-
// Description								/*{{{*/
// $Id: selectloop.cc,v 1.6 1998/09/12 02:46:39 jgg Exp $
/* ######################################################################
   
   Select Loop - Class to allow mulitple sockets to be monitored at once
   SelectLoop::Fd - Selectable file descriptor

   The select loop does only minor things to ensure that the methods
   can operate properly in the multi-threaded environment. It is up to the
   method to lock the widget library when it begins processing events and
   to unlock it when it is done. The lock can be extremely wide, that
   doesn't really matter.
   
   The method setup function must also lock the library as it might be
   called from a concurrent configuration.
   
   ###################################################################### */
									/*}}}*/
// Includes								/*{{{*/
#ifdef __GNUG__
#pragma implementation "deity/selectloop.h"
#endif  
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <deity/selectloop.h>

									/*}}}*/

// Signal Stuff								/*{{{*/
// ---------------------------------------------------------------------
/* */
SelectLoop::Sig SelectLoop::Signals[10] = {{0,0},{0,0},{0,0},{0,0},{0,0},
                                           {0,0},{0,0},{0,0},{0,0},{0,0}};
SelectLoop::SigHandler SelectLoop::DeathHandlers[10] = {0,0,0,0,0,0,0,0,0,0};

bool SelectLoop::Triged = false;

void SelectLoop::SignalHandler(int Signal)
{   
   int I = 0;
   for (; I != 10 && Signals[I].Signal != 0 && Signals[I].Signal != Signal; I++);
   if (Signals[I].Signal == Signal)
   {
      Signals[I].Handler(Signal);
      SelectLoop::Triged = true;
   }
}
									/*}}}*/
// SelectLoop::DeathHandler - Handle death signals			/*{{{*/
// ---------------------------------------------------------------------
/* We allow interested partied to clean up their mess and then exit */
void SelectLoop::DeathHandler(int Signal)
{
   for (SigHandler *I = DeathHandlers; I != DeathHandlers + 10; I++)
      if (*I != 0)
	 (**I)(Signal);
   
   // Send the signal to the default handler
   kill(getpid(),Signal);
}
									/*}}}*/
// SelectLoop::SelectLoop - Constructor					/*{{{*/
// ---------------------------------------------------------------------
/* */
SelectLoop::SelectLoop() : List(0), Quit(false)
{
   // Setup the death signal handler
   struct sigaction Act;
   memset(&Act,0,sizeof(Act));
   Act.sa_handler = DeathHandler;
   sigemptyset(&Act.sa_mask);
   Act.sa_flags = SA_RESETHAND;
   sigaction(SIGINT,&Act,0);
   sigaction(SIGSEGV,&Act,0);   
}
									/*}}}*/
// SelectLoop::~SelectLoop - Destructor					/*{{{*/
// ---------------------------------------------------------------------
/* */
SelectLoop::~SelectLoop()
{
   for (Fd *I = List; I != 0;)
   {
      Fd *Tmp = I;
      I = I->Next;
      delete Tmp;
   }   
}
									/*}}}*/
// SelectLoop::Loop - Main select loop					/*{{{*/
// ---------------------------------------------------------------------
/* */
bool SelectLoop::Loop()
{
   Quit = false;
   
   // Configure the select loop.
   int n = 0;
   fd_set rfds;
   fd_set wfds;
   fd_set efds;
   
   // Blank the FD arrays
   FD_ZERO(&rfds);
   FD_ZERO(&wfds);
   FD_ZERO(&efds);

   // Prepare the methods
   for (Fd *I = List; I != 0; I = I->Next)
      I->Setup(*this);

   sigset_t Set;
   sigemptyset(&Set);
   bool UseSignals = false;
   for (int I = 0; I != 10 && Signals[I].Signal != 0; I++)
   {
      UseSignals = true;
      sigaddset(&Set,Signals[I].Signal);
   }
   
   while (List != 0 && Quit == false)
   {
      // Select the proper FDs
      Fd **Last = &List;
      if (UseSignals == true)
	 sigprocmask(SIG_BLOCK,&Set,0);
      bool iTriged = Triged;
      Triged = false;
      if (UseSignals == true)
	 sigprocmask(SIG_UNBLOCK,&Set,0);
      for (Fd *I = List; I != 0;)
      {
	 if (iTriged == true && I->SignalFlag == true)
	    I->Signal(*this);
	 
	 if (I->FileDesc < 0)
	 {
	    *Last = I->Next;
	    delete I;
	    I = *Last;
	    continue;
	 }
	 
	 if (I->ReadFlag == true)
	    FD_SET(I->FileDesc,&rfds);
	 if (I->WriteFlag == true)
	    FD_SET(I->FileDesc,&wfds);
	 if (I->ErrorFlag == true)
	    FD_SET(I->FileDesc,&efds);
	 if (I->FileDesc > n)
	    n = I->FileDesc;

	 Last = &I->Next;
	 I = I->Next;	 
      }

      // Wait
      if (Triged == true || select(n+1,&rfds,&wfds,&efds,0) <= 0)
      {
	 if (errno != EINTR && Triged == false)
	    return false;
	 
	 FD_ZERO(&rfds);
	 FD_ZERO(&wfds);
	 FD_ZERO(&efds);
      }      

      // Run processors
      if (UseSignals == true)
	 sigprocmask(SIG_BLOCK,&Set,0);
      iTriged = Triged;
      Triged = false;
      if (UseSignals == true)
	 sigprocmask(SIG_UNBLOCK,&Set,0);
      for (Fd *I = List; I != 0;)
      {
	 if (iTriged == true && I->SignalFlag == true)
	    I->Signal(*this);
	 
	 if (I->FileDesc < 0)
	    continue;
	 
	 bool Keep = true;
	 if (FD_ISSET(I->FileDesc,&rfds))
	    Keep &= I->Read(*this);
	 if (FD_ISSET(I->FileDesc,&wfds))
	    Keep &= I->Write(*this);
	 if (FD_ISSET(I->FileDesc,&efds))
	    Keep &= I->Error(*this);

	 if (Keep == false)
	 {
	    *Last = I->Next;
	    delete I;
	    I = *Last;
	 }
	 else
	 {
	    Last = &I->Next;
	    I = I->Next;
	 }	 
      }   
   }
   return true;
}
									/*}}}*/
// SelectLoop::Add - Adds a new FD to be monitored			/*{{{*/
// ---------------------------------------------------------------------
/* */
void SelectLoop::Add(Fd *FD)
{
   FD->Next = List;
   List = FD;
}
									/*}}}*/
// SelectLoop::AddSignal - Add a new signal handler			/*{{{*/
// ---------------------------------------------------------------------
/* It probably would be bad if a signal occured during this. */
void SelectLoop::AddSignal(int Sig,SelectLoop::SigHandler Handler)
{
   int I = 0;
   for (; I != 10 && Signals[I].Signal != 0; I++);
   if (I >= 10)
      return;
   Signals[I].Handler = Handler;
   Signals[I].Signal = Sig;

   struct sigaction Act;
   memset(&Act,0,sizeof(Act));
   Act.sa_handler = SignalHandler;
   sigemptyset(&Act.sa_mask);
   Act.sa_flags = 0;
   sigaction(Sig,&Act,0);
}
									/*}}}*/
// SelectLoop::AddDeathHandler - Add a death signal handler		/*{{{*/
// ---------------------------------------------------------------------
/* Add a new death handler to the chain, if the list is full things
   silently fail. */
void SelectLoop::AddDeathHandler(SigHandler Handler)
{
   for (SigHandler *I = DeathHandlers; I != DeathHandlers + 10; I++)
   {
      if (*I == 0)
      {
	 *I = Handler;
	 return;
      }
   }   
}
									/*}}}*/
// SelectLoop::Fd::Fd - Constructor					/*{{{*/
// ---------------------------------------------------------------------
/* */
SelectLoop::Fd::Fd(int Fd,bool Read,bool Write,bool Error,bool Signal) :
                  ReadFlag(Read), WriteFlag(Write), ErrorFlag(Error),
                  SignalFlag(Signal)
{
   this->FileDesc  = Fd;
}
									/*}}}*/