xref: /titanic_41/usr/src/lib/libast/common/sfio/vthread.h (revision 1dd08564e4a3aafe66b00aee6f222b0885346fe8)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #ifndef _VTHREAD_H
23 #define _VTHREAD_H	1
24 
25 #define VTHREAD_VERSION    20001201L
26 
27 /*	Header for the Vthread library.
28 **	Note that the macro vt_threaded may be defined
29 **	outside of vthread.h to suppress threading.
30 **
31 **	Written by Kiem-Phong Vo, kpv@research.att.com
32 */
33 
34 #include	<ast_common.h>
35 #include	<errno.h>
36 
37 /* ast doesn't do threads yet */
38 #if _PACKAGE_ast && !defined(vt_threaded)
39 #define vt_threaded     0
40 #endif
41 
42 #if !defined(vt_threaded) || (defined(vt_threaded) && vt_threaded == 1)
43 #define _may_use_threads	1
44 #else
45 #define _may_use_threads	0
46 #endif
47 #undef vt_threaded
48 
49 #if _may_use_threads && !defined(vt_threaded) && _hdr_pthread
50 #define vt_threaded		1
51 #include			<pthread.h>
52 typedef pthread_mutex_t		_vtmtx_t;
53 typedef pthread_once_t		_vtonce_t;
54 typedef pthread_t		_vtself_t;
55 typedef pthread_t		_vtid_t;
56 typedef pthread_attr_t		_vtattr_t;
57 
58 #if !defined(PTHREAD_ONCE_INIT) && defined(pthread_once_init)
59 #define PTHREAD_ONCE_INIT	pthread_once_init
60 #endif
61 
62 #endif
63 
64 #if _may_use_threads && !defined(vt_threaded) && _WIN32
65 #define vt_threaded		1
66 #include			<windows.h>
67 typedef CRITICAL_SECTION	_vtmtx_t;
68 typedef int			_vtonce_t;
69 typedef HANDLE			_vtself_t;
70 typedef DWORD			_vtid_t;
71 typedef SECURITY_ATTRIBUTES	_vtattr_t;
72 #endif
73 
74 #ifndef vt_threaded
75 #define vt_threaded		0
76 #endif
77 
78 /* common attributes for various structures */
79 #define VT_RUNNING	000000001	/* thread is running		*/
80 #define VT_SUSPENDED	000000002	/* thread is suspended		*/
81 #define VT_WAITED	000000004	/* thread has been waited	*/
82 #define VT_FREE		000010000	/* object can be freed		*/
83 #define VT_INIT		000020000	/* object was initialized	*/
84 #define VT_BITS		000030007	/* bits that we care about	*/
85 
86 /* directives for vtset() */
87 #define VT_STACK	1		/* set stack size		*/
88 
89 typedef struct _vtmutex_s	Vtmutex_t;
90 typedef struct _vtonce_s	Vtonce_t;
91 typedef struct _vthread_s	Vthread_t;
92 
93 #ifndef EINVAL
94 #define EINVAL			22
95 #endif
96 #ifndef EBUSY
97 #define EBUSY			16
98 #endif
99 #ifndef EDEADLK
100 #define EDEADLK			45
101 #endif
102 #ifndef EPERM
103 #define EPERM			1
104 #endif
105 
106 _BEGIN_EXTERNS_
107 
108 extern Vthread_t*	vtopen _ARG_((Vthread_t*, int));
109 extern int		vtclose _ARG_((Vthread_t*));
110 extern int		vtset _ARG_((Vthread_t*, int, Void_t*));
111 extern int		vtrun _ARG_((Vthread_t*, void*(*)(void*), void*));
112 extern int		vtkill _ARG_((Vthread_t*));
113 extern int		vtwait _ARG_((Vthread_t*));
114 
115 extern int		vtonce _ARG_((Vtonce_t*, void(*)() ));
116 
117 extern Vtmutex_t*	vtmtxopen _ARG_((Vtmutex_t*, int));
118 extern int		vtmtxclose _ARG_((Vtmutex_t*));
119 extern int 		vtmtxlock _ARG_((Vtmutex_t*));
120 extern int 		vtmtxtrylock _ARG_((Vtmutex_t*));
121 extern int 		vtmtxunlock _ARG_((Vtmutex_t*));
122 extern int 		vtmtxclrlock _ARG_((Vtmutex_t*));
123 
124 extern Void_t*		vtstatus _ARG_((Vthread_t*));
125 extern int		vterror _ARG_((Vthread_t*));
126 extern int		vtmtxerror _ARG_((Vtmutex_t*));
127 extern int		vtonceerror _ARG_((Vtonce_t*));
128 
129 _END_EXTERNS_
130 
131 #if vt_threaded
132 
133 /* mutex structure */
134 struct _vtmutex_s
135 {	_vtmtx_t	lock;
136 	int		count;
137 	_vtid_t		owner;
138 	int		state;
139 	int		error;
140 };
141 
142 /* structure for states of thread */
143 struct _vthread_s
144 {	_vtself_t	self;		/* self-handle		*/
145 	_vtid_t		id;		/* thread id		*/
146 	_vtattr_t	attrs;		/* attributes		*/
147 	size_t		stack;		/* stack size		*/
148 	int		state;		/* execution state	*/
149 	int		error;		/* error status 	*/
150 	Void_t*		exit;		/* exit value		*/
151 };
152 
153 /* structure for exactly once execution */
154 struct _vtonce_s
155 {	int		done;
156 	_vtonce_t	once;
157 	int		error;
158 };
159 
160 #if _WIN32
161 #define VTONCE_INITDATA		{0, 0}
162 #else
163 #define VTONCE_INITDATA		{0, PTHREAD_ONCE_INIT }
164 #endif
165 
166 #define vtstatus(vt)		((vt)->exit)
167 #define vterror(vt)		((vt)->error)
168 #define vtmtxerror(mtx)		((mtx)->error)
169 #define vtonceerror(once)	((once)->error)
170 
171 #endif /*vt_threaded*/
172 
173 /* fake structures and functions */
174 #if !vt_threaded
175 struct _vtmutex_s
176 {	int	error;
177 };
178 struct _vtattr_s
179 {	int	error;
180 };
181 struct _vthread_s
182 {	int	error;
183 };
184 struct _vtonce_s
185 {	int	error;
186 };
187 
188 typedef int		_vtmtx_t;
189 typedef int		_vtonce_t;
190 typedef int		_vtself_t;
191 typedef int		_vtid_t;
192 typedef int		_vtattr_t;
193 
194 #define VTONCE_INITDATA		{0}
195 
196 #define vtopen(vt,flgs)		((Vthread_t*)0)
197 #define vtclose(vt)		(-1)
198 #define vtkill(vt)		(-1)
199 #define vtwait(vt)		(-1)
200 #define vtrun(vt,fn,arg)	(-1)
201 
202 #define vtset(vt,t,v)		(-1)
203 #define vtonce(on,fu)		(-1)
204 
205 #define vtmtxopen(mtx,flgs)	((Vtmutex_t*)0)
206 #define vtmtxclose(mtx)		(-1)
207 #define vtmtxlock(mtx)		(-1)
208 #define vtmtxtrylock(mtx)	(-1)
209 #define vtmtxunlock(mtx)	(-1)
210 #define vtmtxclrlock(mtx)	(-1)
211 
212 #define vtstatus(vt)		((Void_t*)0)
213 #define vterror(vt)		(0)
214 #define vtmtxerror(mtx)		(0)
215 #define vtonceerror(once)	(0)
216 
217 #endif /*!vt_threaded*/
218 
219 #endif /*_VTHREAD_H*/
220