D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 1091 - Wrong size reserved for critical sections
Summary: Wrong size reserved for critical sections
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 Linux
: P2 normal
Assignee: Walter Bright
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2007-04-02 18:49 UTC by Frits van Bommel
Modified: 2020-05-19 05:43 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Frits van Bommel 2007-04-02 18:49:43 UTC
It looks like DMD on Linux only reserves 4 bytes for a critical section used to implement a synchronized {} block, while it should be at least 28 bytes.
(A pointer to this is passed to _d_criticalenter, which takes a pointer to the following (C) struct:
---
// from phobos/internal/critical.c
typedef struct D_CRITICAL_SECTION

{

    struct D_CRITICAL_SECTION *next;

    pthread_mutex_t cs;

} D_CRITICAL_SECTION;
---
According to my (32-bit) GCC the entire structure totals 28 bytes)

This manifests itself in the following (synthetic) code:
---
import std.stdio;

void main()
{
        synchronized {
                synchronized {
                        writefln("Hello world!");
                }
        }
}
---
The critical sections in the .data section are 4 bytes apart, yet should be 28 bytes large. Overlapping is _bad_.
This code hangs when ran on my computer, but it's probably subject to the whim of the pthreads implementation...
(Note: this is obviously useless code, I constructed it after figuring out what the problem was)

AFAICT above D code should have been roughly equivalent to the following C code (linked to Phobos):
---
#include <stdio.h>
#include <pthread.h>
#include <string.h>

// from phobos/internal/critical.c
typedef struct D_CRITICAL_SECTION
{
    struct D_CRITICAL_SECTION *next;
    pthread_mutex_t cs;
} D_CRITICAL_SECTION;

D_CRITICAL_SECTION c1, c2;

int main() {
	// not sure if this is necessary, but the space DMD
	// reserves is also zeroed, so...
	memset(&c1, 0, sizeof(D_CRITICAL_SECTION));
	memset(&c2, 0, sizeof(D_CRITICAL_SECTION));
	
	_d_criticalenter(&c1);
	_d_criticalenter(&c2);
	printf("Hello world!\n");
	_d_criticalexit(&c2);
	_d_criticalexit(&c1);
	
	return 0;
}
---
Which runs just fine.


(I used keyword "wrong-code" because "wrong-data" wasn't on the list ;) )
Comment 2 Walter Bright 2007-04-20 13:18:08 UTC
Fixed DMD 1.013
Comment 3 Dlang Bot 2020-05-19 05:43:05 UTC
dlang/dub pull request #1946 "Make the testsuite re-runnable on OSX" was merged into master:

- d51ae6b8bccea6d91c0c18e16c5326d83de56b44 by Geod24:
  testsuite: Make test for issue1091 re-runnable
  
  After the initial run, one couldn't run this test again as the expected message
  'building configuration' would not be printed anymore.

https://github.com/dlang/dub/pull/1946