R/zzz.R

Defines functions .onUnload .onLoad loadTbbLibrary

# !diagnostics suppress=.dllInfo,.tbbDllInfo,.tbbMallocDllInfo,.tbbMallocProxyDllInfo

# NOTE: we intentionally do _not_ load tbbmalloc_proxy by default, as its
# intended use is to replace the default allocator, something that may be
# dangerous to do by default. in addition, TBB's documentation recommends
# only loading explicitly via e.g. LD_PRELOAD
.dllInfo               <- NULL
.tbbDllInfo            <- NULL
.tbbMallocDllInfo      <- NULL
.tbbMallocProxyDllInfo <- NULL

loadTbbLibrary <- function(name) {

   # On Windows, TBB is statically linked into RcppParallel.dll, but we
   # still ship a stub tbb.dll for compatibility with packages linking via
   # '-ltbb' (e.g. through StanHeaders). Such packages record a load-time
   # dependency on 'tbb.dll', which the Windows loader can only resolve if
   # we've already loaded it -- the library directory itself is not on the
   # DLL search path.
   if (is_windows()) {

      # NOTE: resolve against the package's own library directory, where
      # install.libs.R places the stub; tbbRoot() would prefer TBB_LIB,
      # which on Windows points at Rtools and holds only static libraries
      path <- archSystemFile("lib", paste0(name, ".dll"))
      if (!file.exists(path)) {
         verboseMessage("tbb library '%s' not found in package 'lib' folder; skipping", name)
         return(NULL)
      }

      verboseMessage("loading tbb library '%s'", path)
      return(dyn.load(path, local = FALSE, now = TRUE))

   }

   path <- tbbLibraryPath(name)
   if (is.null(path)) {
      verboseMessage("tbb library '%s' could not be resolved; skipping", name)
      return(NULL)
   }

   if (!file.exists(path)) {
      warning("tbb library ", shQuote(name), " not found.")
      return(NULL)
   }

   verboseMessage("loading tbb library '%s'", path)
   dyn.load(path, local = FALSE, now = TRUE)
   
}

.onLoad <- function(libname, pkgname) {
   
   # on Windows, load RcppParallel first
   if (.Platform$OS.type == "windows") {
      .dllInfo <<- library.dynam("RcppParallel", pkgname, libname)
   }
   
   # load tbb, tbbmalloc
   .tbbDllInfo       <<- loadTbbLibrary("tbb")
   .tbbMallocDllInfo <<- loadTbbLibrary("tbbmalloc")
   
   # load tbbmalloc_proxy, but only if requested
   useTbbMallocProxy <- Sys.getenv("RCPP_PARALLEL_USE_TBBMALLOC_PROXY", unset = "FALSE")
   if (useTbbMallocProxy %in% c("TRUE", "True", "true", "1"))
      .tbbMallocProxyDllInfo <<- loadTbbLibrary("tbbmalloc_proxy")
   
   # load RcppParallel library if available
   if (.Platform$OS.type != "windows") {
      .dllInfo <<- library.dynam("RcppParallel", pkgname, libname, local = FALSE)
   }
   
}

.onUnload <- function(libpath) {
   
   # unload the package library
   if (!is.null(.dllInfo))
      library.dynam.unload("RcppParallel", libpath)
   
   # NOTE: we do not explicitly unload tbbmalloc_proxy as switching
   # the allocator at runtime can cause issues
   
   # unload tbbmalloc if we loaded it
   if (!is.null(.tbbMallocDllInfo))
      dyn.unload(.tbbMallocDllInfo[["path"]])
   
   # unload tbb if we loaded it
   if (!is.null(.tbbDllInfo))
      dyn.unload(.tbbDllInfo[["path"]])
   
}

Try the RcppParallel package in your browser

Any scripts or data that you put into this service are public.

RcppParallel documentation built on July 27, 2026, 5:08 p.m.