1 /** @file thread.h
2  * Threading API.
3  */
4 
5 #ifndef	RAZORBACK_THREAD_H
6 #define	RAZORBACK_THREAD_H
7 
8 #include <razorback/visibility.h>
9 #include <razorback/types.h>
10 #include <razorback/lock.h>
11 #include <razorback/api.h>
12 #ifdef _MSC_VER
13 typedef DWORD rzb_thread_t;
14 #else //_MSC_VER
15 #include <pthread.h>
16 typedef pthread_t rzb_thread_t;
17 #endif //_MSC_VER
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 /** Thread
24  * Purpose:	hold the information about a thread
25  */
26 struct Thread
27 {
28 #ifdef _MSC_VER
29 	HANDLE hThread;
30 #endif //_MSC_VER
31     rzb_thread_t iThread;       			///< pthread Thread info.
32     struct Mutex * mMutex;  				///< mutex protecting this struct
33     bool bRunning;              			///< true if executing, false if not:  must be managed explicitly by thread function
34     void *pUserData;						///< Additional info for the thread
35     char *sName;            				///< The thread name
36     struct RazorbackContext *pContext; 		///< The Thread Context
37     void (*mainFunction) (struct Thread *); ///< Thread Main Function
38     bool bShutdown;         				///< Shutdown Flag
39     int refs;								///< Reference count
40     //void (*interrupt)(struct Thread *);		///< Cancellation handler for a blocking function.
41 };
42 
43 /** Create a new thread
44  * @param *function The function the thread will execute
45  * @param *userData The thread user data
46  * @param *name The name of the thread
47  * @param *context The initial context of the thread
48  * @return Null on error a new Thread on success.
49  */
50 SO_PUBLIC extern struct Thread *Thread_Launch (void (*function) (struct Thread *),
51                                      void *userData, char *name,
52                                      struct RazorbackContext *context);
53 
54 /** Change the registered context of a running thread.
55  * @param thread the thread to change
56  * @param context the new context
57  * @return The old context
58  */
59 SO_PUBLIC extern struct RazorbackContext * Thread_ChangeContext(struct Thread *thread,
60                                     struct RazorbackContext *context);
61 
62 /** Get the registered context of a running thread.
63  * @param thread the thread to change
64  * @return The current context for the thread.
65  */
66 SO_PUBLIC extern struct RazorbackContext * Thread_GetContext(struct Thread *p_pThread);
67 
68 /** Get the running context for the current thread.
69  * @return The current context.
70  */
71 SO_PUBLIC extern struct RazorbackContext * Thread_GetCurrentContext(void);
72 
73 /** Destroy a threads data
74  * @param *thread The thread to destroy
75  */
76 SO_PUBLIC extern void Thread_Destroy (struct Thread *thread);
77 
78 /** Checks whether a thread is running or not
79  * @param *thread The thread the test
80  * @return true if running, false if not
81  */
82 SO_PUBLIC extern bool Thread_IsRunning (struct Thread *thread);
83 
84 /** Check if a thread has finished.
85  * @param thread The thread to test.
86  * @return true if the thread has exited, false if its still running.
87  */
88 SO_PUBLIC extern bool Thread_IsStopped (struct Thread *thread);
89 
90 /** Suspend execution of the calling thread until the target thread therminates.
91  * @param thread The target thread.
92  */
93 SO_PUBLIC extern void Thread_Join(struct Thread *thread);
94 /** Interrupt the target thread.
95  * @param thread The target thread.
96  */
97 SO_PUBLIC extern void Thread_Interrupt(struct Thread *thread);
98 /** Stop the target thread.
99  * @param thread The target thread.
100  */
101 SO_PUBLIC extern void Thread_Stop (struct Thread *thread);
102 SO_PUBLIC extern void Thread_StopAndJoin (struct Thread *thread);
103 SO_PUBLIC extern void Thread_InterruptAndJoin (struct Thread *thread);
104 /** Cause the current thread to yield execution to other runnable threads.
105  */
106 SO_PUBLIC extern void Thread_Yield(void);
107 
108 /** Get the number of currently running threads.
109  * @return the number of currently running threads.
110  */
111 SO_PUBLIC extern uint32_t Thread_getCount (void);
112 
113 /** Get the current thread.
114  */
115 SO_PUBLIC extern struct Thread *Thread_GetCurrent(void);
116 /** Get the current thread ID.
117  */
118 SO_PUBLIC extern rzb_thread_t Thread_GetCurrentId(void);
119 
120 SO_PUBLIC extern int Thread_KeyCmp(void *a, void *id);
121 SO_PUBLIC extern int Thread_Cmp(void *a, void *b);
122 
123 #ifdef __cplusplus
124 }
125 #endif
126 #endif // RAZORBACK_THREAD_H
127