7
7
# distribution, for details about the copyright.
8
8
#
9
9
10
- # # Thread support for Nim .
10
+ # # Nim的线程支持模块 .
11
11
# #
12
- # # **Note **: This is part of the system module. Do not import it directly .
13
- # # To activate thread support you need to compile
14
- # # with the ``--threads:on`` command line switch.
12
+ # # **注意 **: 这是system模块的一部分. 不需要直接import .
13
+ # # 需要在编译的时候在命令行使用 ``--threads:on``
14
+ # # 开关来开启线程的支持
15
15
# #
16
- # # Nim's memory model for threads is quite different from other common
17
- # # programming languages (C, Pascal): Each thread has its own
18
- # # (garbage collected) heap and sharing of memory is restricted. This helps
19
- # # to prevent race conditions and improves efficiency. See `the manual for
20
- # # details of this memory model <manual.html#threads>`_.
16
+ # # Nim语言线程的内存模型与常见的编程语言(C, Pascal)不同:
17
+ # # 每一个线程都拥有自己(垃圾回收)的堆,共享内存是受限制的。
18
+ # # 这个能避免竞争条件,并且能提高效率。
19
+ # # 详情可查看 `手册中关于这种内存模型的描述 <manual.html#threads>`_.
21
20
# #
22
- # # Examples
21
+ # # 示例
23
22
# # ========
24
23
# #
25
24
# # .. code-block:: Nim
92
91
threadDestructionHandlers {.rtlThreadVar .}: seq [proc () {.closure , gcsafe .}]
93
92
94
93
proc onThreadDestruction * (handler: proc () {.closure , gcsafe .}) =
95
- # # Registers a *thread local* handler that is called at the thread's
96
- # # destruction.
94
+ # # 注册一个 *thread local* 的处理proc,在线程销毁之前调用。
97
95
# #
98
- # # A thread is destructed when the ``.thread`` proc returns
99
- # # normally or when it raises an exception. Note that unhandled exceptions
100
- # # in a thread nevertheless cause the whole process to die.
96
+ # # 当一个 ``.thread`` proc正常退出或者抛出异常,这个线程会销毁,
97
+ # # 注意:线程抛出的异常如果未处理,将会导致整个进程退出。
101
98
when not defined (nimNoNilSeqs):
102
99
if threadDestructionHandlers.isNil:
103
100
threadDestructionHandlers = @ []
@@ -191,22 +188,22 @@ else:
191
188
{.pop .}
192
189
193
190
proc running * [TArg](t: Thread [TArg ]): bool {.inline .} =
194
- # # Returns true if `t` is running.
191
+ # # 如果线程 `t` 正在运行,返回true。
195
192
result = t.dataFn != nil
196
193
197
194
proc handle * [TArg](t: Thread [TArg ]): SysThread {.inline .} =
198
- # # Returns the thread handle of `t`.
195
+ # # 返回线程 `t` 的句柄。
199
196
result = t.sys
200
197
201
198
when hostOS == " windows" :
202
199
const MAXIMUM_WAIT_OBJECTS = 64
203
200
204
201
proc joinThread * [TArg](t: Thread [TArg ]) {.inline .} =
205
- # # Waits for the thread `t` to finish.
202
+ # # 等待 `t` 中的每一个线程运行完成。
206
203
discard waitForSingleObject (t.sys, - 1 'i32 )
207
204
208
205
proc joinThreads * [TArg](t: varargs [Thread [TArg ]]) =
209
- # # Waits for every thread in `t` to finish.
206
+ # # 等待 `t` 中的每一个线程运行完成。
210
207
var a: array [MAXIMUM_WAIT_OBJECTS , SysThread ]
211
208
var k = 0
212
209
while k < len (t):
@@ -218,33 +215,33 @@ when hostOS == "windows":
218
215
219
216
elif defined (genode):
220
217
proc joinThread * [TArg](t: Thread [TArg ]) {.importcpp .}
221
- # # Waits for the thread `t` to finish.
218
+ # # 等待 `t` 中的每一个线程运行完成。
222
219
223
220
proc joinThreads * [TArg](t: varargs [Thread [TArg ]]) =
224
- # # Waits for every thread in `t` to finish.
221
+ # # 等待 `t` 中的每一个线程运行完成。
225
222
for i in 0 .. t.high: joinThread (t[i])
226
223
227
224
else :
228
225
proc joinThread * [TArg](t: Thread [TArg ]) {.inline .} =
229
- # # Waits for the thread `t` to finish.
226
+ # # 等待 `t` 中的每一个线程运行完成。
230
227
discard pthread_join (t.sys, nil )
231
228
232
229
proc joinThreads * [TArg](t: varargs [Thread [TArg ]]) =
233
- # # Waits for every thread in `t` to finish.
230
+ # # 等待 `t` 中的每一个线程运行完成。
234
231
for i in 0 .. t.high: joinThread (t[i])
235
232
236
233
when false :
237
234
# XXX a thread should really release its heap here somehow:
238
235
proc destroyThread * [TArg](t: var Thread [TArg ]) =
239
- # # Forces the thread `t` to terminate. This is potentially dangerous if
240
- # # you don't have full control over `t` and its acquired resources.
236
+ # # 强制终止线程 `t` 。
237
+ # # 如果你并不拥有 `t` 的全部控制权和他的资源,此操作存在潜在的危险。
241
238
when hostOS == " windows" :
242
239
discard TerminateThread (t.sys, 1 'i32 )
243
240
else :
244
241
discard pthread_cancel (t.sys)
245
242
when declared (registerThread): unregisterThread (addr (t))
246
243
t.dataFn = nil
247
- # # if thread `t` already exited, `t.core` will be `null`.
244
+ # # 如果线程 `t` 已经退出, `t.core` 将会是 `null`。
248
245
if not isNil (t.core):
249
246
deallocShared (t.core)
250
247
t.core = nil
@@ -253,11 +250,10 @@ when hostOS == "windows":
253
250
proc createThread * [TArg](t: var Thread [TArg ],
254
251
tp: proc (arg: TArg ) {.thread , nimcall .},
255
252
param: TArg ) =
256
- # # Creates a new thread `t` and starts its execution.
253
+ # # 创建一个新的线程 `t` 并且开始执行。
257
254
# #
258
- # # Entry point is the proc `tp`.
259
- # # `param` is passed to `tp`. `TArg` can be ``void`` if you
260
- # # don't need to pass any data to the thread.
255
+ # # 线程的入口函数是 `tp` 。 `param` 是传送给线程函数的参数 `tp` 。
256
+ # # 如不需要传递任何数据给线程, `TArg` 可以传 ``void``
261
257
t.core = cast [PGcThread ](allocShared0 (sizeof (GcThread )))
262
258
263
259
when TArg isnot void : t.data = param
@@ -270,15 +266,15 @@ when hostOS == "windows":
270
266
raise newException (ResourceExhaustedError , " cannot create thread" )
271
267
272
268
proc pinToCpu * [Arg](t: var Thread [Arg ]; cpu: Natural ) =
273
- # # Pins a thread to a `CPU`:idx:.
269
+ # # 绑定一个线程到一个 `CPU`:idx: 。
274
270
# #
275
- # # In other words sets a thread's `affinity `:idx:.
276
- # # If you don't know what this means, you shouldn't use this proc.
271
+ # # 换句话说:设置一个线程的 `亲和性 `:idx: 。
272
+ # # 如果你不清楚这个proc的功能,最好不要使用。
277
273
setThreadAffinityMask (t.sys, uint (1 shl cpu))
278
274
279
275
elif defined (genode):
280
276
var affinityOffset: cuint = 1
281
- # # CPU affinity offset for next thread, safe to roll-over.
277
+ # # 下一个线程的CPU亲核性偏移量,安全回滚。
282
278
283
279
proc createThread * [TArg](t: var Thread [TArg ],
284
280
tp: proc (arg: TArg ) {.thread , nimcall .},
@@ -302,11 +298,10 @@ else:
302
298
proc createThread * [TArg](t: var Thread [TArg ],
303
299
tp: proc (arg: TArg ) {.thread , nimcall .},
304
300
param: TArg ) =
305
- # # Creates a new thread `t` and starts its execution.
301
+ # # 创建一个新的线程 `t` 并且开始执行。
306
302
# #
307
- # # Entry point is the proc `tp`. `param` is passed to `tp`.
308
- # # `TArg` can be ``void`` if you
309
- # # don't need to pass any data to the thread.
303
+ # # 线程的入口函数是 `tp` 。 `param` 是传送给线程函数的参数 `tp` 。
304
+ # # 如不需要传递任何数据给线程, `TArg` 可以传 ``void``
310
305
t.core = cast [PGcThread ](allocShared0 (sizeof (GcThread )))
311
306
312
307
when TArg isnot void : t.data = param
@@ -319,10 +314,10 @@ else:
319
314
raise newException (ResourceExhaustedError , " cannot create thread" )
320
315
321
316
proc pinToCpu * [Arg](t: var Thread [Arg ]; cpu: Natural ) =
322
- # # Pins a thread to a `CPU`:idx:.
317
+ # # 绑定一个线程到一个 `CPU`:idx: 。
323
318
# #
324
- # # In other words sets a thread's `affinity `:idx:.
325
- # # If you don't know what this means, you shouldn't use this proc.
319
+ # # 换句话说:设置一个线程的 `亲和性 `:idx: 。
320
+ # # 如果你不清楚这个proc的功能,最好不要使用。
326
321
when not defined (macosx):
327
322
var s {.noinit .}: CpuSet
328
323
cpusetZero (s)
@@ -337,7 +332,7 @@ var threadId {.threadvar.}: int
337
332
338
333
when defined (windows):
339
334
proc getThreadId * (): int =
340
- # # Gets the ID of the currently running thread.
335
+ # # 获取当前线程ID。
341
336
if threadId == 0 :
342
337
threadId = int (getCurrentThreadId ())
343
338
result = threadId
@@ -350,7 +345,7 @@ elif defined(linux):
350
345
var NR_gettid {.importc : " __NR_gettid" , header : " <sys/syscall.h>" .}: clong
351
346
352
347
proc getThreadId * (): int =
353
- # # Gets the ID of the currently running thread.
348
+ # # 获取当前线程ID。
354
349
if threadId == 0 :
355
350
threadId = int (syscall (NR_gettid))
356
351
result = threadId
@@ -359,7 +354,7 @@ elif defined(dragonfly):
359
354
proc lwp_gettid (): int32 {.importc , header : " unistd.h" .}
360
355
361
356
proc getThreadId * (): int =
362
- # # Gets the ID of the currently running thread.
357
+ # # 获取当前线程ID。
363
358
if threadId == 0 :
364
359
threadId = int (lwp_gettid ())
365
360
result = threadId
@@ -368,7 +363,7 @@ elif defined(openbsd):
368
363
proc getthrid (): int32 {.importc : " getthrid" , header : " <unistd.h>" .}
369
364
370
365
proc getThreadId * (): int =
371
- # # get the ID of the currently running thread.
366
+ # # 获取当前线程ID。
372
367
if threadId == 0 :
373
368
threadId = int (getthrid ())
374
369
result = threadId
@@ -377,7 +372,7 @@ elif defined(netbsd):
377
372
proc lwp_self (): int32 {.importc : " _lwp_self" , header : " <lwp.h>" .}
378
373
379
374
proc getThreadId * (): int =
380
- # # Gets the ID of the currently running thread.
375
+ # # 获取当前线程ID。
381
376
if threadId == 0 :
382
377
threadId = int (lwp_self ())
383
378
result = threadId
@@ -387,7 +382,7 @@ elif defined(freebsd):
387
382
var SYS_thr_self {.importc :" SYS_thr_self" , header :" <sys/syscall.h>" }: cint
388
383
389
384
proc getThreadId * (): int =
390
- # # Gets the ID of the currently running thread.
385
+ # # 获取当前线程ID。
391
386
var tid = 0 .cint
392
387
if threadId == 0 :
393
388
discard syscall (SYS_thr_self, addr tid)
@@ -399,7 +394,7 @@ elif defined(macosx):
399
394
var SYS_thread_selfid {.importc :" SYS_thread_selfid" , header :" <sys/syscall.h>" .}: cint
400
395
401
396
proc getThreadId * (): int =
402
- # # Gets the ID of the currently running thread.
397
+ # # 获取当前线程ID。
403
398
if threadId == 0 :
404
399
threadId = int (syscall (SYS_thread_selfid))
405
400
result = threadId
@@ -409,7 +404,7 @@ elif defined(solaris):
409
404
proc thr_self (): thread_t {.importc , header : " <thread.h>" .}
410
405
411
406
proc getThreadId * (): int =
412
- # # Gets the ID of the currently running thread.
407
+ # # 获取当前线程ID。
413
408
if threadId == 0 :
414
409
threadId = int (thr_self ())
415
410
result = threadId
@@ -419,7 +414,7 @@ elif defined(haiku):
419
414
proc find_thread (name: cstring ): thr_id {.importc , header : " <OS.h>" .}
420
415
421
416
proc getThreadId * (): int =
422
- # # Gets the ID of the currently running thread.
417
+ # # 获取当前线程ID。
423
418
if threadId == 0 :
424
419
threadId = int (find_thread (nil ))
425
420
result = threadId
0 commit comments