Thread overview
[D-runtime] How not to write OS specific code
Feb 01, 2011
Walter Bright
Feb 02, 2011
Sean Kelly
Feb 02, 2011
Walter Bright
Feb 02, 2011
Sean Kelly
Feb 02, 2011
Brad Roberts
Feb 02, 2011
Jonathan M Davis
February 01, 2011
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/d-runtime/attachments/20110201/219df23a/attachment.html>
February 01, 2011
I don't know that the static assert is appropriate, since that stuff is optional, and the convention in the Posix headers is not to have an assert for undefined blocks, but it's no big deal to me either way. The 'else' clause was clearly wrong however.

Sent from my iPhone

On Feb 1, 2011, at 10:55 AM, Walter Bright <walter at digitalmars.com> wrote:

> Found this today in src/core/sys/posix/time.d:
> 
> version( linux )
> {
>     enum CLOCK_MONOTONIC        = 1;
>     enum CLOCK_MONOTONIC_RAW    = 4; // non-standard
>     enum CLOCK_MONOTONIC_COARSE = 6; // non-standard
> }
> else
> {
>     enum CLOCK_MONOTONIC        = 4;
> }
> 
> This is WRONG WRONG WRONG.
> 
> 1. it's wrong for OSX, which does not even define CLOCK_MONOTONIC
> 2. there's no guarantee that on "other" operating systems CLOCK_MONOTONIC will be defined, or if it is, that it will be 4.
> 3. you only find out there's a problem when something mysterious happens.
> 4. cut & pasting declarations from one operating system to another without checking is akin to dropping an anvil on the head of the hapless developer who can't figure out why there are mysterious failures on that other system. The developer then has to go back and hand-check every last one of those declarations.
> 
> This is not acceptable practice. Declarations for an OS should never be inserted or enabled until they are checked.
> 
> The corrected version is:
> 
> version( linux )
> {
>     enum CLOCK_MONOTONIC        = 1;
>     enum CLOCK_MONOTONIC_RAW    = 4; // non-standard
>     enum CLOCK_MONOTONIC_COARSE = 6; // non-standard
> }
> else version (FreeBSD)
> {   // time.h
>     enum CLOCK_MONOTONIC         = 4;
>     enum CLOCK_MONOTONIC_PRECISE = 11;
>     enum CLOCK_MONOTONIC_FAST    = 12;
> }
> else version (OSX)
> {
>     // No CLOCK_MONOTONIC defined
> }
> else
> {
>     static assert(0);
> }
> 
> Note that the assert will trip when a new OS is tried, thus pointing the developer at places that need to be checked.
> _______________________________________________
> D-runtime mailing list
> D-runtime at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/d-runtime
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/d-runtime/attachments/20110201/00440488/attachment.html>
February 01, 2011
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/d-runtime/attachments/20110201/fc513328/attachment.html>
February 01, 2011
On Tuesday 01 February 2011 18:28:07 Sean Kelly wrote:
> I don't know that the static assert is appropriate, since that stuff is optional, and the convention in the Posix headers is not to have an assert for undefined blocks, but it's no big deal to me either way. The 'else' clause was clearly wrong however.

Well, if the static assert(0) is there in the false, then when you try and compile it on a new OS, you know that that's something that needs to be dealt with. If the solution on that OS is to just not have anything defined for that block, then you add an else version block for that OS with nothing in it, and if it is supposed to have those definitions, then you define them appropriately. Regardless, it won't get missed that way. If you don't have that else block, then you're likely to miss it when trying to get druntime to compile and run on a new OS unless not having it results in something not compiling.

- Jonathan M Davis
February 01, 2011
If the default is not to define it then the user will get a compile error the first time he tries to use it. I'd love to proactively develop the Posix headers, but they're huge. It's easier to build them out as demand dictates.

Sent from my iPhone

On Feb 1, 2011, at 6:38 PM, Walter Bright <walter at digitalmars.com> wrote:

