Fix header locations
[ghc.git] / compiler / cbits / genSym.c
1 #include <assert.h>
2 #include "Rts.h"
3 #include "Unique.h"
4
5 static HsInt GenSymCounter = 0;
6 static HsInt GenSymInc = 1;
7
8 #define UNIQUE_BITS (sizeof (HsInt) * 8 - UNIQUE_TAG_BITS)
9 #define UNIQUE_MASK ((1ULL << UNIQUE_BITS) - 1)
10
11 STATIC_INLINE void checkUniqueRange(HsInt u STG_UNUSED) {
12 #if DEBUG
13 // Uh oh! We will overflow next time a unique is requested.
14 assert(u != UNIQUE_MASK);
15 #endif
16 }
17
18 HsInt genSym(void) {
19 #if defined(THREADED_RTS)
20 if (n_capabilities == 1) {
21 GenSymCounter = (GenSymCounter + GenSymInc) & UNIQUE_MASK;
22 checkUniqueRange(GenSymCounter);
23 return GenSymCounter;
24 } else {
25 HsInt n = atomic_inc((StgWord *)&GenSymCounter, GenSymInc)
26 & UNIQUE_MASK;
27 checkUniqueRange(n);
28 return n;
29 }
30 #else
31 GenSymCounter = (GenSymCounter + GenSymInc) & UNIQUE_MASK;
32 checkUniqueRange(GenSymCounter);
33 return GenSymCounter;
34 #endif
35 }
36
37 void initGenSym(HsInt NewGenSymCounter, HsInt NewGenSymInc) {
38 GenSymCounter = NewGenSymCounter;
39 GenSymInc = NewGenSymInc;
40 }