Is GC.setAttr(p, GC.BlkAttr.NO_MOVE) guaranteed to work?

2014-07-02 Thread Ali Çehreli via Digitalmars-d-learn
There is an example in GC.addRoot() documentation where the programmer 
is trying to mark a memory block as NO_MOVE:


  http://dlang.org/phobos/core_memory.html#.GC.addRoot

auto context = new Object;
GC.addRoot(cast(void*)context);
GC.setAttr(cast(void*)context, GC.BlkAttr.NO_MOVE);

If I understand it correctly, as soon as addRoot() succeeds, the block 
may have already been moved perhaps due to the needs of another thread.


Will setAttr() still work if that happened? Perhaps the GC is supposed 
to track any previously used memory block reference like 'context' so 
that the call will succeed? (I doubt it.)


If the example can indeed fail, will swapping the last two statements 
work as in the following code?


auto context = new Object;
GC.setAttr(cast(void*)context, GC.BlkAttr.NO_MOVE);
GC.addRoot(cast(void*)context);

How about the reverse operations: Can I still use 'ctx' to refer to 
potentially moved memory block in the following code?


GC.removeRoot(ctx);
GC.clrAttr(ctx, GC.BlkAttr.NO_MOVE);

It seems to me that as a general rule, one cannot trust setting NO_MOVE 
on a GC-managed block at all. If that's true, addRoot() must have an 
overload that takes attributes as well and work atomically in that 
regard, right?


Ali


Re: Is GC.setAttr(p, GC.BlkAttr.NO_MOVE) guaranteed to work?

2014-07-02 Thread Rainer Schuetze via Digitalmars-d-learn



On 02.07.2014 08:17, Ali Çehreli wrote:

There is an example in GC.addRoot() documentation where the programmer
is trying to mark a memory block as NO_MOVE:

   http://dlang.org/phobos/core_memory.html#.GC.addRoot

 auto context = new Object;
 GC.addRoot(cast(void*)context);
 GC.setAttr(cast(void*)context, GC.BlkAttr.NO_MOVE);

If I understand it correctly, as soon as addRoot() succeeds, the block
may have already been moved perhaps due to the needs of another thread.


Yes, but the local variable context also has been changed to the new 
location by a moving GC (also any temporaries on the stack).




Will setAttr() still work if that happened? Perhaps the GC is supposed
to track any previously used memory block reference like 'context' so
that the call will succeed? (I doubt it.)

If the example can indeed fail, will swapping the last two statements
work as in the following code?

 auto context = new Object;
 GC.setAttr(cast(void*)context, GC.BlkAttr.NO_MOVE);
 GC.addRoot(cast(void*)context);

How about the reverse operations: Can I still use 'ctx' to refer to
potentially moved memory block in the following code?

 GC.removeRoot(ctx);
 GC.clrAttr(ctx, GC.BlkAttr.NO_MOVE);

It seems to me that as a general rule, one cannot trust setting NO_MOVE
on a GC-managed block at all. If that's true, addRoot() must have an
overload that takes attributes as well and work atomically in that
regard, right?

Ali


addRoot tells the GC that there are external references into GC managed 
memory. These references are not visible to the GC, so it cannot modify 
them when moving the referenced memory. As a consequence, addRoot 
implies NO_MOVE. I think the example is flawed.


The issue raised does show a rule to follow, though: do not store a GC 
pointer to external memory before calling addRoot() or setAttr(NO_MOVE), 
it might get moved after the assignment, but before being fixed.


BTW: The current GC has no support for moving, and the NO_MOVE flag 
isn't even stored anywhere (getAttr after setAttr will not report it 
being set).




Integer max value

2014-07-02 Thread pgtkda via Digitalmars-d-learn

Is there a way to get the max size of an integer?


Re: Integer max value

2014-07-02 Thread Rene Zwanenburg via Digitalmars-d-learn

On Wednesday, 2 July 2014 at 08:39:06 UTC, pgtkda wrote:

Is there a way to get the max size of an integer?


int.max

The same exists for other built-ins and enums.


Re: Integer max value

2014-07-02 Thread pgtkda via Digitalmars-d-learn

On Wednesday, 2 July 2014 at 08:58:46 UTC, Rene Zwanenburg wrote:

On Wednesday, 2 July 2014 at 08:39:06 UTC, pgtkda wrote:

Is there a way to get the max size of an integer?


int.max

The same exists for other built-ins and enums.


