xref: /haiku/src/apps/expander/GenericThread.h (revision 83b1a68c52ba3e0e8796282759f694b7fdddf06d)
1 // license: public domain
2 // authors: jonas.sundstrom@kirilla.com
3 #ifndef _GENERIC_THREAD_H
4 #define _GENERIC_THREAD_H
5 
6 
7 #include <OS.h>
8 #include <Message.h>
9 
10 
11 class GenericThread {
12 public:
13 								GenericThread(
14 									const char* threadName = "generic_thread",
15 									int32 priority = B_NORMAL_PRIORITY,
16 									BMessage* message = NULL);
17 	virtual						~GenericThread(void);
18 
19 			BMessage*			GetDataStore(void);
20 			void				SetDataStore(BMessage* message);
21 
22 			status_t			Start(void);
23 			status_t			Pause(bool shouldBlock = true,
24 									bigtime_t timeout = 0);
25 			void				Quit(void);
26 			bool				IsPaused(void);
27 			bool				HasQuitBeenRequested(void);
28 
29 			status_t			Suspend(void);
30 			status_t			Resume(void);
31 			status_t			Kill(void);
32 
33 			void				ExitWithReturnValue(status_t returnValue);
34 			status_t			SetExitCallback(void (*callback)(void*),
35 									void* data);
36 			status_t			WaitForThread(status_t* exitValue);
37 
38 			status_t			Rename(char* name);
39 
40 			status_t			SendData(int32 code, void* buffer,
41 									size_t size);
42 			int32				ReceiveData(thread_id* sender, void* buffer,
43 									size_t size);
44 			bool				HasData(void);
45 
46 			status_t			SetPriority(int32 priority);
47 
48 			void				Snooze(bigtime_t delay);
49 			void				SnoozeUntil(bigtime_t delay,
50 									int timeBase = B_SYSTEM_TIMEBASE);
51 
52 			status_t			GetInfo(thread_info* info);
53 			thread_id			GetThread(void);
54 			team_id				GetTeam(void);
55 			char*				GetName(void);
56 			thread_state		GetState(void);
57 			sem_id				GetSemaphore(void);
58 			int32				GetPriority(void);
59 			bigtime_t			GetUserTime(void);
60 			bigtime_t			GetKernelTime(void);
61 			void*				GetStackBase(void);
62 			void*				GetStackEnd(void);
63 
64 protected:
65 	virtual	status_t			ThreadFunction(void);
66 	virtual	status_t			ThreadStartup(void);
67 	virtual	status_t			ExecuteUnit(void);
68 	virtual	status_t			ThreadShutdown(void);
69 
70 	virtual	void				ThreadStartupFailed(status_t status);
71 	virtual	void				ExecuteUnitFailed(status_t status);
72 	virtual	void				ThreadShutdownFailed(status_t status);
73 
74 			void				BeginUnit(void);
75 									// acquire m_execute_cycle
76 			void				EndUnit(void);
77 									// release m_execute_cycle
78 
79 		BMessage*				fThreadDataStore;
80 
81 private:
82 		static	 status_t		private_thread_function(void* pointer);
83 
84 				thread_id		fThreadId;
85 
86 				sem_id			fExecuteUnit;
87 									// acquire/relase within tread_function...
88 									// for Pause()
89 
90 				bool			fQuitRequested;
91 				bool			fThreadIsPaused;
92 };
93 
94 
95 #endif	// _GENERIC_THREAD_H
96