summary refs log tree commit diff stats
path: root/lib/system/threads.nim
diff options
context:
space:
mode:
Diffstat (limited to 'lib/system/threads.nim')
-rwxr-xr-xlib/system/threads.nim120
1 files changed, 3 insertions, 117 deletions
diff --git a/lib/system/threads.nim b/lib/system/threads.nim
index 39e5d3908..4ec1239a9 100755
--- a/lib/system/threads.nim
+++ b/lib/system/threads.nim
@@ -21,6 +21,8 @@
 ##
 ## .. code-block:: nimrod
 ##
+##  import locks
+##
 ##  var
 ##    thr: array [0..4, TThread[tuple[a,b: int]]]
 ##    L: TLock
@@ -39,8 +41,6 @@
   
 const
   maxRegisters = 256 # don't think there is an arch with more registers
-  maxLocksPerThread = 10 ## max number of locks a thread can hold
-                         ## at the same time
   useStackMaskHack = false ## use the stack mask hack for better performance
   StackGuardSize = 4096
   ThreadStackMask = 1024*256*sizeof(int)-1
@@ -251,7 +251,7 @@ when not defined(useNimRtl):
 type
   TThread* {.pure, final.}[TMsg] =
       object of TGcThread ## Nimrod thread. A thread is a heavy object (~14K)
-                          ## that should not be part of a message! Use
+                          ## that **must not** be part of a message! Use
                           ## a ``TThreadId`` for that.
     emptyFn: proc ()
     dataFn: proc (m: TMsg)
@@ -395,120 +395,6 @@ when useStackMaskHack:
     createThread(mainThread, tp)
     joinThread(mainThread)
 
-# --------------------------- lock handling ----------------------------------
-
-type
-  TLock* = TSysLock ## Nimrod lock; whether this is re-entrant
-                    ## or not is unspecified!
-  
-const
-  noDeadlocks = false # compileOption("deadlockPrevention")
-
-when false:
-  var
-    deadlocksPrevented*: int ## counts the number of times a 
-                             ## deadlock has been prevented
-    locksLen {.threadvar.}: int
-    locks {.threadvar.}: array [0..MaxLocksPerThread-1, pointer]
-
-  proc OrderedLocks(): bool = 
-    for i in 0 .. locksLen-2:
-      if locks[i] >= locks[i+1]: return false
-    result = true
-
-proc InitLock*(lock: var TLock) {.inline.} =
-  ## Initializes the lock `lock`.
-  InitSysLock(lock)
-
-proc TryAcquire*(lock: var TLock): bool {.inline.} = 
-  ## Try to acquires the lock `lock`. Returns `true` on success.
-  result = TryAcquireSys(lock)
-  when noDeadlocks:
-    if not result: return
-    # we have to add it to the ordered list. Oh, and we might fail if
-    # there is no space in the array left ...
-    if locksLen >= len(locks):
-      ReleaseSys(lock)
-      raise newException(EResourceExhausted, "cannot acquire additional lock")
-    # find the position to add:
-    var p = addr(lock)
-    var L = locksLen-1
-    var i = 0
-    while i <= L:
-      sysAssert locks[i] != nil
-      if locks[i] < p: inc(i) # in correct order
-      elif locks[i] == p: return # thread already holds lock
-      else:
-        # do the crazy stuff here:
-        while L >= i:
-          locks[L+1] = locks[L]
-          dec L
-        locks[i] = p
-        inc(locksLen)
-        sysAssert OrderedLocks()
-        return
-    # simply add to the end:
-    locks[locksLen] = p
-    inc(locksLen)
-    sysAssert OrderedLocks(g)
-
-proc Acquire*(lock: var TLock) =
-  ## Acquires the lock `lock`.
-  when nodeadlocks:
-    var p = addr(lock)
-    var L = locksLen-1
-    var i = 0
-    while i <= L:
-      sysAssert locks[i] != nil
-      if locks[i] < p: inc(i) # in correct order
-      elif locks[i] == p: return # thread already holds lock
-      else:
-        # do the crazy stuff here:
-        if locksLen >= len(locks):
-          raise newException(EResourceExhausted, 
-              "cannot acquire additional lock")
-        while L >= i:
-          ReleaseSys(cast[ptr TSysLock](locks[L])[])
-          locks[L+1] = locks[L]
-          dec L
-        # acquire the current lock:
-        AcquireSys(lock)
-        locks[i] = p
-        inc(locksLen)
-        # acquire old locks in proper order again:
-        L = locksLen-1
-        inc i
-        while i <= L:
-          AcquireSys(cast[ptr TSysLock](locks[i])[])
-          inc(i)
-        # DANGER: We can only modify this global var if we gained every lock!
-        # NO! We need an atomic increment. Crap.
-        discard system.atomicInc(deadlocksPrevented, 1)
-        sysAssert OrderedLocks(g)
-        return
-        
-    # simply add to the end:
-    if locksLen >= len(locks):
-      raise newException(EResourceExhausted, "cannot acquire additional lock")
-    AcquireSys(lock)
-    locks[locksLen] = p
-    inc(locksLen)
-    sysAssert OrderedLocks(g)
-  else:
-    AcquireSys(lock)
-  
-proc Release*(lock: var TLock) =
-  ## Releases the lock `lock`.
-  when nodeadlocks:
-    var p = addr(lock)
-    var L = locksLen
-    for i in countdown(L-1, 0):
-      if locks[i] == p: 
-        for j in i..L-2: locks[j] = locks[j+1]
-        dec locksLen
-        break
-  ReleaseSys(lock)
-
 # ------------------------ message passing support ---------------------------
 
 proc getInBoxMem[TMsg](t: var TThread[TMsg]): pointer {.inline.} =
revision' href='/ahoang/Nim/blame/nim/lexbase.pas?h=devel&id=1c8ddca7e08af9075a930edaca6c522d5e6fd8b5'>^
405b86068

















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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232