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