1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
21 ***********************************************************************/
25 #define VTHREAD_VERSION 20001201L
27 /* Header for the Vthread library.
28 ** Note that the macro vt_threaded may be defined
29 ** outside of vthread.h to suppress threading.
31 ** Written by Kiem-Phong Vo, kpv@research.att.com
34 #include <ast_common.h>
37 /* ast doesn't do threads yet */
38 #if _PACKAGE_ast && !defined(vt_threaded)
42 #if !defined(vt_threaded) || (defined(vt_threaded) && vt_threaded == 1)
43 #define _may_use_threads 1
45 #define _may_use_threads 0
49 #if _may_use_threads && !defined(vt_threaded) && _hdr_pthread
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
;
58 #if !defined(PTHREAD_ONCE_INIT) && defined(pthread_once_init)
59 #define PTHREAD_ONCE_INIT pthread_once_init
64 #if _may_use_threads && !defined(vt_threaded) && _WIN32
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
;
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 */
86 /* directives for vtset() */
87 #define VT_STACK 1 /* set stack size */
89 typedef struct _vtmutex_s Vtmutex_t
;
90 typedef struct _vtonce_s Vtonce_t
;
91 typedef struct _vthread_s Vthread_t
;
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
*));
115 extern int vtonce
_ARG_((Vtonce_t
*, void(*)() ));
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
*));
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
*));
133 /* mutex structure */
142 /* structure for states of thread */
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 */
153 /* structure for exactly once execution */
161 #define VTONCE_INITDATA {0, 0}
163 #define VTONCE_INITDATA {0, PTHREAD_ONCE_INIT }
166 #define vtstatus(vt) ((vt)->exit)
167 #define vterror(vt) ((vt)->error)
168 #define vtmtxerror(mtx) ((mtx)->error)
169 #define vtonceerror(once) ((once)->error)
171 #endif /*vt_threaded*/
173 /* fake structures and functions */
188 typedef int _vtmtx_t
;
189 typedef int _vtonce_t
;
190 typedef int _vtself_t
;
192 typedef int _vtattr_t
;
194 #define VTONCE_INITDATA {0}
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)
202 #define vtset(vt,t,v) (-1)
203 #define vtonce(on,fu) (-1)
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)
212 #define vtstatus(vt) ((Void_t*)0)
213 #define vterror(vt) (0)
214 #define vtmtxerror(mtx) (0)
215 #define vtonceerror(once) (0)
217 #endif /*!vt_threaded*/
219 #endif /*_VTHREAD_H*/