Skip to content

Commit 5bf285f

Browse files
authored
Merge pull request nim-lang#1 from shaoxie1986/devel
翻译
2 parents cbefb26 + daab7c2 commit 5bf285f

File tree

3 files changed

+66
-73
lines changed

3 files changed

+66
-73
lines changed

lib/core/locks.nim

+13-15
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,56 @@
77
# distribution, for details about the copyright.
88
#
99

10-
## This module contains Nim's support for locks and condition vars.
10+
## 这个模块包含了Nim对锁和条件变量的支持。
1111

1212
const insideRLocksModule = false
1313
include "system/syslocks"
1414

1515
type
16-
Lock* = SysLock ## Nim lock; whether this is re-entrant
17-
## or not is unspecified!
18-
Cond* = SysCond ## Nim condition variable
16+
Lock* = SysLock ## Nim的锁; 重入或者不重入。
17+
Cond* = SysCond ## Nim的条件变量
1918

2019
{.push stackTrace: off.}
2120

2221
proc initLock*(lock: var Lock) {.inline.} =
23-
## Initializes the given lock.
22+
## 初始化指定的锁。
2423
initSysLock(lock)
2524

2625
proc deinitLock*(lock: var Lock) {.inline.} =
27-
## Frees the resources associated with the lock.
26+
## 释放锁的相关资源。
2827
deinitSys(lock)
2928

3029
proc tryAcquire*(lock: var Lock): bool =
31-
## Tries to acquire the given lock. Returns `true` on success.
30+
## 试图获取指定的锁。成功返回 `true`
3231
result = tryAcquireSys(lock)
3332

3433
proc acquire*(lock: var Lock) =
35-
## Acquires the given lock.
34+
## 获取指定的锁。
3635
acquireSys(lock)
3736

3837
proc release*(lock: var Lock) =
39-
## Releases the given lock.
38+
## 释放指定的锁。
4039
releaseSys(lock)
4140

4241

4342
proc initCond*(cond: var Cond) {.inline.} =
44-
## Initializes the given condition variable.
43+
## 初始化指定的条件变量。
4544
initSysCond(cond)
4645

4746
proc deinitCond*(cond: var Cond) {.inline.} =
48-
## Frees the resources associated with the lock.
47+
## 释放条件变量的相关资源。
4948
deinitSysCond(cond)
5049

5150
proc wait*(cond: var Cond, lock: var Lock) {.inline.} =
52-
## waits on the condition variable `cond`.
51+
## 等待条件变量 `cond`.
5352
waitSysCond(cond, lock)
5453

5554
proc signal*(cond: var Cond) {.inline.} =
56-
## sends a signal to the condition variable `cond`.
55+
## 发送一个信号给条件变量 `cond`.
5756
signalSysCond(cond)
5857

5958
template withLock*(a: Lock, body: untyped) =
60-
## Acquires the given lock, executes the statements in body and
61-
## releases the lock after the statements finish executing.
59+
## 获取指定锁, 执行body中的语句,并且在语句执行完成之后释放锁。
6260
mixin acquire, release
6361
acquire(a)
6462
{.locks: [a].}:

lib/core/rlocks.nim

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
# distribution, for details about the copyright.
88
#
99

10-
## This module contains Nim's support for reentrant locks.
10+
## 这个模块包含Nim对可重入锁的支持.
1111

1212
const insideRLocksModule = true
1313
include "system/syslocks"
1414

1515
type
16-
RLock* = SysLock ## Nim lock, re-entrant
16+
RLock* = SysLock ## Nim的可重入锁
1717

1818
proc initRLock*(lock: var RLock) {.inline.} =
19-
## Initializes the given lock.
19+
## 初始化指定的锁。
2020
when defined(posix):
2121
var a: SysLockAttr
2222
initSysLockAttr(a)
@@ -26,23 +26,23 @@ proc initRLock*(lock: var RLock) {.inline.} =
2626
initSysLock(lock)
2727

2828
proc deinitRLock*(lock: var RLock) {.inline.} =
29-
## Frees the resources associated with the lock.
29+
## 释放锁的相关资源。
3030
deinitSys(lock)
3131

3232
proc tryAcquire*(lock: var RLock): bool =
33-
## Tries to acquire the given lock. Returns `true` on success.
33+
## 试图获取指定的锁。成功返回 `true`
3434
result = tryAcquireSys(lock)
3535

3636
proc acquire*(lock: var RLock) =
37-
## Acquires the given lock.
37+
## 获取指定的锁。
3838
acquireSys(lock)
3939

4040
proc release*(lock: var RLock) =
41-
## Releases the given lock.
41+
## 释放指定的锁。
4242
releaseSys(lock)
4343

4444
template withRLock*(lock: var RLock, code: untyped): untyped =
45-
## Acquires the given lock and then executes the code.
45+
## 获取指定锁, 执行code
4646
block:
4747
acquire(lock)
4848
defer:

