-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ensure autoloading kernel32.dll threading API
- Loading branch information
Showing
10 changed files
with
137 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ build/ | |
*.so.* | ||
*.dylib | ||
*.a | ||
*.lib | ||
*.la | ||
*.dll | ||
*.exe | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Constantine | ||
# Copyright (c) 2018-2019 Status Research & Development GmbH | ||
# Copyright (c) 2020-Present Mamy André-Ratsimbazafy | ||
# Licensed and distributed under either of | ||
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT). | ||
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0). | ||
# at your option. This file may not be copied, modified, or distributed except according to those terms. | ||
|
||
import ../constantine/platforms/loadtime_functions | ||
|
||
# When Constantine is built as a library, we want to minimize friction of using it. | ||
# Hence we want to users to be able to directly use it without special ceremony. | ||
# | ||
# This is possible for dynamic libraries if --noMain isn't used. | ||
# | ||
# https://github.com/nim-lang/Nim/blob/v2.0.0/compiler/cgen.nim#L1572-L1583 | ||
# https://github.com/nim-lang/Nim/blob/v2.0.0/lib/nimbase.h#L513 | ||
# | ||
# The function DllMain is autoloaded on Windows | ||
# Functions tagged __attribute__((constructor)) are autoloaded on UNIX OSes | ||
# | ||
# Alas, Nim doesn't provide such facilities for static libraries | ||
# so we provide our own {.loadTime.} macro for autoloading: | ||
# - any proc | ||
# - on any OS | ||
# - whether using a dynamic or static library | ||
# | ||
# We use them for runtime CPU features detection. | ||
# | ||
# And on Windows as functions in DLLs (kernel APIs for the threadpool for example) are loaded | ||
# as global variables | ||
|
||
when defined(windows) and (appType == "lib" or appType == "staticlib"): | ||
proc ctt_init_NimMain() {.importc, cdecl.} | ||
## Setup Nim globals, including loading library dependencies on Windows | ||
## We assume that Constantine was compiled with --nimMainPrefix:ctt_init_ | ||
|
||
proc ctt_autoload_NimMain() {.load_time.} = | ||
## Autosetup Constantine globals on library load. | ||
## This must be referenced from an another module | ||
## to not be optimized away by the static linker | ||
ctt_init_NimMain() | ||
|
||
proc ctt_autoloader_addr*(): pointer = | ||
## This returns an runtime reference to the autoloader | ||
## so that it cannot be optimized away. | ||
## Compare it with "nil" | ||
cast[pointer](ctt_autoload_NimMain) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Constantine | ||
# Copyright (c) 2018-2019 Status Research & Development GmbH | ||
# Copyright (c) 2020-Present Mamy André-Ratsimbazafy | ||
# Licensed and distributed under either of | ||
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT). | ||
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0). | ||
# at your option. This file may not be copied, modified, or distributed except according to those terms. | ||
|
||
# To ensure that __attribute__((constructor)) procs are not | ||
# removed because they are unaccessible, we force symbol reference | ||
when defined(windows) and (appType == "lib" or appType == "staticlib"): | ||
import ../../bindings/lib_autoload | ||
|
||
proc check_lib_dependency_loader*() = | ||
## This prevents the linker from deleting our constructor function | ||
## that loads Windows kernel, synchronization and threading related functions. | ||
## We only need to have any symbol in the translation unit being used. | ||
doAssert ctt_autoloader_addr() != nil | ||
else: | ||
template check_lib_dependency_loader*() = | ||
## This prevents the linker from deleting our constructor function | ||
## that loads Windows kernel, synchronization and threading related functions. | ||
## We only need to have any symbol in the translation unit being used. | ||
discard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** Constantine | ||
* Copyright (c) 2018-2019 Status Research & Development GmbH | ||
* Copyright (c) 2020-Present Mamy André-Ratsimbazafy | ||
* Licensed and distributed under either of | ||
* * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT). | ||
* * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0). | ||
* at your option. This file may not be copied, modified, or distributed except according to those terms. | ||
*/ | ||
|
||
// This is a test to ensure the C API for the threadpool works on all platforms | ||
// and: | ||
// if special options like -d:tlsEmulation:off are needed | ||
// if NimMain is done implicitly at load time. | ||
|
||
#include <stdio.h> | ||
#include <constantine.h> | ||
|
||
void ctt_init_NimMain(void); | ||
|
||
int main(){ | ||
printf("Constantine: Testing the C API for the threadpool.\n"); | ||
// ctt_init_NimMain(); | ||
|
||
struct ctt_threadpool* tp = ctt_threadpool_new(4); | ||
printf("Constantine: Threadpool init successful.\n"); | ||
ctt_threadpool_shutdown(tp); | ||
printf("Constantine: Threadpool shutdown successful.\n"); | ||
|
||
return 0; | ||
} |