Portable lock-free data structures by Keir Fraser (MCAS)
[openafs.git] / src / mcas / sparc_defns.h
1 #ifndef __SPARC_DEFNS_H__
2 #define __SPARC_DEFNS_H__
3
4 #ifndef SPARC
5 #define SPARC
6 #endif
7
8 #include <sys/types.h>
9 #include <sys/processor.h>
10 #include <sys/procset.h>
11 #include <sched.h>
12 #include <alloca.h>
13
14 #define CACHE_LINE_SIZE 64
15
16 #if 1
17 #include <thread.h>
18 #define pthread_mutex_t mutex_t
19 #define pthread_cond_t  cond_t
20 #define pthread_t       thread_t
21 #define pthread_key_t   thread_key_t
22 #define pthread_create(_a,_b,_c,_d) thr_create(NULL,0,_c,_d,THR_BOUND|THR_NEW_LWP,_a)
23 #define pthread_join(_a,_b) thr_join(_a,NULL,NULL)
24 #define pthread_key_create(_a,_b) thr_keycreate(_a,_b)
25 #define pthread_setspecific(_a,_b) thr_setspecific(_a,_b)
26 static void *pthread_getspecific(pthread_key_t _a)
27 {
28     void *__x;
29     thr_getspecific(_a,&__x);
30     return __x;
31 }
32 #define pthread_setconcurrency(_x) thr_setconcurrency(_x)
33 #define pthread_mutex_init(_a,_b) mutex_init(_a,USYNC_THREAD,NULL)
34 #define pthread_mutex_lock(_a) mutex_lock(_a)
35 #define pthread_mutex_unlock(_a) mutex_unlock(_a)
36 #define pthread_cond_init(_a,_b) cond_init(_a,USYNC_THREAD,NULL)
37 #define pthread_cond_wait(_a,_b) cond_wait(_a,_b)
38 #define pthread_cond_broadcast(_a) cond_broadcast(_a)
39 #else
40 #include <pthread.h>
41 #endif
42
43
44 /*
45  * I. Compare-and-swap.
46  */
47
48 typedef unsigned long long _u64;
49
50 extern int CASIO_internal(int *, int, int);
51 extern void * CASPO_internal(void *, void *, void *);
52 extern _u64 CAS64O_internal(_u64 *, _u64, _u64);
53 #define CASIO(_a,_o,_n) (CASIO_internal((int*)(_a),(int)(_o),(int)(_n)))
54 #define CASPO(_a,_o,_n) (CASPO_internal((void*)(_a),(void*)(_o),(void*)(_n)))
55 #define CAS32O(_a,_o,_n) (_u32)(CASIO_internal((int *)_a,(int)_o,(int)_n))
56 #define CAS64O(_a,_o,_n) (CAS64O_internal((_u64 *)_a,(_u64)_o,(_u64)_n))
57
58 static int FASIO(int *a, int n)
59 {
60     int no, o = *a;
61     while ( (no = CASIO(a, o, n)) != o ) o = no;
62     return o;
63 }
64
65 static void *FASPO(void *a, void *n)
66 {
67     void *no, *o = *(void **)a;
68     while ( (no = CASPO(a, o, n)) != o ) o = no;
69     return o;
70 }
71
72
73 /*
74  * II. Memory barriers.
75  *  WMB(): All preceding write operations must commit before any later writes.
76  *  RMB(): All preceding read operations must commit before any later reads.
77  *  MB():  All preceding memory accesses must commit before any later accesses.
78  *
79  *  If the compiler does not observe these barriers (but any sane compiler
80  *  will!), then VOLATILE should be defined as 'volatile'.
81  */
82
83 extern void MEMBAR_ALL(void);
84 extern void MEMBAR_STORESTORE(void);
85 extern void MEMBAR_LOADLOAD(void);
86 #define MB()  MEMBAR_ALL()
87 #define WMB() MEMBAR_STORESTORE()
88 #define RMB() MEMBAR_LOADLOAD()
89 #define VOLATILE /*volatile*/
90
91
92 /*
93  * III. Cycle counter access.
94  */
95
96 typedef unsigned long tick_t;
97 extern tick_t RDTICK(void);
98
99
100 /*
101  * IV. Types.
102  */
103
104 typedef unsigned char      _u8;
105 typedef unsigned short     _u16;
106 typedef unsigned int       _u32;
107
108 #endif /* __SPARC_DEFNS_H__ */