D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4354 - Phobos should expose per-thread errno
Summary: Phobos should expose per-thread errno
Status: REOPENED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P3 normal
Assignee: No Owner
URL:
Keywords: bootcamp
Depends on:
Blocks:
 
Reported: 2010-06-20 17:51 UTC by torhu
Modified: 2024-12-01 16:13 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description torhu 2010-06-20 17:51:39 UTC
core.stdc.errno should probably expose the per-thread errno, but it doesn't.  Using the _errno() function instead of the errno macro in errno.c is one way to fix it.  Or possibly defining _MT when compiling the C parts of phobos.

Test case below is only tested with DMD 2.047, but this issue is present in both druntime and Phobos 1.x.  Build with -version=fix to see the fix in action.

---
import core.thread;
import core.stdc.errno : EDOM;
import core.stdc.math;
import std.stdio;
version (fix) { }
else {
	import core.stdc.errno;
}

version (fix) {
	extern (C) int* _errno();
	int errno() { return *_errno(); }
	int errno(int v) { return *_errno() = v; }
}

void main()
{
	pow(-1, 1.5);  // sets EDOM (33)
	assert(errno == EDOM);

	Thread t = new Thread({
            assert(errno == 0);  // fails if using phobos errno
	});
	t.start();

	t.join();
	assert(errno == EDOM);
}
---

I stumpled across this when working on a D wrapper for a C library that uses errno.
Comment 1 RazvanN 2017-07-07 08:41:31 UTC
This is actually a druntime bug since errno isn't defined in phobos, but in this example you could use it because std.stdio has a public import of core.stdc.
Comment 2 RazvanN 2017-07-07 08:42:17 UTC
Don't mind my previous comment, it is unfinished and it was before actually running the example.
Comment 3 Vladimir Panteleev 2017-07-07 14:29:46 UTC
(In reply to RazvanN from comment #2)
> Don't mind my previous comment,

I guess this should remain open, then.
Comment 4 Andrei Alexandrescu 2017-07-07 18:46:02 UTC
This is a reproducible problem on Windows (at least with wine). We use a C small file in druntime (src/core/stdc/errno.c) to make sure we use the C macro. Far as I can tell the same technique is used across Windows and Posix, which indicates the problem is with dmc's stdlib.
Comment 5 dlangBugzillaToGithub 2024-12-01 16:13:26 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/9579

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB