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