xref: /haiku/src/servers/registrar/ShutdownProcess.h (revision e6b30aee0fd7a23d6a6baab9f3718945a0cd838a)
1 /*
2  * Copyright 2005, Ingo Weinhold, bonefish@users.sf.net.
3  * Distributed under the terms of the MIT License.
4  *
5  * Manages the shutdown process.
6  */
7 #ifndef SHUTDOWN_PROCESS_H
8 #define SHUTDOWN_PROCESS_H
9 
10 #include <hash_set>
11 
12 #include <Looper.h>
13 
14 #include "AppInfoList.h"
15 #include "EventMaskWatcher.h"
16 #include "RosterAppInfo.h"
17 
18 #if __GNUC__ >= 4
19 using __gnu_cxx::hash_set;
20 #endif
21 
22 
23 class EventQueue;
24 class TRoster;
25 
26 // Note: EventMaskWatcher is inherited public due to a gcc bug/C++ feature:
27 // Once cast into a Watcher dynamic_cast<>()ing it back into an
28 // EventMaskWatcher fails otherwise.
29 class ShutdownProcess : public BLooper, public EventMaskWatcher {
30 public:
31 	ShutdownProcess(TRoster *roster, EventQueue *eventQueue);
32 	~ShutdownProcess();
33 
34 	status_t Init(BMessage *request);
35 
36 	virtual void MessageReceived(BMessage *message);
37 
38 	static void SendReply(BMessage *request, status_t error);
39 
40 private:
41 	void _SendReply(status_t error);
42 
43 	void _NegativeQuitRequestReply(thread_id thread);
44 
45 	void _PrepareShutdownMessage(BMessage &message) const;
46 	status_t _ShutDown();
47 
48 	status_t _PushEvent(uint32 eventType, team_id team, int32 phase);
49 	status_t _GetNextEvent(uint32 &eventType, team_id &team, int32 &phase,
50 		bool block);
51 
52 	void _SetPhase(int32 phase);
53 	void _ScheduleTimeoutEvent(bigtime_t timeout, team_id team = -1);
54 
55 	bool _LockAppLists();
56 	void _UnlockAppLists();
57 
58 	void _InitShutdownWindow();
59 	void _SetShowShutdownWindow(bool show);
60 	void _AddShutdownWindowApps(AppInfoList &infos);
61 	void _RemoveShutdownWindowApp(team_id team);
62 	void _SetShutdownWindowCurrentApp(team_id team);
63 	void _SetShutdownWindowText(const char *text);
64 	void _SetShutdownWindowCancelButtonEnabled(bool enabled);
65 	void _SetShutdownWindowKillButtonEnabled(bool enabled);
66 	void _SetShutdownWindowWaitForShutdown();
67 	void _SetShutdownWindowWaitForAbortedOK();
68 
69 	static status_t _WorkerEntry(void *data);
70 	status_t _Worker();
71 
72 	void _WorkerDoShutdown();
73 	bool _WaitForApp(team_id team, AppInfoList *list, bool systemApps);
74 	void _QuitApps(AppInfoList &list, bool systemApps);
75 	void _QuitBackgroundApps();
76 	void _WaitForBackgroundApps();
77 	void _KillBackgroundApps();
78 	void _QuitNonApps();
79 	void _QuitBlockingApp(AppInfoList &list, team_id team, const char *appName,
80 		bool cancelAllowed);
81 	void _DisplayAbortingApp(team_id team);
82 	void _WaitForDebuggedTeams();
83 
84 private:
85 	class TimeoutEvent;
86 	class InternalEvent;
87 	struct InternalEventList;
88 	class QuitRequestReplyHandler;
89 	class ShutdownWindow;
90 
91 	friend class QuitRequestReplyHandler;
92 
93 	BLocker					fWorkerLock;	// protects fields shared by looper
94 											// and worker
95 	BMessage				*fRequest;
96 	TRoster					*fRoster;
97 	EventQueue				*fEventQueue;
98 	hash_set<team_id>		fVitalSystemApps;
99 	AppInfoList				fSystemApps;
100 	AppInfoList				fUserApps;
101 	AppInfoList				fBackgroundApps;
102 	hash_set<team_id>		fDebuggedTeams;
103 	TimeoutEvent			*fTimeoutEvent;
104 	InternalEventList		*fInternalEvents;
105 	sem_id					fInternalEventSemaphore;
106 	QuitRequestReplyHandler	*fQuitRequestReplyHandler;
107 	thread_id				fWorker;
108 	int32					fCurrentPhase;
109 	status_t				fShutdownError;
110 	bool					fHasGUI;
111 	bool					fReboot;
112 	bool					fRequestReplySent;
113 	ShutdownWindow			*fWindow;
114 };
115 
116 #endif	// SHUTDOWN_PROCESS_H
117