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