> The assert is to force the person porting the header to a new system to check to see what is appropriate for that system. I believe os specific code ought to be proactively written, not defaulted, even if the default is to leave it undefined.
> 
> Making it proactive means the maintainer looking at it knows that yes, it is undefined for system X, rather than being overlooked.
> 
> And I agree that having a default be a particular value is just a land mine waiting to blow the next person's foot off.
> 
> 
> Sean Kelly wrote:
>> 
>> I don't know that the static assert is appropriate, since that stuff is optional, and the convention in the Posix headers is not to have an assert for undefined blocks, but it's no big deal to me either way. The 'else' clause was clearly wrong however.
>> 
>> Sent from my iPhone
>> 
>> On Feb 1, 2011, at 10:55 AM, Walter Bright <walter at digitalmars.com> wrote:
>> 
>>> Found this today in src/core/sys/posix/time.d:
>>> 
>>> version( linux )
>>> {
>>>     enum CLOCK_MONOTONIC        = 1;
>>>     enum CLOCK_MONOTONIC_RAW    = 4; // non-standard
>>>     enum CLOCK_MONOTONIC_COARSE = 6; // non-standard
>>> }
>>> else
>>> {
>>>     enum CLOCK_MONOTONIC        = 4;
>>> }
>>> 
>>> This is WRONG WRONG WRONG.
>>> 
>>> 1. it's wrong for OSX, which does not even define CLOCK_MONOTONIC
>>> 2. there's no guarantee that on "other" operating systems CLOCK_MONOTONIC will be defined, or if it is, that it will be 4.
>>> 3. you only find out there's a problem when something mysterious happens.
>>> 4. cut & pasting declarations from one operating system to another without checking is akin to dropping an anvil on the head of the hapless developer who can't figure out why there are mysterious failures on that other system. The developer then has to go back and hand-check every last one of those declarations.
>>> 
>>> This is not acceptable practice. Declarations for an OS should never be inserted or enabled until they are checked.
>>> 
>>> The corrected version is:
>>> 
>>> version( linux )
>>> {
>>>     enum CLOCK_MONOTONIC        = 1;
>>>     enum CLOCK_MONOTONIC_RAW    = 4; // non-standard
>>>     enum CLOCK_MONOTONIC_COARSE = 6; // non-standard
>>> }
>>> else version (FreeBSD)
>>> {   // time.h
>>>     enum CLOCK_MONOTONIC         = 4;
>>>     enum CLOCK_MONOTONIC_PRECISE = 11;
>>>     enum CLOCK_MONOTONIC_FAST    = 12;
>>> }
>>> else version (OSX)
>>> {
>>>     // No CLOCK_MONOTONIC defined
>>> }
>>> else
>>> {
>>>     static assert(0);
>>> }
>>> 
>>> Note that the assert will trip when a new OS is tried, thus pointing the developer at places that need to be checked.
>> 
>>> _______________________________________________
>>> D-runtime mailing list
>>> D-runtime at puremagic.com
>>> http://lists.puremagic.com/mailman/listinfo/d-runtime
>> 
>> _______________________________________________
>> D-runtime mailing list
>> D-runtime at puremagic.com
>> http://lists.puremagic.com/mailman/listinfo/d-runtime
>> 
> 
> _______________________________________________
> D-runtime mailing list
> D-runtime at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/d-runtime
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/d-runtime/attachments/20110201/593fc855/attachment-0001.html>
February 01, 2011
Not just easier but also safer.  On demand means that the new entries are much more likely to be exercised and thus known to be correct.

On 2/1/2011 8:03 PM, Sean Kelly wrote:
> If the default is not to define it then the user will get a compile error the first time he tries to use it. I'd love to proactively develop the Posix headers, but they're huge. It's easier to build them out as demand dictates.
> 
> Sent from my iPhone
> 
> On Feb 1, 2011, at 6:38 PM, Walter Bright <walter at digitalmars.com <mailto:walter at digitalmars.com>> wrote:
> 
>> The assert is to force the person porting the header to a new system to check to see what is appropriate for that system. I believe os specific code ought to be proactively written, not defaulted, even if the default is to leave it undefined.
>>
>> Making it proactive means the maintainer looking at it knows that yes, it is undefined for system X, rather than being overlooked.
>>
>> And I agree that having a default be a particular value is just a land mine waiting to blow the next person's foot off.
>>
>>
>> Sean Kelly wrote:
>>> I don't know that the static assert is appropriate, since that stuff is optional, and the convention in the Posix headers is not to have an assert for undefined blocks, but it's no big deal to me either way. The 'else' clause was clearly wrong however.
>>>
>>> Sent from my iPhone
>>>
>>> On Feb 1, 2011, at 10:55 AM, Walter Bright <<mailto:walter at digitalmars.com>walter at digitalmars.com <mailto:walter at digitalmars.com>> wrote:
>>>
>>>> Found this today in src/core/sys/posix/time.d:
>>>>
>>>> version( linux )
>>>> {
>>>>     enum CLOCK_MONOTONIC        = 1;
>>>>     enum CLOCK_MONOTONIC_RAW    = 4; // non-standard
>>>>     enum CLOCK_MONOTONIC_COARSE = 6; // non-standard
>>>> }
>>>> else
>>>> {
>>>>     enum CLOCK_MONOTONIC        = 4;
>>>> }
>>>>
>>>> This is *WRONG WRONG WRONG*.
>>>>
>>>> 1. it's wrong for OSX, which does not even define CLOCK_MONOTONIC
>>>> 2. there's no guarantee that on "other" operating systems CLOCK_MONOTONIC will be defined, or if it is, that it will
>>>> be 4.
>>>> 3. you only find out there's a problem when something mysterious happens.
>>>> 4. cut & pasting declarations from one operating system to another without checking is akin to dropping an anvil on
>>>> the head of the hapless developer who can't figure out why there are mysterious failures on that other system. The
>>>> developer then has to go back and hand-check every last one of those declarations.
>>>>
>>>> This is *not* acceptable practice. Declarations for an OS should never be inserted or enabled until they are checked.
>>>>
>>>> The corrected version is:
>>>>
>>>> version( linux )
>>>> {
>>>>     enum CLOCK_MONOTONIC        = 1;
>>>>     enum CLOCK_MONOTONIC_RAW    = 4; // non-standard
>>>>     enum CLOCK_MONOTONIC_COARSE = 6; // non-standard
>>>> }
>>>> else version (FreeBSD)
>>>> {   // time.h
>>>>     enum CLOCK_MONOTONIC         = 4;
>>>>     enum CLOCK_MONOTONIC_PRECISE = 11;
>>>>     enum CLOCK_MONOTONIC_FAST    = 12;
>>>> }
>>>> else version (OSX)
>>>> {
>>>>     // No CLOCK_MONOTONIC defined
>>>> }
>>>> else
>>>> {
>>>>     static assert(0);
>>>> }
>>>>
>>>> Note that the assert will trip when a new OS is tried, thus pointing the developer at places that need to be checked. _______________________________________________