www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - win32 waitnotify library & requirement of dummy var ?

reply Dan <ddaglas gmail.com> writes:
All,

The attached zip file contains a Win32 implementation for Java's
wait()/notify()/notifyAll() functionality for D.

If you examine the source file waitnotify.d, it contains the following
declaration in the WaitNotifyObject class:

public bool dummy = false;

It is an unused variable.  With it in there, the library works fine.  When I
take out the dummy variable, Access Violations occur.  (Try compiling by
issuing "make" and run "main.exe".)

Why is this happening?  Is it a problem with the compiler?  With the public.d
/ privateImpl.d paradigm in use?  Is it an alignment issue?

--Dan
Dec 18 2006
parent reply catrino <catrino cbs.cnrs.fr> writes:
Thank you for your waitnotify library. I was looking for something like this. I
have implemented something myself but your implementation is better. I believe
these wait(), notify() functionalities should be part of the Object class, which
means to recompile phobos. The problem is that it is not possible to add members
to the Object class. I've tried that and the compiler fails on an assertion on
the
Object size.

For your SIGSEGV problem, I believe that it is due to the public/private
implementation. In your waitnotifyImpl.d you added an int member to the
WaitNotifyObject. It means that the size of a WaitNotifyObject will be the size
of
Object + the size of an int. In your public waitnotify.d, if you don't add a
member the WaitNotifyObject will appear to have the same size of a basic Object
but it is bigger in fact. I believe the hidden int member gets altered by other
objects in memory, the place where it lies looks available. I had similar
problems
when I was playing with phobos (adding members to classes and recompiling).

Another thing that is important when using public/private implementation is to
always keep the methods in the same order. This is because the D compiler stores
the methods in the virtual function table in the order they appear in the source
file. For example if you have a public file like this :
class A {
   void fun();
   void afun();
}
and a private file like this
class A {
   void afun() { ... }
   void fun() { ... }
}
then when you will call A.fun() from a program importing your public d file and
linked with your already compiled private implementation you will have A.afun()
called instead. The same happens when you try to call A.afun(), you call A.fun()
instead.
I had this problem too.

I hope you could read my poor english and that I could help you.

Vincent
Dec 19 2006
parent Dan <ddaglas gmail.com> writes:
== Quote from catrino (catrino cbs.cnrs.fr)'s article
 Thank you for your waitnotify library. I was looking for something like this. I
 have implemented something myself but your implementation is better. I believe
 these wait(), notify() functionalities should be part of the Object class,
which
 means to recompile phobos. The problem is that it is not possible to add
members
 to the Object class. I've tried that and the compiler fails on an assertion on
the
 Object size.
 For your SIGSEGV problem, I believe that it is due to the public/private
 implementation. In your waitnotifyImpl.d you added an int member to the
 WaitNotifyObject. It means that the size of a WaitNotifyObject will be the
size of
 Object + the size of an int. In your public waitnotify.d, if you don't add a
 member the WaitNotifyObject will appear to have the same size of a basic Object
 but it is bigger in fact. I believe the hidden int member gets altered by other
 objects in memory, the place where it lies looks available. I had similar
problems
 when I was playing with phobos (adding members to classes and recompiling).
 Another thing that is important when using public/private implementation is to
 always keep the methods in the same order. This is because the D compiler
stores
 the methods in the virtual function table in the order they appear in the
source
 file. For example if you have a public file like this :
 class A {
    void fun();
    void afun();
 }
 and a private file like this
 class A {
    void afun() { ... }
    void fun() { ... }
 }
 then when you will call A.fun() from a program importing your public d file and
 linked with your already compiled private implementation you will have A.afun()
 called instead. The same happens when you try to call A.afun(), you call
A.fun()
 instead.
 I had this problem too.
 I hope you could read my poor english and that I could help you.
 Vincent

Vincent, I appreciate the advice. I will try a few things, like experimenting with volatile, making sure methods are in the same order, etc. Thanks. Dan
Dec 20 2006