summaryrefslogtreecommitdiff
path: root/comm/Scheduler.h
blob: d465006d42ddda172640184137e97a2198dc3e45 (plain)
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
/*
 *	Ivy League
 *
 *	Channel sets, or multiplexers, or schedulers
 *
 *	Copyright 1990-2000
 *	Laboratoire de Recherche en Informatique (LRI)
 *	Centre d'Etudes de la Navigation Aerienne (CENA)
 *
 *	original code by Michel Beaudouin-Lafon,
 *	heavily modified by Stephane Chatty and Stephane Sire
 *
 *	$Id$
 *
 */


#ifndef IvlScheduler_H_
#define IvlScheduler_H_

#include "ivl/Loop.h"
#include "Channel.h"
#include <sys/types.h>
#include "ivl/Timer.h"

extern	char*	StrReprBuf;

//	This class defines channel sets for multiplexing i/o
//	Because of the coercion defined for IvlFd -> int,
//	most arguments of type ints can be IvlFd, IvlChannel, ...
//	An array of pointers to channels is kept in the object;
//	The masks for reading/writing are kept consistent with the IOMode of
//	the channels, as long as it is not changed by a direct access to the channel
//	operator[] returns a pointer and cannot be used as an lhs of an assignment.
//

//     WARNING: when the Scheduler is deleted, the Channels it still
//     have in its channel set are not Removed; instead IvlChannel::Removed
//     is called 


class IvlBaseScheduler : public IvlBaseMultiplexer {
friend	class	IvlBaseTimeOut;
friend	class	IvlBaseScheduledHandler;
friend	class	IvlChannel;
friend	MPX_RES	IvlLoop ();

protected:
	IvlChannel**	Channels;
	IvlTimerSet	Timers;
	short		ReadCount;
	short		WriteCount;
	short		SelectCount;
	bool		Looping;
	bool	SigFired;
	IvlBaseScheduledHandler**	Handlers;
	int*	NbSignals;

	bool	Add (IvlChannel*);
	bool	Remove (int);
	void	HandleSignal (IvlBaseScheduledHandler&);
	void	HandleDeferredSignals ();
inline	IvlTimerSet*	GetTimerSet ()	{ return &Timers; }

virtual	void	SetMasks (int, IOMODE);
virtual	void	SetTimeOut (Millisecond) = 0;
virtual	void	SuppressTimeOut () = 0;
virtual	void	AddSignalHook ();

public:
		IvlBaseScheduler ();
		~IvlBaseScheduler ();
	
	IvlChannel*	operator [] (int);

	void	RemoveAll ();
	void	SetMode (int, IOMODE);
	MPX_RES	Run ();

	void	Abort ();
	void	Close ();
};

class IvlScheduler : public IvlBaseScheduler {
protected:
	fd_set	ReadMask;
	fd_set	WriteMask;
	fd_set	SelectMask;
	IvlList	Hooks;
	IvlList	FinalHooks;
	bool	Looping;
	Millisecond	TimeOut;

	void	SetMasks (int, IOMODE);
	void	SetTimeOut (Millisecond);
	void	SuppressTimeOut ();
	MPX_RES	Loop ();
	void	ExecHooks (bool = false);

public:
		IvlScheduler ();
		~IvlScheduler ();
	
	MPX_RES	LoopScan (bool nointr = true);
	int	Scan (bool nointr = true, bool poll = false);
	void	Stop ();
	char*	StrRepr (char* = StrReprBuf);
	void	AddHook (IvlMpxHook*, bool = false);
	void	RemoveHook (IvlMpxHook*, bool = false);
	void	AddFinalHook (IvlMpxHook*);
	void	RemoveFinalHook (IvlMpxHook*);
};

extern IvlBaseScheduler* IvlScd;
extern void IvlOpen (IvlBaseScheduler* = 0);

#if 0 /* rendu inutile par la fusion avec DNN ? */
extern MPX_RES IvlLoop ();
extern void IvlStop ();
#endif

#endif /* IvlScheduler_H_ */