Merge from vendor branch NTPD:
[dragonfly.git] / sys / i386 / include / gencount.h
1 /*
2  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Joerg Sonnenberger <joerg@bec.de>.
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/i386/include/Attic/gencount.h,v 1.1 2004/12/08 23:19:51 joerg Exp $
35  */
36
37 #ifndef _MACHINE_GENCOUNT_H
38 #define _MACHINE_GENCOUNT_H
39
40 #ifndef _KERNEL
41 #error "no user-servicable parts inside"
42 #endif
43
44 #include <sys/types.h>
45 #include <sys/thread2.h>
46
47 typedef struct {
48         uint32_t low, high;
49 } gencount_t;
50
51 /* not interrupt-safe */
52 static __inline void
53 gencount_init(gencount_t *gencnt)
54 {
55         gencnt->low = 0;
56         gencnt->high = 0;
57 }
58
59 /* interrupt-safe */
60 static __inline void
61 gencount_inc(gencount_t *gencnt)
62 {
63         crit_enter();
64         if (++gencnt->high == 0)
65                 ++gencnt->low;
66         crit_exit();
67 }
68
69 /* interrupt-safe */
70 static __inline gencount_t
71 gencount_get(gencount_t *gencnt)
72 {
73         volatile gencount_t *vgencnt;
74         gencount_t ret;
75
76         vgencnt = gencnt;
77         ret.low = vgencnt->low;
78         ret.high = vgencnt->high;
79         if (ret.low != vgencnt->low) {
80                 /*
81                  * Choose an arbitrary time between when we started
82                  * and when we finished. Since blocking can occur
83                  * at any time, this has to be save.
84                  */
85                 ret.low++;
86                 ret.high = 0;
87         }
88
89         return(ret);
90 }
91
92 /* not interrupt-safe */
93 static __inline int
94 gencount_cmp(gencount_t *gencnt1, gencount_t *gencnt2)
95 {
96         if (gencnt1->low > gencnt2->low)
97                 return(1);
98         else if (gencnt1->low < gencnt1->low)
99                 return(-1);
100         else if (gencnt1->high > gencnt2->high)
101                 return(1);
102         else if (gencnt1->high < gencnt2->high)
103                 return(1);
104         else
105                 return(0);
106 }
107
108 #endif