summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2018-12-05 08:12:17 +0100
committerGitHub <noreply@github.com>2018-12-05 08:12:17 +0100
commitd21529649746e68fd6f3f955627eda3ee0017534 (patch)
treef6b764ae9d9c855031a02649bcf90f8c63edd2ca
parent88dcad7c011b71fe6376c1fab62ca815fd4f1b73 (diff)
parente4850b7f1cac1f2111f2d0fdf976cf4531ef21a2 (diff)
downloadNim-d21529649746e68fd6f3f955627eda3ee0017534.tar.gz
Merge pull request #9846 from timotheecour/pr_getpid
add os.getCurrentProcessId()
-rw-r--r--changelog.md1
-rw-r--r--lib/pure/os.nim9
-rw-r--r--lib/pure/osproc.nim3
3 files changed, 12 insertions, 1 deletions
diff --git a/changelog.md b/changelog.md
index 536d58237..6dbfa1a2d 100644
--- a/changelog.md
+++ b/changelog.md
@@ -91,6 +91,7 @@ proc enumToString*(enums: openArray[enum]): string =
 - Vm suport for float32<->int32 and float64<->int64 casts was added.
 - There is a new pragma block `noSideEffect` that works like
   the `gcsafe` pragma block.
+- added os.getCurrentProcessId()
 
 ### Language changes
 
diff --git a/lib/pure/os.nim b/lib/pure/os.nim
index e2dd872e8..533d8f350 100644
--- a/lib/pure/os.nim
+++ b/lib/pure/os.nim
@@ -2419,6 +2419,15 @@ proc isHidden*(path: string): bool {.noNimScript.} =
     let fileName = lastPathPart(path)
     result = len(fileName) >= 2 and fileName[0] == '.' and fileName != ".."
 
+proc getCurrentProcessId*(): int {.noNimScript.} =
+  ## return current process ID. See also ``osproc.processID(p: Process)``.
+  when defined(windows):
+    proc GetCurrentProcessId(): DWORD {.stdcall, dynlib: "kernel32",
+                                        importc: "GetCurrentProcessId".}
+    result = GetCurrentProcessId().int
+  else:
+    result = getpid()
+
 {.pop.}
 
 proc setLastModificationTime*(file: string, t: times.Time) {.noNimScript.} =
diff --git a/lib/pure/osproc.nim b/lib/pure/osproc.nim
index a9f37412f..b2239b9c5 100644
--- a/lib/pure/osproc.nim
+++ b/lib/pure/osproc.nim
@@ -155,7 +155,7 @@ proc running*(p: Process): bool {.rtl, extern: "nosp$1", tags: [].}
   ## Returns true iff the process `p` is still running. Returns immediately.
 
 proc processID*(p: Process): int {.rtl, extern: "nosp$1".} =
-  ## returns `p`'s process ID.
+  ## returns `p`'s process ID. See also ``os.getCurrentProcessId()``.
   return p.id
 
 proc waitForExit*(p: Process, timeout: int = -1): int {.rtl,
@@ -1341,3 +1341,4 @@ proc execCmdEx*(command: string, options: set[ProcessOption] = {
       result[1] = peekExitCode(p)
       if result[1] != -1: break
   close(p)
+