Fix bnc#472021
[mono.git] / mono / io-layer / thread-private.h
1 /*
2  * thread-private.h:  Private definitions for thread handles
3  *
4  * Author:
5  *      Dick Porter (dick@ximian.com)
6  *
7  * (C) 2002 Ximian, Inc.
8  */
9
10 #ifndef _WAPI_THREAD_PRIVATE_H_
11 #define _WAPI_THREAD_PRIVATE_H_
12
13 #include <config.h>
14 #include <glib.h>
15 #include <pthread.h>
16 #ifdef HAVE_SEMAPHORE_H
17 #include <semaphore.h>
18 #endif
19 #ifdef USE_MACH_SEMA
20 #include <mach/mach_init.h>
21 #include <mach/task.h>
22 #include <mach/semaphore.h>
23 typedef semaphore_t MonoSemType;
24 #define MONO_SEM_INIT(addr,value) semaphore_create (current_task (), (addr), SYNC_POLICY_FIFO, (value))
25 #define MONO_SEM_WAIT(sem) semaphore_wait (*(sem))
26 #define MONO_SEM_POST(sem) semaphore_signal (*(sem))
27 #define MONO_SEM_DESTROY(sem) semaphore_destroy (current_task (), *(sem))
28 #else
29 typedef sem_t MonoSemType;
30 #define MONO_SEM_INIT(addr,value) sem_init ((addr), 0, (value))
31 #define MONO_SEM_WAIT(sem) sem_wait ((sem))
32 #define MONO_SEM_POST(sem) sem_post ((sem))
33 #define MONO_SEM_DESTROY(sem) sem_destroy ((sem))
34 #endif
35
36 /* There doesn't seem to be a defined symbol for this */
37 #define _WAPI_THREAD_CURRENT (gpointer)0xFFFFFFFE
38 extern gpointer _wapi_thread_duplicate (void);
39
40 extern struct _WapiHandleOps _wapi_thread_ops;
41
42 typedef enum {
43         THREAD_STATE_START,
44         THREAD_STATE_EXITED
45 } WapiThreadState;
46
47 #define INTERRUPTION_REQUESTED_HANDLE (gpointer)0xFFFFFFFE
48
49 struct _WapiHandle_thread
50 {
51         guint32 exitstatus;
52         WapiThreadState state : 2;
53         guint joined : 1;
54         guint has_apc : 1;
55         guint32 create_flags;
56         /* Fields below this point are only valid for the owning process */
57         pthread_t id;
58         GPtrArray *owned_mutexes;
59         gpointer handle;
60         /* 
61      * Handle this thread waits on. If this is INTERRUPTION_REQUESTED_HANDLE,
62          * it means the thread is interrupted by another thread, and shouldn't enter
63          * a wait.
64          * This also acts as a reference for the handle.
65          */
66         gpointer wait_handle;
67         MonoSemType suspend_sem;
68         guint32 (*start_routine)(gpointer arg);
69         gpointer start_arg;
70 };
71
72 typedef struct
73 {
74         guint32 (*callback)(gpointer arg);
75         gpointer param;
76 } ApcInfo;
77
78 extern gboolean _wapi_thread_apc_pending (gpointer handle);
79 extern gboolean _wapi_thread_cur_apc_pending (void);
80 extern gboolean _wapi_thread_dispatch_apc_queue (gpointer handle);
81 extern void _wapi_thread_own_mutex (gpointer mutex);
82 extern void _wapi_thread_disown_mutex (gpointer mutex);
83 extern gpointer _wapi_thread_handle_from_id (pthread_t tid);
84 extern void _wapi_thread_set_termination_details (gpointer handle,
85                                                   guint32 exitstatus);
86 extern void _wapi_thread_cleanup (void);
87
88 #endif /* _WAPI_THREAD_PRIVATE_H_ */