1*b30d1939SAndy Fiddaman /***********************************************************************
2*b30d1939SAndy Fiddaman *                                                                      *
3*b30d1939SAndy Fiddaman *               This software is part of the ast package               *
4*b30d1939SAndy Fiddaman *          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5*b30d1939SAndy Fiddaman *                      and is licensed under the                       *
6*b30d1939SAndy Fiddaman *                 Eclipse Public License, Version 1.0                  *
7*b30d1939SAndy Fiddaman *                    by AT&T Intellectual Property                     *
8*b30d1939SAndy Fiddaman *                                                                      *
9*b30d1939SAndy Fiddaman *                A copy of the License is available at                 *
10*b30d1939SAndy Fiddaman *          http://www.eclipse.org/org/documents/epl-v10.html           *
11*b30d1939SAndy Fiddaman *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12*b30d1939SAndy Fiddaman *                                                                      *
13*b30d1939SAndy Fiddaman *              Information and Software Systems Research               *
14*b30d1939SAndy Fiddaman *                            AT&T Research                             *
15*b30d1939SAndy Fiddaman *                           Florham Park NJ                            *
16*b30d1939SAndy Fiddaman *                                                                      *
17*b30d1939SAndy Fiddaman *                 Glenn Fowler <gsf@research.att.com>                  *
18*b30d1939SAndy Fiddaman *                  David Korn <dgk@research.att.com>                   *
19*b30d1939SAndy Fiddaman *                   Phong Vo <kpv@research.att.com>                    *
20*b30d1939SAndy Fiddaman *                                                                      *
21*b30d1939SAndy Fiddaman ***********************************************************************/
22*b30d1939SAndy Fiddaman #pragma prototyped
23*b30d1939SAndy Fiddaman 
24*b30d1939SAndy Fiddaman #ifndef _ASO_H
25*b30d1939SAndy Fiddaman #define _ASO_H	1
26*b30d1939SAndy Fiddaman 
27*b30d1939SAndy Fiddaman #define ASO_VERSION	20111111L
28*b30d1939SAndy Fiddaman 
29*b30d1939SAndy Fiddaman #include <ast_common.h>
30*b30d1939SAndy Fiddaman 
31*b30d1939SAndy Fiddaman /*
32*b30d1939SAndy Fiddaman  * ast atomic scalar operations interface definitions
33*b30d1939SAndy Fiddaman  */
34*b30d1939SAndy Fiddaman 
35*b30d1939SAndy Fiddaman /* asometh() types (ordered mutually exclusive flags) */
36*b30d1939SAndy Fiddaman #define ASO_NEXT	(-1)
37*b30d1939SAndy Fiddaman #define ASO_SIGNAL	1
38*b30d1939SAndy Fiddaman #define ASO_THREAD	2
39*b30d1939SAndy Fiddaman #define ASO_PROCESS	4
40*b30d1939SAndy Fiddaman #define ASO_INTRINSIC	8
41*b30d1939SAndy Fiddaman 
42*b30d1939SAndy Fiddaman /* asolock() operations */
43*b30d1939SAndy Fiddaman #define ASO_UNLOCK	0	/* unlock if key matches  		*/
44*b30d1939SAndy Fiddaman #define ASO_TRYLOCK	1	/* matched key means successful attempt	*/
45*b30d1939SAndy Fiddaman #define ASO_LOCK	2	/* matched key first, then spin-lock	*/
46*b30d1939SAndy Fiddaman #define ASO_SPINLOCK	3	/* no matching of key before locking	*/
47*b30d1939SAndy Fiddaman 
48*b30d1939SAndy Fiddaman /* Asoerror_f types */
49*b30d1939SAndy Fiddaman #define ASO_EMETHOD	0	/* method specific error		*/
50*b30d1939SAndy Fiddaman #define ASO_EHUNG	1	/* asoloop() possibly hung		*/
51*b30d1939SAndy Fiddaman 
52*b30d1939SAndy Fiddaman /* for internal use, but standardized for libs such as CDT and Vmalloc */
53*b30d1939SAndy Fiddaman #define ASO_RELAX	((1<<2)-1) /* cycles between spin-loop yield */
54*b30d1939SAndy Fiddaman #define ASOLOOP(k)	asoloop(++(k))
55*b30d1939SAndy Fiddaman 
56*b30d1939SAndy Fiddaman #define ASODISC(d,e)	(memset(d,0,sizeof(*(d))),(d)->version=ASO_VERSION,(d)->errorf=(e))
57*b30d1939SAndy Fiddaman 
58*b30d1939SAndy Fiddaman typedef int (*Asoerror_f)(int, const char*);
59*b30d1939SAndy Fiddaman typedef void* (*Asoinit_f)(void*, const char*);
60*b30d1939SAndy Fiddaman typedef ssize_t (*Asolock_f)(void*, ssize_t, void volatile*);
61*b30d1939SAndy Fiddaman 
62*b30d1939SAndy Fiddaman typedef struct Asodisc_s
63*b30d1939SAndy Fiddaman {
64*b30d1939SAndy Fiddaman 	uint32_t	version;
65*b30d1939SAndy Fiddaman 	unsigned int	hung;
66*b30d1939SAndy Fiddaman 	Asoerror_f	errorf;
67*b30d1939SAndy Fiddaman } Asodisc_t;
68*b30d1939SAndy Fiddaman 
69*b30d1939SAndy Fiddaman typedef struct Asometh_s
70*b30d1939SAndy Fiddaman {
71*b30d1939SAndy Fiddaman 	const char*	name;
72*b30d1939SAndy Fiddaman 	int		type;
73*b30d1939SAndy Fiddaman 	Asoinit_f	initf;
74*b30d1939SAndy Fiddaman 	Asolock_f	lockf;
75*b30d1939SAndy Fiddaman 	const char*	details;
76*b30d1939SAndy Fiddaman } Asometh_t;
77*b30d1939SAndy Fiddaman 
78*b30d1939SAndy Fiddaman #if (_BLD_aso || _BLD_taso) && defined(__EXPORT__)
79*b30d1939SAndy Fiddaman #define extern	extern __EXPORT__
80*b30d1939SAndy Fiddaman #endif
81*b30d1939SAndy Fiddaman #if !(_BLD_aso || _BLD_taso) && defined(__IMPORT__)
82*b30d1939SAndy Fiddaman #define extern	extern __IMPORT__
83*b30d1939SAndy Fiddaman #endif
84*b30d1939SAndy Fiddaman 
85*b30d1939SAndy Fiddaman extern Asometh_t*		asometh(int, void*);
86*b30d1939SAndy Fiddaman 
87*b30d1939SAndy Fiddaman #undef	extern
88*b30d1939SAndy Fiddaman 
89*b30d1939SAndy Fiddaman #if _BLD_aso && defined(__EXPORT__)
90*b30d1939SAndy Fiddaman #define extern	extern __EXPORT__
91*b30d1939SAndy Fiddaman #endif
92*b30d1939SAndy Fiddaman #if !_BLD_aso && defined(__IMPORT__)
93*b30d1939SAndy Fiddaman #define extern	extern __IMPORT__
94*b30d1939SAndy Fiddaman #endif
95*b30d1939SAndy Fiddaman 
96*b30d1939SAndy Fiddaman extern Asometh_t*		_asometh(int, void*);
97*b30d1939SAndy Fiddaman extern int			asoinit(const char*, Asometh_t*, Asodisc_t*);
98*b30d1939SAndy Fiddaman extern int			asolock(unsigned int volatile*, unsigned int, int);
99*b30d1939SAndy Fiddaman extern int			asoloop(uintmax_t);
100*b30d1939SAndy Fiddaman extern int			asorelax(long);
101*b30d1939SAndy Fiddaman 
102*b30d1939SAndy Fiddaman extern uint8_t			asocas8(uint8_t volatile*, int, int);
103*b30d1939SAndy Fiddaman extern uint8_t			asoget8(uint8_t volatile*);
104*b30d1939SAndy Fiddaman extern uint8_t			asoinc8(uint8_t volatile*);
105*b30d1939SAndy Fiddaman extern uint8_t			asodec8(uint8_t volatile*);
106*b30d1939SAndy Fiddaman 
107*b30d1939SAndy Fiddaman #define asocaschar(p,o,n)	asocas8(p,o,n)
108*b30d1939SAndy Fiddaman #define asogetchar(p)		asoget8(p)
109*b30d1939SAndy Fiddaman #define asoincchar(p)		asoinc8(p)
110*b30d1939SAndy Fiddaman #define asodecchar(p)		asodec8(p)
111*b30d1939SAndy Fiddaman 
112*b30d1939SAndy Fiddaman extern uint16_t			asocas16(uint16_t volatile*, uint16_t, uint16_t);
113*b30d1939SAndy Fiddaman extern uint16_t			asoget16(uint16_t volatile*);
114*b30d1939SAndy Fiddaman extern uint16_t			asoinc16(uint16_t volatile*);
115*b30d1939SAndy Fiddaman extern uint16_t			asodec16(uint16_t volatile*);
116*b30d1939SAndy Fiddaman 
117*b30d1939SAndy Fiddaman #define asocasshort(p,o,n)	asocas16(p,o,n)
118*b30d1939SAndy Fiddaman #define asogetshort(p)		asoget16(p)
119*b30d1939SAndy Fiddaman #define asoincshort(p)		asoinc16(p)
120*b30d1939SAndy Fiddaman #define asodecshort(p)		asodec16(p)
121*b30d1939SAndy Fiddaman 
122*b30d1939SAndy Fiddaman extern uint32_t			asocas32(uint32_t volatile*, uint32_t, uint32_t);
123*b30d1939SAndy Fiddaman extern uint32_t			asoget32(uint32_t volatile*);
124*b30d1939SAndy Fiddaman extern uint32_t			asoinc32(uint32_t volatile*);
125*b30d1939SAndy Fiddaman extern uint32_t			asodec32(uint32_t volatile*);
126*b30d1939SAndy Fiddaman 
127*b30d1939SAndy Fiddaman #if _ast_sizeof_int == 4
128*b30d1939SAndy Fiddaman #define asocasint(p,o,n)	asocas32((uint32_t volatile*)p,o,n)
129*b30d1939SAndy Fiddaman #define asogetint(p)		asoget32((uint32_t volatile*)p)
130*b30d1939SAndy Fiddaman #define asoincint(p)		asoinc32((uint32_t volatile*)p)
131*b30d1939SAndy Fiddaman #define asodecint(p)		asodec32((uint32_t volatile*)p)
132*b30d1939SAndy Fiddaman #endif
133*b30d1939SAndy Fiddaman 
134*b30d1939SAndy Fiddaman #if _ast_sizeof_long == 4
135*b30d1939SAndy Fiddaman #define asocaslong(p,o,n)	asocas32((uint32_t volatile*)p,o,n)
136*b30d1939SAndy Fiddaman #define asogetlong(p)		asoget32((uint32_t volatile*)p)
137*b30d1939SAndy Fiddaman #define asoinclong(p)		asoinc32((uint32_t volatile*)p)
138*b30d1939SAndy Fiddaman #define asodeclong(p)		asodec32((uint32_t volatile*)p)
139*b30d1939SAndy Fiddaman #endif
140*b30d1939SAndy Fiddaman 
141*b30d1939SAndy Fiddaman #if _ast_sizeof_size_t == 4
142*b30d1939SAndy Fiddaman #define asocassize(p,o,n)	asocas32((uint32_t volatile*)p,o,n)
143*b30d1939SAndy Fiddaman #define asogetsize(p)		asoget32((uint32_t volatile*)p)
144*b30d1939SAndy Fiddaman #define asoincsize(p)		asoinc32((uint32_t volatile*)p)
145*b30d1939SAndy Fiddaman #define asodecsize(p)		asodec32((uint32_t volatile*)p)
146*b30d1939SAndy Fiddaman #endif
147*b30d1939SAndy Fiddaman 
148*b30d1939SAndy Fiddaman #ifdef _ast_int8_t
149*b30d1939SAndy Fiddaman 
150*b30d1939SAndy Fiddaman extern uint64_t			asocas64(uint64_t volatile*, uint64_t, uint64_t);
151*b30d1939SAndy Fiddaman extern uint64_t			asoget64(uint64_t volatile*);
152*b30d1939SAndy Fiddaman extern uint64_t			asoinc64(uint64_t volatile*);
153*b30d1939SAndy Fiddaman extern uint64_t			asodec64(uint64_t volatile*);
154*b30d1939SAndy Fiddaman 
155*b30d1939SAndy Fiddaman #if _ast_sizeof_int == 8
156*b30d1939SAndy Fiddaman #define asocasint(p,o,n)	asocas64((uint64_t volatile*)p,o,n)
157*b30d1939SAndy Fiddaman #define asogetint(p)		asoget64((uint64_t volatile*)p)
158*b30d1939SAndy Fiddaman #define asoincint(p)		asoinc64((uint64_t volatile*)p)
159*b30d1939SAndy Fiddaman #define asodecint(p)		asodec64((uint64_t volatile*)p)
160*b30d1939SAndy Fiddaman #endif
161*b30d1939SAndy Fiddaman 
162*b30d1939SAndy Fiddaman #if _ast_sizeof_long == 8
163*b30d1939SAndy Fiddaman #define asocaslong(p,o,n)	asocas64((uint64_t volatile*)p,o,n)
164*b30d1939SAndy Fiddaman #define asogetlong(p)		asoget64((uint64_t volatile*)p)
165*b30d1939SAndy Fiddaman #define asoinclong(p)		asoinc64((uint64_t volatile*)p)
166*b30d1939SAndy Fiddaman #define asodeclong(p)		asodec64((uint64_t volatile*)p)
167*b30d1939SAndy Fiddaman #endif
168*b30d1939SAndy Fiddaman 
169*b30d1939SAndy Fiddaman #if _ast_sizeof_size_t == 8
170*b30d1939SAndy Fiddaman #define asocassize(p,o,n)	asocas64((uint64_t volatile*)p,o,n)
171*b30d1939SAndy Fiddaman #define asogetsize(p)		asoget64((uint64_t volatile*)p)
172*b30d1939SAndy Fiddaman #define asoincsize(p)		asoinc64((uint64_t volatile*)p)
173*b30d1939SAndy Fiddaman #define asodecsize(p)		asodec64((uint64_t volatile*)p)
174*b30d1939SAndy Fiddaman #endif
175*b30d1939SAndy Fiddaman 
176*b30d1939SAndy Fiddaman #endif
177*b30d1939SAndy Fiddaman 
178*b30d1939SAndy Fiddaman extern void*			asocasptr(void volatile*, void*, void*);
179*b30d1939SAndy Fiddaman extern void*			asogetptr(void volatile*);
180*b30d1939SAndy Fiddaman 
181*b30d1939SAndy Fiddaman #undef	extern
182*b30d1939SAndy Fiddaman 
183*b30d1939SAndy Fiddaman #endif