thank you very much



similar method as Math.Ceiling in c#

2014-07-02 Thread pgtkda via Digitalmars-d-learn

Is there a similar method as Math.Ceiling in D?

http://msdn.microsoft.com/en-US/en-en/library/zx4t0t48(v=vs.110).aspx


Re: similar method as Math.Ceiling in c#

2014-07-02 Thread via Digitalmars-d-learn

On Wednesday, 2 July 2014 at 13:36:17 UTC, pgtkda wrote:

Is there a similar method as Math.Ceiling in D?

http://msdn.microsoft.com/en-US/en-en/library/zx4t0t48(v=vs.110).aspx


http://dlang.org/phobos/std_math.html#.ceil


overloading InExpression

2014-07-02 Thread Vlad Levenfeld via Digitalmars-d-learn

Is this possible?

The documentation for std.container lists in as an operator in 
the container API but only associative arrays actually seem to 
support it.


Re: overloading InExpression

2014-07-02 Thread Dicebot via Digitalmars-d-learn

struct S
{
int opIn_r(int key)
{
return key*2;
}
}

void main()
{
assert((42 in S.init) == 84);
}


Re: Can't modify this

2014-07-02 Thread Maxim Fomin via Digitalmars-d-learn

On Saturday, 28 June 2014 at 20:40:21 UTC, Ary Borenszweig wrote:

This doesn't work:

class Foo {
  this() {
this = new Foo;
  }
}

Error: Cannot modify 'this'

However you can do this:

class Foo {
  this() {
auto p = this;
*p = new Foo();
  }
}

It even changes the value of this!

Should that compile? I mean, it's the same as modifying 
'this'...


D language was not aimed toward preventing any attepmt to 
circumvent some hypothetical limitations (it does not even cope 
with things which it should definetely prevent). There are holes 
much - much worse. And changing value of this parameter is not a 
problem anyway, since it is possible to have typeid(this) != 
typeid of type of current method which is bigger problem than 
pointing to different value of same type.


Re: overloading InExpression

2014-07-02 Thread Vlad Levenfeld via Digitalmars-d-learn

On Wednesday, 2 July 2014 at 14:14:57 UTC, Dicebot wrote:

struct S
{
int opIn_r(int key)
{
return key*2;
}
}

void main()
{
assert((42 in S.init) == 84);
}


Thanks! I wonder, why the _r and lack of documentation?


Re: overloading InExpression

2014-07-02 Thread Ali Çehreli via Digitalmars-d-learn

On 07/02/2014 07:35 AM, Vlad Levenfeld wrote:

On Wednesday, 2 July 2014 at 14:14:57 UTC, Dicebot wrote:

struct S
{
int opIn_r(int key)
{
return key*2;
}
}

void main()
{
assert((42 in S.init) == 84);
}


Thanks! I wonder, why the _r


I think it is the old syntax, meaning this is the overload for when an 
S object is on the right-hand side.


 and lack of documentation?

It appears in the binary operator table:

  http://dlang.org/operatoroverloading.html#Binary

Also see the section 'Inclusion query by opBinaryRight!in' here:

  http://ddili.org/ders/d.en/operator_overloading.html

Ali



Re: overloading InExpression

2014-07-02 Thread Dicebot via Digitalmars-d-learn

On Wednesday, 2 July 2014 at 15:36:23 UTC, Kozzi11 wrote:

Thanks! I wonder, why the _r and lack of documentation?


Maybe something from old days? But in current a
href=http://dlang.org/operatoroverloading.html#Binary;
target=_blankdoc/a there is a opBinary:


Yep, I think it is D1 legacy approach. opBinary should be more 
appropriate.


Re: overloading InExpression

2014-07-02 Thread Kozzi11 via Digitalmars-d-learn

On Wednesday, 2 July 2014 at 14:35:55 UTC, Vlad Levenfeld wrote:

On Wednesday, 2 July 2014 at 14:14:57 UTC, Dicebot wrote:

struct S
{
int opIn_r(int key)
{
return key*2;
}
}

void main()
{
assert((42 in S.init) == 84);
}


Thanks! I wonder, why the _r and lack of documentation?


Maybe something from old days? But in current a
href=http://dlang.org/operatoroverloading.html#Binary;
target=_blankdoc/a there is a opBinary:

struct S
{
 int opBinaryRight(string op : in)(int key)
 {
 return key*2;
 }
}

