xref: /haiku/src/apps/expander/GenericThread.h (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
1 #ifndef __GENERIC_THREAD_H__
2 #define __GENERIC_THREAD_H__
3 
4 #include <OS.h>
5 #include <Message.h>
6 
7 class GenericThread
8 {
9 	public:
10 		GenericThread(const char * a_thread_name = "generic_thread", int32 a_priority = B_NORMAL_PRIORITY, BMessage * a_message = NULL);
11 		virtual	~GenericThread(void);
12 
13 		BMessage *	GetDataStore(void);
14 		void		SetDataStore(BMessage * a_message);
15 
16 		status_t	Start(void);
17 		status_t	Pause(bool a_do_block = TRUE, bigtime_t a_timeout = 0);
18 		void		Quit(void);
19 		bool		IsPaused(void);
20 		bool		HasQuitBeenRequested(void);
21 
22 		status_t	Suspend(void);
23 		status_t	Resume(void);
24 		status_t	Kill(void);
25 
26 		void		ExitWithReturnValue(status_t a_return_value);
27 		status_t	SetExitCallback(void(* a_callback)(void *), void * a_data);
28 		status_t	WaitForThread(status_t * a_exit_value);
29 
30 		status_t	Rename(char * a_name);
31 
32 		status_t	SendData(int32 a_code, void * a_buffer, size_t a_buffer_size);
33 		int32		ReceiveData(thread_id * a_sender, void * a_buffer, size_t a_buffer_size);
34 		bool		HasData(void);
35 
36 		status_t	SetPriority(int32 a_new_priority);
37 
38 		void		Snooze(bigtime_t a_microseconds);
39 		void		SnoozeUntil(bigtime_t a_microseconds, int a_timebase = B_SYSTEM_TIMEBASE);
40 
41 
42 		status_t		GetInfo(thread_info * a_thread_info);
43 		thread_id		GetThread(void);
44 		team_id			GetTeam(void);
45 		char	*		GetName(void);
46 		thread_state	GetState(void);
47 		sem_id			GetSemaphore(void);
48 		int32			GetPriority(void);
49 		bigtime_t		GetUserTime(void);
50 		bigtime_t		GetKernelTime(void);
51 		void	*		GetStackBase(void);
52 		void	*		GetStackEnd(void);
53 
54 	protected:
55 
56 		virtual status_t	ThreadFunction(void);
57 		virtual status_t    ThreadStartup(void);
58 		virtual status_t	ExecuteUnit(void);
59 		virtual status_t	ThreadShutdown(void);
60 
61 		virtual void		ThreadStartupFailed(status_t a_status);
62 		virtual void		ExecuteUnitFailed(status_t a_status);
63 		virtual void		ThreadShutdownFailed(status_t a_status);
64 
65 		void		BeginUnit(void);	// acquire m_execute_cycle
66 		void		EndUnit(void);	// release m_execute_cycle
67 
68 		BMessage	 *		m_thread_data_store;
69 
70 	private:
71 
72 		static status_t	private_thread_function(void * a_simple_thread_ptr);
73 
74 		thread_id		m_thread_id;
75 
76 		sem_id			m_execute_unit;	// acq./relase within tread_function.. For Pause()
77 
78 		bool			m_quit_requested;
79 		bool			m_thread_is_paused;
80 
81 };
82 
83 #endif
84