以下这段伪代码来自于 OraPubCraig A. Shallahamer,这段代码并不长但基本对获取latch、spin、sleep的行为都描述清楚了,如果你对latch仍不甚了了,那么这段代码会对你很有帮助:       
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Function Get_Latch(latch_name,mode)
{
   If Mode eq ‘immediate’ {
     If Fast_Get(latch_name) {
       return  TRUE
     Else {
       return  FALSE
     }
   }
   Else {
     If Fast_Get(latch_name)
     Then {
       v$latch. gets ++
       return  TRUE
     }
     Else {
       v$latch.misses++
       for  try  = 0 .. large_number
       {
         if  Spin_Get(latch_name)
         Then {
           return  TRUE
         }
         Else {
           T0 =  time
           Sleep( try )
           T1= time
           Register_Event( "latch free" ,T1-T0)
         }
       } -- spin/sleep loop
     }
   }
}
 
Function Fast_Get(latch_name)
{
   If try_to_get_latch(latch_name)
   Then {
     return  TRUE
   }
   Else {
     return  FALSE
   }
}
 
Function Spin_Get(latch_name)
{
   v$latch.spin_get++
   for  i = 1 to _spin_count
   {
     If Fast_Get(latch_name)
     Then {
       return  TRUE
     }
   }
}
 
Function Sleep( try )
{
   v$latch.sleeps++
   v$latch.sleep[ try ]++
   sleeptime =
     decode( try ,0,0,1,10,2,20,3,~40,4,~80,...~2000)
   sleep(sleeptime)
}