void main()
{
 assert((42 in S.init) == 84);
}


Re: overloading InExpression

2014-07-02 Thread bearophile via Digitalmars-d-learn

Dicebot:

Yep, I think it is D1 legacy approach. opBinary should be more 
appropriate.


I hope the usage of the old operator overloading functions will 
generate deprecation messages soon.


Bye,
bearophile


Building D on windows

2014-07-02 Thread Kashyap via Digitalmars-d-learn

Hi,
I was able to build dmd on my windows machine using VS2013 (using 
the vs2010 sln file). However, I am not sure how to proceed with 
building the druntime and phobos. Could someone please point me 
to instructions for the same?


I tried the following

1. Downloaded make and shell (digital mars)
2. created a shell file script.sh that contains the following
make -f posix.mak 
DMD=../dmd/src/vcbuild/Win32/Release/dmd_msc.exe\

3. Executed the sh file shell script.sh

I got the following error -
C:\Users\labuser\dlang\druntimedir 
..\dmd\src\vcbuild\Win32\Release\dmd_msc.exe



C:\Users\labuser\dlang\druntimeshell script.sh
shell 1.01
make -f posix.mak DMD=../dmd/src/vcbuild/Win32/Release/dmd_msc.exe
Error on line 10: '=' is not a valid filename char

--- errorlevel 1

Regards,
Kashyap


Re: Building D on windows

2014-07-02 Thread Vladimir Panteleev via Digitalmars-d-learn

On Wednesday, 2 July 2014 at 16:06:08 UTC, Kashyap wrote:

I got the following error -
C:\Users\labuser\dlang\druntimedir 
..\dmd\src\vcbuild\Win32\Release\dmd_msc.exe



C:\Users\labuser\dlang\druntimeshell script.sh
shell 1.01
make -f posix.mak 
DMD=../dmd/src/vcbuild/Win32/Release/dmd_msc.exe

Error on line 10: '=' is not a valid filename char

--- errorlevel 1


Hi,

It looks like you are trying to use the posix makefile with 
Digital Mars make. posix.mak uses GNU make syntax - the makefile 
for DigitalMars make is win32.mak. So you will need to either 
make sure GNU make comes first in your PATH, or simply use 
win32.mak instead.


There is a wiki page with instructions for building D from source:
http://wiki.dlang.org/Building_DMD


Re: Why is the Win32 boilerplate the way it is?

2014-07-02 Thread Vladimir Panteleev via Digitalmars-d-learn

On Sunday, 29 June 2014 at 15:06:25 UTC, Jeremy Sorensen wrote:
The only question I have is what happens when you use 
SUBSYSTEM:WINDOWS:4.0 (Which I understand means XP or higher) 
and the program runs on something older?


Windows XP is version 5.1.
4.0 was Windows NT 4 (which I believe was the NT-family Windows 
version preceding Windows 2000).


Re: overloading InExpression

2014-07-02 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Jul 02, 2014 at 02:35:54PM +, Vlad Levenfeld via 
Digitalmars-d-learn wrote:
 On Wednesday, 2 July 2014 at 14:14:57 UTC, Dicebot wrote:
 struct S
 {
  int opIn_r(int key)
  {
  return key*2;
  }
 }
 
 void main()
 {
  assert((42 in S.init) == 84);
 }
 
 Thanks! I wonder, why the _r and lack of documentation?

Don't use opIn_r, that's a relic from D1. Instead, use this:

struct S {
int opBinaryRight(string op)(int key)
if (op == in)
{
return key*2;
}
}


T

-- 
I think Debian's doing something wrong, `apt-get install pesticide', doesn't 
seem to remove the bugs on my system! -- Mike Dresser


Re: overloading InExpression

2014-07-02 Thread Vlad Levenfeld via Digitalmars-d-learn

Ah yes I never noticed that in was in the binary op table. In
my defense, searching for in usually yields too many results to
be useful. Thanks to everyone for your help!


CTFE bug or enhancement?

2014-07-02 Thread safety0ff via Digitalmars-d-learn

Everything compiles fine except for function qux2:
http://dpaste.dzfl.pl/9d9187e0b450

Is this a bug or an enhancement for CTFE?

It would be really nice to have this feature because core.simd 
has functions such as: void16 __simd(XMM opcode, void16 op1, 
void16 op2, ubyte imm8);


Where all the arguments must be compile time constants.

