Re: How to initialize a globle variable nicely and properly?

2018-12-14 Thread Neia Neutuladh via Digitalmars-d-learn
On Sat, 15 Dec 2018 02:54:55 +, Heromyth wrote:
>  shared static this() {
>  writeln("running A in shared static this(),
> sharedField=", sharedField);
> 
>  Thread th = new Thread(() {  });
>  th.start();

When you start a D thread, thread-local static constructors get run. So 
don't start threads until your shared static constructors finish.

If I encountered something like this, I would set up a queue of actions to 
be run at the start of main() and fill them with static constructors. 
Instead of this static constructor creating a thread, it would enqueue a 
delegate that starts the thread.


Re: How to initialize a globle variable nicely and properly?

2018-12-14 Thread Rubn via Digitalmars-d-learn

On Saturday, 15 December 2018 at 02:54:55 UTC, Heromyth wrote:
We have a module including many globle variables which are 
needed to be initialized firstly in "shared static this() {}", 
see here 
https://github.com/huntlabs/hunt/blob/master/source/hunt/time/Init.d.


The problem is that these variables are not always initialized 
firstly when are referenced by some others moudles in "static 
this() {}". Here is a demo to illustrate it.


//
// module A
//
module test.A;

import std.stdio;
import core.thread;
import core.time;

class A {
__gshared int sharedField;

shared static this() {
writeln("running A in shared static this(), 
sharedField=", sharedField);


Thread th = new Thread(() {  });
th.start();

Thread.sleep(100.msecs);
sharedField = 2;
writeln("running A done in shared static this(), 
sharedField=", sharedField);

}

static this() {
writeln("running A in static this(), sharedField=", 
sharedField);

}
}

//
// module B
//
module test.B;

import test.A;
import std.stdio;
import core.thread;

shared static this() {
writeln("running in shared static this() from B");
}

class B {
shared static this() {
writeln("running B in shared static this(), 
sharedField=", A.sharedField);

}

static this() {
// bug is here
writeln("running B in static this(), sharedField=", 
A.sharedField);

}
}

//
// module main
//
import std.stdio;

import test.A;
import core.thread;

void main()
{
writeln("running in main.");
}

//
// output
//
Running ./demo
running A in shared static this(), sharedField=0
running A in static this(), sharedField=0
running B in static this(), sharedField=0  // bug is here
running A done in shared static this(), sharedField=2
running in shared static this() from B
running B in shared static this(), sharedField=2
running A in static this(), sharedField=2
running B in static this(), sharedField=2
running main.



You can see the sharedField is 0 in B's static this() at first. 
If Thread is disabled to run in shared static this(), this 
problem seems to be fixed.


Some related bugs:
1) https://issues.dlang.org/show_bug.cgi?id=6114
2) https://issues.dlang.org/show_bug.cgi?id=4923


The problem here is that you are creating a new thread in the 
`shared static this()`.


shared static this() {
writeln("running A in shared static this(), 
sharedField=", sharedField);
Thread th = new Thread(() {  }); // Calls `static this()` 
including B's `static this()`

th.start();
Thread.sleep(100.msecs);
sharedField = 2;
writeln("running A done in shared static this(), 
sharedField=", sharedField);

}

static this() {
writeln("running A in static this(), sharedField=", 
sharedField);

}

Creating a new thread causes those thread's constructor `static 
this()` to be called. You need to create your threads somewhere 
else or have the values be initialized before the threads are 
created.


How to initialize a globle variable nicely and properly?

2018-12-14 Thread Heromyth via Digitalmars-d-learn
We have a module including many globle variables which are needed 
to be initialized firstly in "shared static this() {}", see here 
https://github.com/huntlabs/hunt/blob/master/source/hunt/time/Init.d.


The problem is that these variables are not always initialized 
firstly when are referenced by some others moudles in "static 
this() {}". Here is a demo to illustrate it.


//
// module A
//
module test.A;

import std.stdio;
import core.thread;
import core.time;

class A {
__gshared int sharedField;

shared static this() {
writeln("running A in shared static this(), 
sharedField=", sharedField);


Thread th = new Thread(() {  });
th.start();

Thread.sleep(100.msecs);
sharedField = 2;
writeln("running A done in shared static this(), 
sharedField=", sharedField);

}

static this() {
writeln("running A in static this(), sharedField=", 
sharedField);

}
}

//
// module B
//
module test.B;

import test.A;
import std.stdio;
import core.thread;

shared static this() {
writeln("running in shared static this() from B");
}

class B {
shared static this() {
writeln("running B in shared static this(), 
sharedField=", A.sharedField);

}

static this() {
// bug is here
writeln("running B in static this(), sharedField=", 
A.sharedField);

}
}

//
// module main
//
import std.stdio;

import test.A;
import core.thread;

void main()
{
writeln("running in main.");
}

//
// output
//
Running ./demo
running A in shared static this(), sharedField=0
running A in static this(), sharedField=0
running B in static this(), sharedField=0  // bug is here
running A done in shared static this(), sharedField=2
running in shared static this() from B
running B in shared static this(), sharedField=2
running A in static this(), sharedField=2
running B in static this(), sharedField=2
running main.



You can see the sharedField is 0 in B's static this() at first. 
If Thread is disabled to run in shared static this(), this 
problem seems to be fixed.


Some related bugs:
1) https://issues.dlang.org/show_bug.cgi?id=6114
2) https://issues.dlang.org/show_bug.cgi?id=4923



Re: Map of functions

2018-12-14 Thread Francesco Mecca via Digitalmars-d-learn

On Friday, 14 December 2018 at 17:45:26 UTC, rjframe wrote:

On Fri, 14 Dec 2018 16:33:44 +, Giovanni Di Maria wrote:



I need the flow of calls.
Thank you Giovanni


gprof will do this on Linux/BSD if gdc supports the -pg flag (I 
don't know whether it would, but assume so) and your 
application is working.



From code, you'd need to call a trace function on function 
entry. Mine lets you also specify a message (parameter values, 
etc.):


---
void trace(T...)(T args, string func = __FUNCTION__) {
import std.stdio : writeln;
if (args.length > 0) {
debug writeln("*trace: ", func, "- ", args);
} else {
debug writeln("*trace: ", func);
}
}

void main(int a, string b) {
trace();
trace(a, ", ", b);
}
---


You can also generate a call graph using callgrind.
That however will slow down your program.

There are some python script that converts the output of 
callgrind to a dot file that can be converted to svg.


https://stackoverflow.com/questions/33769323/make-callgrind-show-all-function-calls-in-the-kcachegrind-callgraph


Re: Anonymous mapped regions increases unlimitely on spawn

2018-12-14 Thread Boris-Barboris via Digitalmars-d-learn

On Friday, 14 December 2018 at 21:22:05 UTC, unDEFER wrote:

So it looks like a bug, and I have reported about it:

https://issues.dlang.org/show_bug.cgi?id=19487


Not an expert, but you may wish to try GC.minimize() 
(https://dlang.org/phobos/core_memory.html#.GC.minimize).


Re: Anonymous mapped regions increases unlimitely on spawn

2018-12-14 Thread unDEFER via Digitalmars-d-learn

So it looks like a bug, and I have reported about it:

https://issues.dlang.org/show_bug.cgi?id=19487


Re: Indentation in emacs d-mode

2018-12-14 Thread Ali Çehreli via Digitalmars-d-learn

On 12/14/2018 05:37 AM, J-S wrote:

I'm sorry for asking such a stupid question, but can anybody point me to 
a precooked consistent set of customizations?


Good question. My Emacs D editing is suboptimal as well.

Ali


Re: Anonymous mapped regions increases unlimitely on spawn

2018-12-14 Thread unDEFER via Digitalmars-d-learn

So more digging..
dtor of Thread calls in GC.collect() if thread is finished.
But it's do nothing because

bool not_registered = !next && !prev && (sm_tbeg !is this);

is always true... So how to register the thread?


Re: Anonymous mapped regions increases unlimitely on spawn

2018-12-14 Thread unDEFER via Digitalmars-d-learn
So in digging by this problem, I have made simple patch to 
druntime. I have added in druntime/src/core/thread.d to


final Thread start() nothrow

of class Thread

import core.stdc.stdio;
printf("start Thread\n");


And to

~this() nothrow @nogc

import core.stdc.stdio;
printf("detach Thread\n");

I recompiled phobos to apply changes to druntime and I see that 
"start Thread" there is, but "detach Thread" printed only at the 
end of process even if I do GC.collect().


So dtor of class Thread doesn't call on GC. Why?


Re: Map of functions

2018-12-14 Thread Giovanni Di Maria via Digitalmars-d-learn

Thank you very  much,
Fantastic!!!
Giovanni



Re: Map of functions

2018-12-14 Thread rjframe via Digitalmars-d-learn
On Fri, 14 Dec 2018 16:33:44 +, Giovanni Di Maria wrote:

> 
> I need the flow of calls.
> Thank you Giovanni

gprof will do this on Linux/BSD if gdc supports the -pg flag (I don't know 
whether it would, but assume so) and your application is working.


>From code, you'd need to call a trace function on function entry. Mine 
lets you also specify a message (parameter values, etc.):

---
void trace(T...)(T args, string func = __FUNCTION__) {
import std.stdio : writeln;
if (args.length > 0) {
debug writeln("*trace: ", func, "- ", args);
} else {
debug writeln("*trace: ", func);
}
}

void main(int a, string b) {
trace();
trace(a, ", ", b);
}
---


Anonymous mapped regions increases unlimitely on spawn

2018-12-14 Thread unDEFER via Digitalmars-d-learn

Hello!
I have the program which uses BDB and while testing often makes 
spawn. And after 12 hours of testing bdb said:

mmap: Cannot allocate memory

But the problem that I've found that it is not BDB created too 
many maps. Watching for /proc/[PID]/maps shows that number of 
anonymous mapped regions increases on 2 every spawn process, and 
never decreases even after finishing the spawned thread.
According to logs my program made 32543 spawns for test time. And 
my /proc/sys/vm/max_map_count = 65530. So only 444 maps was 
allocated by other reasons and 65086 by spawn.


So what to do? How to make spawn decrease count of anonymous 
mapped regions?


Re: Map of functions

2018-12-14 Thread Giovanni Di Maria via Digitalmars-d-learn


Do you really have a nested function print() inside a nested 
function calculate() inside main()?  That is,



Hi
this is only an example of names of funcions but the structure is 
this:


void main()
{
// maybe some calls to calculate()
}

void calculate()
{
// maybe some calls to print()
}

void print()
{
}



I need the flow of calls.
Thank you
Giovanni




Re: Map of functions

2018-12-14 Thread Ivan Kazmenko via Digitalmars-d-learn
On Friday, 14 December 2018 at 15:38:49 UTC, Giovanni Di Maria 
wrote:

Hi
Is there an utility to print
the functions in a source file, for example:

- main()
--- calculate()
- print()
--- simulate()
- print()
.
Thank you very much
Giovanni Di Maria


Do you really have a nested function print() inside a nested 
function calculate() inside main()?  That is,


void main()
{
void calculate()
{
void print()
{
}
// maybe some calls to print()
}
// maybe some calls to calculate()
}

Or are you talking about the sequence in which function are 
*called*, and from where?  Please clarify.


Ivan Kazmenko.



Map of functions

2018-12-14 Thread Giovanni Di Maria via Digitalmars-d-learn

Hi
Is there an utility to print
the functions in a source file, for example:

- main()
--- calculate()
- print()
--- simulate()
- print()
.
Thank you very much
Giovanni Di Maria



Indentation in emacs d-mode

2018-12-14 Thread J-S via Digitalmars-d-learn
I've tried (and failed) to get my emacs (26.1) to properly handle 
indentation (as per the "D style") in d-mode (2.0.9).


Things that don't work:
- aligning braces below keywords like "foreach", "while", "in" 
etc: when I put the braces on the following line, I get a 4-space 
indent (while the correct behaviour should be: align);
- the "in" keyword itself gets indented 4 spaces w.r.t. the 
function name (I again get a 4-space indent, while the correct 
behaviour should be: align).


class foo {
private:
real _bar;

public:
creal bar;

void fiz()
in
{
foreach (...



I'm sorry for asking such a stupid question, but can anybody 
point me to a precooked consistent set of customizations?






Re: 2D-all?

2018-12-14 Thread Nicholas Wilson via Digitalmars-d-learn

On Friday, 14 December 2018 at 12:43:40 UTC, berni wrote:
I've got a lot of code with two-dimensional arrays, where I use 
stuff like:



assert(matrix.all!(a=>a.all!(b=>b>=0)));


Does anyone know if there is a 2D-version of all so I can write 
something like:



assert(matrix.all2D!(a=>a>=0));


auto all2d(alias f)(ref Matrix m)
{
 return m.all!(a => a.all!(b => f(b)));
}


2D-all?

2018-12-14 Thread berni via Digitalmars-d-learn
I've got a lot of code with two-dimensional arrays, where I use 
stuff like:



assert(matrix.all!(a=>a.all!(b=>b>=0)));


Does anyone know if there is a 2D-version of all so I can write 
something like:



assert(matrix.all2D!(a=>a>=0));


Re: Print RAM used by arrays

2018-12-14 Thread Giovanni Di Maria via Digitalmars-d-learn

Thank you very much for your
precious informations.
Now i will try.
Thank you!!!
Giovanni