oneTBB icon indicating copy to clipboard operation
oneTBB copied to clipboard

Remove <Windows.h> include from headers

Open Chronial opened this issue 3 years ago • 9 comments

The tbb currently includes Windows.h in enumerable_thread_specific.h. Through transitive includes, this also affects combinable.h, concurrent_map.h and concurrent_set.h.

Since windows.h is massive and declares thousands of symbols, this include has a negative impact on build times. The windows include does not need to be in a public header. We should either:

  • Copy the declarations of the few required symbols from windows.h or
  • Move the calls to the Win32 API to a cpp

Chronial avatar Sep 13 '21 12:09 Chronial

to notice : detail/_machine.h includes windows.h as well, however only for TBB Malloc build (#ifdef __TBBMALLOC_BUILD).

anton-potapov avatar Nov 09 '21 07:11 anton-potapov

Two thumbs up from my side for reducing the use of windows.h in headers...

emmenlau avatar Nov 09 '21 09:11 emmenlau

Please remove windows.h from shared header files for tests in test/common. Its presence has created difficulties for the cygwin port that needs Win32 versions of certain functions while mostly defaulting to Linux and Unix code. A particular example is GetMemoryUsage in test/common/memory_usage.h as described in https://github.com/oneapi-src/oneTBB/issues/156#issuecomment-963280611

zmajeed avatar Nov 09 '21 11:11 zmajeed

We are in the process of internal agreement. Sorry for the delay.

alexey-katranov avatar Nov 09 '21 15:11 alexey-katranov

@Chronial notes in their PR https://github.com/oneapi-src/oneTBB/pull/576#issuecomment-957730990 that this issue is about usability

I'll point out that oneTBB is a portable library that provides cross-platform abstractions and interfaces. C++ headers basically define these interfaces. Including windows.h in a header file unfortunately violates these portable abstractions in the worst possible way - by polluting the global namespace with macros that have nothing to do with oneTBB interfaces.

Also it's not like oneTBB is a header-only library that does not separate implementation from interface - this request is just along the same lines as what oneTBB has been doing all along. It's simply that one needs to think of windows.h as an implementation detail that oneTBB needs to encapsulate within its abstraction layer - any other considerations are secondary

zmajeed avatar Nov 10 '21 12:11 zmajeed

oneTBB is a mix of header-only and binary-based APIs; many class templates are header-only at least by default (if no tool support is requested via a macro, etc.). Typically these templates, including enumerable_thread_specific and concurrent containers, can be used not only in oneTBB parallel constructs but in any thread. IIRC in the past there were requests to allow using such APIs without linking with TBB binaries. which is also about usability.

Therefore, even though the use of windows.h is an implementation detail, whether to include it in public headers or not is an important tradeoff that needs to be carefully evaluated. This is what the team is doing now.

One possible solution I can think of is to provide an opt-out macro that precludes the use of windows.h at the cost of making ets_key_per_instance storage type unavailable (as simply changing the behavior of that would create risk of ODR violation).

akukanov avatar Nov 29 '21 15:11 akukanov

Probably NOMINMAX can decrease the overhead of Windows.h inclusion, while this is the solution for a different issue.

Alexandr-Konovalov avatar Feb 13 '24 15:02 Alexandr-Konovalov

Probably NOMINMAX can decrease the overhead of Windows.h inclusion, while this is the solution for a different issue.

This preprocessor define is certainly helpful. In case someone has this problem, there are even more related macros, see http://web.archive.org/web/20121219084749/http://support.microsoft.com/kb/166474:

   VC_EXTRALEAN
   WIN32_LEAN_AND_MEAN

However, this does not really solve all the problems that can be caused by including windows.h. For example, the MSVC header will still add defines for a number of common names. For example, a number of file API methods are using preprocessor defines: CreateFile, DeleteFile, etc from https://learn.microsoft.com/en-us/windows/win32/api/fileapi/. After including windows.h, these names can no longer be used as class or method names.

In summary, its always very good to avoid including windows.h in any headers, if somehow possible. No problem to include it in implementation files, though.

emmenlau avatar Feb 14 '24 10:02 emmenlau

@alexey-katranov is this issue is still relevant?

arunparkugan avatar Aug 13 '24 09:08 arunparkugan