SMP.h 1.48 KB
Newer Older
1
2
/* ----------------------------------------------------------------------------
 *
3
 * (c) The GHC Team, 2005
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 * Macros for SMP support
 *
 * -------------------------------------------------------------------------- */

#ifndef SMP_H
#define SMP_H

/* SMP is currently not compatible with the following options:
 *
 *      INTERPRETER
 *      PROFILING
 *      TICKY_TICKY
 *      and unregisterised builds.
 */

#if defined(SMP)

22
#if  defined(PROFILING)  || defined(TICKY_TICKY)
23
24
25
26
27
28
29
#error Build options incompatible with SMP.
#endif

/* 
 * XCHG - the atomic exchange instruction.  Used for locking closures
 * during updates (see LOCK_CLOSURE below) and the MVar primops.
 */
30
31
32
33
34
35
INLINE_HEADER StgWord
xchg(StgPtr p, StgWord w)
{
    StgWord result;
    result = w;
    __asm__ __volatile__ (
36
 	  "xchg %1,%0"
37
38
39
40
41
42
43
44
45
          :"+r" (result), "+m" (*p)
          : /* no input-only operands */
	);
    return result;
}

INLINE_HEADER StgInfoTable *
lockClosure(StgClosure *p)
{
46
#if i386_HOST_ARCH || x86_64_HOST_ARCH
47
48
49
50
51
52
    StgWord info;
    do {
	info = xchg((P_)&p->header.info, (W_)&stg_WHITEHOLE_info);
	if (info != (W_)&stg_WHITEHOLE_info) return (StgInfoTable *)info;
	yieldThread();
    } while (1);
53
#else
54
55
56
57
58
59
60
61
62
63
64
65
   ACQUIRE_SM_LOCK
#endif
}

INLINE_HEADER void
unlockClosure(StgClosure *p, StgInfoTable *info)
{
#if i386_HOST_ARCH || x86_64_HOST_ARCH
    // This is safe enough, because lockClosure() does the memory barrier:
    p->header.info = info;
#else
    RELEASE_SM_LOCK;
66
#endif
67
}
68
69
70
71

#endif /* SMP */

#endif /* SMP_H */