lib/system/threads.nim

+45-50
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77
# distribution, for details about the copyright.
88
#
99

10-
## Thread support for Nim.
10+
## Nim的线程支持模块.
1111
##
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+
## 开关来开启线程的支持
1515
##
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>`_.
2120
##
22-
## Examples
21+
## 示例
2322
## ========
2423
##
2524
## .. code-block:: Nim
@@ -92,12 +91,10 @@ var
9291
threadDestructionHandlers {.rtlThreadVar.}: seq[proc () {.closure, gcsafe.}]
9392

9493
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,在线程销毁之前调用。
9795
##
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+
## 注意:线程抛出的异常如果未处理,将会导致整个进程退出。
10198
when not defined(nimNoNilSeqs):
10299
if threadDestructionHandlers.isNil:
103100
threadDestructionHandlers = @[]
@@ -191,22 +188,22 @@ else:
191188
{.pop.}
192189

193190
proc running*[TArg](t: Thread[TArg]): bool {.inline.} =
194-
## Returns true if `t` is running.
191+
## 如果线程 `t` 正在运行,返回true。
195192
result = t.dataFn != nil
196193

197194
proc handle*[TArg](t: Thread[TArg]): SysThread {.inline.} =
198-
## Returns the thread handle of `t`.
195+
## 返回线程 `t` 的句柄。
199196
result = t.sys
200197

201198
when hostOS == "windows":
202199
const MAXIMUM_WAIT_OBJECTS = 64
203200

204201
proc joinThread*[TArg](t: Thread[TArg]) {.inline.} =
205-
## Waits for the thread `t` to finish.
202+
## 等待 `t` 中的每一个线程运行完成。
206203
discard waitForSingleObject(t.sys, -1'i32)
207204

208205
proc joinThreads*[TArg](t: varargs[Thread[TArg]]) =
209-
## Waits for every thread in `t` to finish.
206+
## 等待 `t` 中的每一个线程运行完成。
210207
var a: array[MAXIMUM_WAIT_OBJECTS, SysThread]
211208
var k = 0
212209
while k < len(t):
@@ -218,33 +215,33 @@ when hostOS == "windows":
218215

219216
elif defined(genode):
220217
proc joinThread*[TArg](t: Thread[TArg]) {.importcpp.}
221-
## Waits for the thread `t` to finish.
218+
## 等待 `t` 中的每一个线程运行完成。
222219

223220
proc joinThreads*[TArg](t: varargs[Thread[TArg]]) =
224-
## Waits for every thread in `t` to finish.
221+
## 等待 `t` 中的每一个线程运行完成。
225222
for i in 0..t.high: joinThread(t[i])
226223

227224
else:
228225
proc joinThread*[TArg](t: Thread[TArg]) {.inline.} =
229-
## Waits for the thread `t` to finish.
226+
## 等待 `t` 中的每一个线程运行完成。
230227
discard pthread_join(t.sys, nil)
231228

232229
proc joinThreads*[TArg](t: varargs[Thread[TArg]]) =
233-
## Waits for every thread in `t` to finish.
230+
## 等待 `t` 中的每一个线程运行完成。
234231
for i in 0..t.high: joinThread(t[i])
235232

236233
when false:
237234
# XXX a thread should really release its heap here somehow:
238235
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` 的全部控制权和他的资源,此操作存在潜在的危险。
241238
when hostOS == "windows":
242239
discard TerminateThread(t.sys, 1'i32)
243240
else:
244241
discard pthread_cancel(t.sys)
245242
when declared(registerThread): unregisterThread(addr(t))
246243
t.dataFn = nil
247-
## if thread `t` already exited, `t.core` will be `null`.
244+
## 如果线程 `t` 已经退出, `t.core` 将会是 `null`
248245
if not isNil(t.core):
249246
deallocShared(t.core)
250247
t.core = nil
@@ -253,11 +250,10 @@ when hostOS == "windows":
253250
proc createThread*[TArg](t: var Thread[TArg],
254251
tp: proc (arg: TArg) {.thread, nimcall.},
255252
param: TArg) =
256-
## Creates a new thread `t` and starts its execution.
253+
## 创建一个新的线程 `t` 并且开始执行。
257254
##
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``
261257
t.core = cast[PGcThread](allocShared0(sizeof(GcThread)))
262258

263259
when TArg isnot void: t.data = param
@@ -270,15 +266,15 @@ when hostOS == "windows":
270266
raise newException(ResourceExhaustedError, "cannot create thread")
271267

272268
proc pinToCpu*[Arg](t: var Thread[Arg]; cpu: Natural) =
273-
## Pins a thread to a `CPU`:idx:.
269+
## 绑定一个线程到一个 `CPU`:idx:
274270
##
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的功能,最好不要使用。
277273
setThreadAffinityMask(t.sys, uint(1 shl cpu))
278274

279275
elif defined(genode):
280276
var affinityOffset: cuint = 1
281-
## CPU affinity offset for next thread, safe to roll-over.
277+
## 下一个线程的CPU亲核性偏移量,安全回滚。
282278

283279
proc createThread*[TArg](t: var Thread[TArg],
284280
tp: proc (arg: TArg) {.thread, nimcall.},
@@ -302,11 +298,10 @@ else:
302298
proc createThread*[TArg](t: var Thread[TArg],
303299
tp: proc (arg: TArg) {.thread, nimcall.},
304300
param: TArg) =
305-
## Creates a new thread `t` and starts its execution.
301+
## 创建一个新的线程 `t` 并且开始执行。
306302
##
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``
310305
t.core = cast[PGcThread](allocShared0(sizeof(GcThread)))
311306

312307
when TArg isnot void: t.data = param
@@ -319,10 +314,10 @@ else:
319314
raise newException(ResourceExhaustedError, "cannot create thread")
320315

321316
proc pinToCpu*[Arg](t: var Thread[Arg]; cpu: Natural) =
322-
## Pins a thread to a `CPU`:idx:.
317+
## 绑定一个线程到一个 `CPU`:idx:
323318
##
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的功能,最好不要使用。
326321
when not defined(macosx):
327322
var s {.noinit.}: CpuSet
328323
cpusetZero(s)
@@ -337,7 +332,7 @@ var threadId {.threadvar.}: int
337332

338333
when defined(windows):
339334
proc getThreadId*(): int =
340-
## Gets the ID of the currently running thread.
335+
## 获取当前线程ID。
341336
if threadId == 0:
342337
threadId = int(getCurrentThreadId())
343338
result = threadId
@@ -350,7 +345,7 @@ elif defined(linux):
350345
var NR_gettid {.importc: "__NR_gettid", header: "<sys/syscall.h>".}: clong
351346

352347
proc getThreadId*(): int =
353-
## Gets the ID of the currently running thread.
348+
## 获取当前线程ID。
354349
if threadId == 0:
355350
threadId = int(syscall(NR_gettid))
356351
result = threadId
@@ -359,7 +354,7 @@ elif defined(dragonfly):
359354
proc lwp_gettid(): int32 {.importc, header: "unistd.h".}
360355

361356
proc getThreadId*(): int =
362-
## Gets the ID of the currently running thread.
357+
## 获取当前线程ID。
363358
if threadId == 0:
364359
threadId = int(lwp_gettid())
365360
result = threadId
@@ -368,7 +363,7 @@ elif defined(openbsd):
368363
proc getthrid(): int32 {.importc: "getthrid", header: "<unistd.h>".}
369364

370365
proc getThreadId*(): int =
371-
## get the ID of the currently running thread.
366+
## 获取当前线程ID。
372367
if threadId == 0:
373368
threadId = int(getthrid())
374369
result = threadId
@@ -377,7 +372,7 @@ elif defined(netbsd):
377372
proc lwp_self(): int32 {.importc: "_lwp_self", header: "<lwp.h>".}
378373

379374
proc getThreadId*(): int =
380-
## Gets the ID of the currently running thread.
375+
## 获取当前线程ID。
381376
if threadId == 0:
382377
threadId = int(lwp_self())
383378
result = threadId
@@ -387,7 +382,7 @@ elif defined(freebsd):
387382
var SYS_thr_self {.importc:"SYS_thr_self", header:"<sys/syscall.h>"}: cint
388383

389384
proc getThreadId*(): int =
390-
## Gets the ID of the currently running thread.
385+
## 获取当前线程ID。
391386
var tid = 0.cint
392387
if threadId == 0:
393388
discard syscall(SYS_thr_self, addr tid)
@@ -399,7 +394,7 @@ elif defined(macosx):
399394
var SYS_thread_selfid {.importc:"SYS_thread_selfid", header:"<sys/syscall.h>".}: cint
400395

401396
proc getThreadId*(): int =
402-
## Gets the ID of the currently running thread.
397+
## 获取当前线程ID。
403398
if threadId == 0:
404399
threadId = int(syscall(SYS_thread_selfid))
405400
result = threadId
@@ -409,7 +404,7 @@ elif defined(solaris):
409404
proc thr_self(): thread_t {.importc, header: "<thread.h>".}
410405

411406
proc getThreadId*(): int =
412-
## Gets the ID of the currently running thread.
407+
## 获取当前线程ID。
413408
if threadId == 0:
414409
threadId = int(thr_self())
415410
result = threadId
@@ -419,7 +414,7 @@ elif defined(haiku):
419414
proc find_thread(name: cstring): thr_id {.importc, header: "<OS.h>".}
420415

421416
proc getThreadId*(): int =
422-
## Gets the ID of the currently running thread.
417+
## 获取当前线程ID。
423418
if threadId == 0:
424419
threadId = int(find_thread(nil))
425420
result = threadId

0 commit comments

Comments
 (0)