It would be nice to be able to push some parameters out from the 
type list and into the argument list in user code too.


Re: CTFE bug or enhancement?

2014-07-02 Thread safety0ff via Digitalmars-d-learn

Actually, this is an enhancement because adding:
enum b = blah

Makes them fail. :(


Re: CTFE bug or enhancement?

2014-07-02 Thread safety0ff via Digitalmars-d-learn

On Thursday, 3 July 2014 at 01:55:14 UTC, safety0ff wrote:

Actually, this is an enhancement because adding:
enum b = blah

Makes them fail. :(


The question is now: how can the delegate be evaluated for the 
return value but not for the enum?


Re: CTFE bug or enhancement?

2014-07-02 Thread safety0ff via Digitalmars-d-learn

On Thursday, 3 July 2014 at 02:02:19 UTC, safety0ff wrote:

On Thursday, 3 July 2014 at 01:55:14 UTC, safety0ff wrote:

Actually, this is an enhancement because adding:
enum b = blah

Makes them fail. :(


The question is now: how can the delegate be evaluated for the 
return value but not for the enum?


Looks like an ICE: 
https://github.com/D-Programming-Language/dmd/blob/master/src/interpret.c#L5169


spawn and wait

2014-07-02 Thread Puming via Digitalmars-d-learn

Hi,

I want to spawn several similar tasks and then wait for all of 
them to complete to go on do some other things, like:


```d
void task(int id)
{
  // do the stuff
}

void main()
{
  foreach (i; 0..10) {
spawn(task, i);
  }
  wait(?); // wait for all task to complete
  doSomeOtherThings();
}
```

But I don't see a `wait` method for Tid, similar to Pid in 
std.process.


What is the idiomatic way to do these things?

My current workaround is using messages:

```d
#!/usr/bin/rdmd
import std.stdio;
import std.concurrency;

void child(int id)
{
writeln(Starting child: , id);
ownerTid.send(id);
}

void main()
{
foreach (i; 0..10)
{
spawn(child, i);
}
for (int n = 0; n  10; ++n) {
receive((int i) {
writeln(Received:, i);
});
}
}
```

But it is verbose and seems error prone.


DUB help plz

2014-07-02 Thread K.K. via Digitalmars-d-learn

I've been trying to figure DUB out and have been having a bit of
trouble. I'm trying to make a simple program that uses Derelict
GL3 and SDL2. I keep getting this error though:

derelict.util.exception.SharedLibLoadException@C:\Users\Kyoji\AppData\Roaming\dub\packages\derelict-util-1.0.1\source\derelict\util\exception.d(35):
Failed to load one or more shared libraries:
 SDL2.dll - The specified module could not be found.

I'm not sure how to get that .dll, I tried doing 'dub upgrade'
but that didn't do much.

Here's what some of my files look like:
app.d :

import std.stdio;
import derelict.opengl3.gl3;
import derelict.sdl2.sdl;

void main()
{
writeln(Edit source/app.d to start your project.);
DerelictGL3.load();
DerelictSDL2.load();
DerelictGL3.reload();
}
---

dub.json :

---

{
name: testbuild,
description: A minimal D application.,
copyright: Copyright © 2014, Kyoji,
authors: [Kyoji],
importPaths: [lib/],
dependencies: {
derelict-gl3: =1.0.2,
derelict-sdl2: =1.2.1,

}
}

--

I setup a lib folder from the project root, and I have the source
files for the packages under 'import'.

Is the only thing I'm missing the .dll's?

Thanks!


Re: spawn and wait

2014-07-02 Thread Ali Çehreli via Digitalmars-d-learn

On 07/02/2014 08:29 PM, Puming wrote:

 I want to spawn several similar tasks and then wait for all of them to
 complete to go on do some other things

If you don't care about account for each of them individually, 
core.thread.thread_joinAll would work. The following program starts two 
waves of threads and waits for both of the waves to complete:


import std.stdio;
import std.concurrency;
import core.thread;

void foo(Duration duration)
{
writefln(Working for %s, duration);
Thread.sleep(duration);
}

void spawnThreads(size_t count)
{
foreach (i; 0 .. count) {
spawn(foo, (i + 1).seconds);
}
writefln(Started %s workers, count);
}

void main()
{
spawnThreads(2);
writefln(Waiting for all to finish);
thread_joinAll();

spawnThreads(3);
writefln(Waiting for all to finish);
thread_joinAll();
}

Ali