160617d67df40188e59124dc13747ebebd3a3292
[dragonfly.git] / sys / sys / mplock2.h
1 /*
2  * SYS/MPLOCK2.H
3  *
4  * Implement the MP lock.  Note that debug operations
5  */
6 #ifndef _SYS_MPLOCK2_H_
7 #define _SYS_MPLOCK2_H_
8
9 #ifndef _MACHINE_ATOMIC_H_
10 #include <machine/atomic.h>
11 #endif
12 #ifndef _SYS_THREAD_H_
13 #include <sys/thread.h>
14 #endif
15 #ifndef _SYS_GLOBALDATA_H_
16 #include <sys/globaldata.h>
17 #endif
18
19 #ifdef SMP
20
21 /*
22  * NOTE: try_mplock()/lwkt_trytoken() return non-zero on success.
23  */
24 #define get_mplock()            lwkt_gettoken(&mp_token)
25 #define try_mplock()            lwkt_trytoken(&mp_token)
26 #define rel_mplock()            lwkt_reltoken(&mp_token)
27 #define get_mplock_count(td)    lwkt_cnttoken(&mp_token, td)
28
29 void cpu_get_initial_mplock(void);
30 void handle_cpu_contention_mask(void);
31
32 extern cpumask_t cpu_contention_mask;
33
34 /*
35  * A cpu wanted the MP lock but could not get it.  This function is also
36  * called directly from the LWKT scheduler.
37  *
38  * Reentrant, may be called even if the cpu is already contending the MP
39  * lock.
40  */
41 static __inline
42 void
43 set_cpu_contention_mask(globaldata_t gd)
44 {
45         atomic_set_cpumask(&cpu_contention_mask, gd->gd_cpumask);
46 }
47
48 /*
49  * A cpu is no longer contending for the MP lock after previously contending
50  * for it.
51  *
52  * Reentrant, may be called even if the cpu was not previously contending
53  * the MP lock.
54  */
55 static __inline
56 void
57 clr_cpu_contention_mask(globaldata_t gd)
58 {
59         atomic_clear_cpumask(&cpu_contention_mask, gd->gd_cpumask);
60 }
61
62 #define MP_LOCK_HELD()          LWKT_TOKEN_HELD(&mp_token)
63 #define ASSERT_MP_LOCK_HELD()   ASSERT_LWKT_TOKEN_HELD(&mp_token)
64
65
66 #else
67
68 /*
69  * UNI-PROCESSOR BUILD - Degenerate case macros
70  */
71 #define get_mplock()
72 #define rel_mplock()
73 #define try_mplock()            1
74 #define owner_mplock()          0
75 #define MP_LOCK_HELD(gd)        1
76 #define ASSERT_MP_LOCK_HELD(td)
77
78 #endif
79
80 #endif