xref: /onnv-gate/usr/src/lib/libast/common/sfio/vthread.h (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #ifndef _VTHREAD_H
234887Schin #define _VTHREAD_H	1
244887Schin 
254887Schin #define VTHREAD_VERSION    20001201L
264887Schin 
274887Schin /*	Header for the Vthread library.
284887Schin **	Note that the macro vt_threaded may be defined
294887Schin **	outside of vthread.h to suppress threading.
304887Schin **
314887Schin **	Written by Kiem-Phong Vo, kpv@research.att.com
324887Schin */
334887Schin 
344887Schin #include	<ast_common.h>
354887Schin #include	<errno.h>
364887Schin 
374887Schin /* ast doesn't do threads yet */
384887Schin #if _PACKAGE_ast && !defined(vt_threaded)
394887Schin #define vt_threaded     0
404887Schin #endif
414887Schin 
424887Schin #if !defined(vt_threaded) || (defined(vt_threaded) && vt_threaded == 1)
434887Schin #define _may_use_threads	1
444887Schin #else
454887Schin #define _may_use_threads	0
464887Schin #endif
474887Schin #undef vt_threaded
484887Schin 
494887Schin #if _may_use_threads && !defined(vt_threaded) && _hdr_pthread
504887Schin #define vt_threaded		1
514887Schin #include			<pthread.h>
524887Schin typedef pthread_mutex_t		_vtmtx_t;
534887Schin typedef pthread_once_t		_vtonce_t;
544887Schin typedef pthread_t		_vtself_t;
554887Schin typedef pthread_t		_vtid_t;
564887Schin typedef pthread_attr_t		_vtattr_t;
574887Schin 
584887Schin #if !defined(PTHREAD_ONCE_INIT) && defined(pthread_once_init)
594887Schin #define PTHREAD_ONCE_INIT	pthread_once_init
604887Schin #endif
614887Schin 
624887Schin #endif
634887Schin 
644887Schin #if _may_use_threads && !defined(vt_threaded) && _WIN32
654887Schin #define vt_threaded		1
664887Schin #include			<windows.h>
674887Schin typedef CRITICAL_SECTION	_vtmtx_t;
684887Schin typedef int			_vtonce_t;
694887Schin typedef HANDLE			_vtself_t;
704887Schin typedef DWORD			_vtid_t;
714887Schin typedef SECURITY_ATTRIBUTES	_vtattr_t;
724887Schin #endif
734887Schin 
744887Schin #ifndef vt_threaded
754887Schin #define vt_threaded		0
764887Schin #endif
774887Schin 
784887Schin /* common attributes for various structures */
794887Schin #define VT_RUNNING	000000001	/* thread is running		*/
804887Schin #define VT_SUSPENDED	000000002	/* thread is suspended		*/
814887Schin #define VT_WAITED	000000004	/* thread has been waited	*/
824887Schin #define VT_FREE		000010000	/* object can be freed		*/
834887Schin #define VT_INIT		000020000	/* object was initialized	*/
844887Schin #define VT_BITS		000030007	/* bits that we care about	*/
854887Schin 
864887Schin /* directives for vtset() */
874887Schin #define VT_STACK	1		/* set stack size		*/
884887Schin 
894887Schin typedef struct _vtmutex_s	Vtmutex_t;
904887Schin typedef struct _vtonce_s	Vtonce_t;
914887Schin typedef struct _vthread_s	Vthread_t;
924887Schin 
934887Schin #ifndef EINVAL
944887Schin #define EINVAL			22
954887Schin #endif
964887Schin #ifndef EBUSY
974887Schin #define EBUSY			16
984887Schin #endif
994887Schin #ifndef EDEADLK
1004887Schin #define EDEADLK			45
1014887Schin #endif
1024887Schin #ifndef EPERM
1034887Schin #define EPERM			1
1044887Schin #endif
1054887Schin 
1064887Schin _BEGIN_EXTERNS_
1074887Schin 
1084887Schin extern Vthread_t*	vtopen _ARG_((Vthread_t*, int));
1094887Schin extern int		vtclose _ARG_((Vthread_t*));
1104887Schin extern int		vtset _ARG_((Vthread_t*, int, Void_t*));
1114887Schin extern int		vtrun _ARG_((Vthread_t*, void*(*)(void*), void*));
1124887Schin extern int		vtkill _ARG_((Vthread_t*));
1134887Schin extern int		vtwait _ARG_((Vthread_t*));
1144887Schin 
1154887Schin extern int		vtonce _ARG_((Vtonce_t*, void(*)() ));
1164887Schin 
1174887Schin extern Vtmutex_t*	vtmtxopen _ARG_((Vtmutex_t*, int));
1184887Schin extern int		vtmtxclose _ARG_((Vtmutex_t*));
1194887Schin extern int 		vtmtxlock _ARG_((Vtmutex_t*));
1204887Schin extern int 		vtmtxtrylock _ARG_((Vtmutex_t*));
1214887Schin extern int 		vtmtxunlock _ARG_((Vtmutex_t*));
1224887Schin extern int 		vtmtxclrlock _ARG_((Vtmutex_t*));
1234887Schin 
1244887Schin extern Void_t*		vtstatus _ARG_((Vthread_t*));
1254887Schin extern int		vterror _ARG_((Vthread_t*));
1264887Schin extern int		vtmtxerror _ARG_((Vtmutex_t*));
1274887Schin extern int		vtonceerror _ARG_((Vtonce_t*));
1284887Schin 
1294887Schin _END_EXTERNS_
1304887Schin 
1314887Schin #if vt_threaded
1324887Schin 
1334887Schin /* mutex structure */
1344887Schin struct _vtmutex_s
1354887Schin {	_vtmtx_t	lock;
1364887Schin 	int		count;
1374887Schin 	_vtid_t		owner;
1384887Schin 	int		state;
1394887Schin 	int		error;
1404887Schin };
1414887Schin 
1424887Schin /* structure for states of thread */
1434887Schin struct _vthread_s
1444887Schin {	_vtself_t	self;		/* self-handle		*/
1454887Schin 	_vtid_t		id;		/* thread id		*/
1464887Schin 	_vtattr_t	attrs;		/* attributes		*/
1474887Schin 	size_t		stack;		/* stack size		*/
1484887Schin 	int		state;		/* execution state	*/
1494887Schin 	int		error;		/* error status 	*/
1504887Schin 	Void_t*		exit;		/* exit value		*/
1514887Schin };
1524887Schin 
1534887Schin /* structure for exactly once execution */
1544887Schin struct _vtonce_s
1554887Schin {	int		done;
1564887Schin 	_vtonce_t	once;
1574887Schin 	int		error;
1584887Schin };
1594887Schin 
1604887Schin #if _WIN32
1614887Schin #define VTONCE_INITDATA		{0, 0}
1624887Schin #else
1634887Schin #define VTONCE_INITDATA		{0, PTHREAD_ONCE_INIT }
1644887Schin #endif
1654887Schin 
1664887Schin #define vtstatus(vt)		((vt)->exit)
1674887Schin #define vterror(vt)		((vt)->error)
1684887Schin #define vtmtxerror(mtx)		((mtx)->error)
1694887Schin #define vtonceerror(once)	((once)->error)
1704887Schin 
1714887Schin #endif /*vt_threaded*/
1724887Schin 
1734887Schin /* fake structures and functions */
1744887Schin #if !vt_threaded
1754887Schin struct _vtmutex_s
1764887Schin {	int	error;
1774887Schin };
1784887Schin struct _vtattr_s
1794887Schin {	int	error;
1804887Schin };
1814887Schin struct _vthread_s
1824887Schin {	int	error;
1834887Schin };
1844887Schin struct _vtonce_s
1854887Schin {	int	error;
1864887Schin };
1874887Schin 
1884887Schin typedef int		_vtmtx_t;
1894887Schin typedef int		_vtonce_t;
1904887Schin typedef int		_vtself_t;
1914887Schin typedef int		_vtid_t;
1924887Schin typedef int		_vtattr_t;
1934887Schin 
1944887Schin #define VTONCE_INITDATA		{0}
1954887Schin 
1964887Schin #define vtopen(vt,flgs)		((Vthread_t*)0)
1974887Schin #define vtclose(vt)		(-1)
1984887Schin #define vtkill(vt)		(-1)
1994887Schin #define vtwait(vt)		(-1)
2004887Schin #define vtrun(vt,fn,arg)	(-1)
2014887Schin 
2024887Schin #define vtset(vt,t,v)		(-1)
2034887Schin #define vtonce(on,fu)		(-1)
2044887Schin 
2054887Schin #define vtmtxopen(mtx,flgs)	((Vtmutex_t*)0)
2064887Schin #define vtmtxclose(mtx)		(-1)
2074887Schin #define vtmtxlock(mtx)		(-1)
2084887Schin #define vtmtxtrylock(mtx)	(-1)
2094887Schin #define vtmtxunlock(mtx)	(-1)
2104887Schin #define vtmtxclrlock(mtx)	(-1)
2114887Schin 
2124887Schin #define vtstatus(vt)		((Void_t*)0)
2134887Schin #define vterror(vt)		(0)
2144887Schin #define vtmtxerror(mtx)		(0)
2154887Schin #define vtonceerror(once)	(0)
2164887Schin 
2174887Schin #endif /*!vt_threaded*/
2184887Schin 
2194887Schin #endif /*_VTHREAD_H*/
220