Re: Minimize lock time

2010-06-11 Thread Justin Spahr-Summers
On Thu, 10 Jun 2010 12:42:09 +0200, Simen kjaeraas 
simen.kja...@gmail.com wrote:
 
 Kagamin s...@here.lot wrote:
 
  Let's consider the following code:
 
  synchronized(syncRoot)
  {
if(condition)opSuccess();
else writeln(possibly,slow);
  }
 
  Suppose the else close doesn't need to be executed in lock domain and  
  can be slow. How to minimize lock time here?
 
  synchronized(syncRoot)
  {
if(condition)opSuccess();
else goto Lwrite;
  }
  Lwrite: writeln(possibly,slow);
 
  We can do this... but...
 
 A flag, as has been mentioned, would work.
 
 Other solutions that might work:
 
 
 synchronized(syncRoot)
 {
if (condition)
{
  opSuccess();
  return;
}
 }
 writeln(possibly,slow);
 
 
 do
 {
synchronized(syncRoot)
{
  if (condition)
  {
opSuccess();
break;
  }
}
writeln(possibly, slow);

All good suggestions. synchronized(), in general, just doesn't scale 
very well (though no fault of D's). If you're doing very complex things, 
you might have better luck with a semaphore of some kind. As always, 
though, avoiding the need for synchronization is best.


Re: dsss build, tango, GC ?

2010-06-11 Thread Jacob Carlborg

On 2010-06-10 23:20, Ferd Burton wrote:

== Quote from Jacob Carlborg (d...@me.com)'s article

On 2010-06-09 21:16, Fred Burton wrote:

Hi, I have (sort of) 2 questions:

Background:
I was doing some coding using Phobos, and recently switched to using Tango.

1. I'm trying to build using dsss and it says :
/usr/bin/ld: cannot find -lphobos
collect2: ld returned 1 exit status
--- errorlevel 1
Command /usr/bin/rebuild returned with code 65280, aborting.
Error: Command failed, aborting.

You have to change your default rebuild configuration file. It's located
in /etc/rebuild/default on Posix systems. Change the profile value to
dmd-posix-tango. Looking at the dmd-posix file, you can see, in the
link section, that it explicitly links to phobos which is not
necessary (at least not anymore). That's why you get the linker error.


Thanks.

(trying...)

okay, now it gives:

/usr/bin/ld: cannot find -ltango-base-dmd
collect2: ld returned 1 exit status
--- errorlevel 1
Command /usr/bin/rebuild returned with code 65280, aborting.
Error: Command failed, aborting.

hmm... I have a
/usr/lib/libtango-dmd.a

editing dmd-posix-tango to go from
 cmd=dmd -L--start-group -L-ltango-base-dmd $i -of$o
to
 cmd=dmd -L--start-group -L-ltango-dmd $i -of$o
works!


Previously (when that config file was written) tango was split in two 
libraries: base and user. Now it's just one library.



Now it seems to cleanly build with tango.


That's good to hear



It does still leak memory though ( ran for 4 minutes, 20 seconds and:

==3411== 237,749,649 bytes in 515,441 blocks are definitely lost in loss record 
1,024 of 1,024
==3411==at 0x4024918: malloc (in 
/usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3411==by 0x806D11C: gc_malloc (in /home/fburton/lang/d/guigl/glgui)
==3411==by 0x8069670: _d_arrayappendcT (in /home/fburton/lang/d/guigl/glgui)
==3411==by 0x8064F17: 
_D5tango4text4Util12__T5splitTaZ5splitFAaAaZAAa15__foreachbody24MFKAaZi (in
/home/fburton/lang/d/guigl/glgui)
==3411==by 0x8065013: 
_D5tango4text4Util20__T12PatternFructTaZ12PatternFruct7opApplyMFDFKAaZiZi (in
/home/fburton/lang/d/guigl/glgui)
==3411==by 0x8064EED: _D5tango4text4Util12__T5splitTaZ5splitFAaAaZAAa (in 
/home/fburton/lang/d/guigl/glgui)
==3411==by 0x8065B9C: 
_D3flb3gui8wordwrap9word_wrapFAAafC3flb3gui9themebase10IStyleTextZAAa (in
/home/fburton/lang/d/guigl/glgui)
==3411==by 0x80659BE: 
_D3flb3gui8somewind13ACustomWindow4drawMFC3flb3gui7guibase13Idraw_contextZv (in
/home/fburton/lang/d/guigl/glgui)
==3411==by 0x8063672: 
_D3flb3gui7guibase14window_manager4drawMFC3flb3gui7guibase13Idraw_contextZv (in
/home/fburton/lang/d/guigl/glgui)
==3411==by 0x804A2C9: _D3flb3gui5glgui5GlGui3runMFZv (in 
/home/fburton/lang/d/guigl/glgui)
==3411==by 0x804A33B: _Dmain (in /home/fburton/lang/d/guigl/glgui)
==3411==by 0x8068133: _D2rt8compiler3dmd2rt6dmain24mainUiPPaZi7runMainMFZv 
(in /home/fburton/lang/d/guigl/glgui)
==3411==

)


I guess I can't help you with that one.

--
/Jacob Carlborg


Arithmetic conversions and a surprise with 'max'

2010-06-11 Thread Ali Çehreli
The following program demonstrates a problem that I just hit. It is a 
known gotcha of arithmetic conversion rules.


The program is trying to center some text around an index of a char 
array. To avoid negative index values, it calls 'max' to limit the 
starting offset at 0.


import std.algorithm;

void main()
{
// An empty line
char[10] line = ' ';

// We want to center some text around the first quarter mark
int center_of_text = line.length / 4;
string text = 01234567;

// To be safe, we want to limit the starting index at 0.
// (No negative index please!)
int start = max(0, center_of_text - text.length / 2);

assert(start = 0); // FAILS!
}

The problem is due to converting the second argument of max to unsigned.

Ali


using std.process

2010-06-11 Thread Philippe Sigaud
OK, this is a real newbie question:

How can I use std.process?

when I do:

import std.process;

void main() {
execvp(mkdir, [test]);
}

nothing happens.

What am I doing wrong?
I'm on Vista there, didn't try the equivalent under Linux.


Philippe



Re: using std.process

2010-06-11 Thread Graham Fawcett
On Fri, 11 Jun 2010 19:09:04 +, Philippe Sigaud wrote:

 OK, this is a real newbie question:
 
 How can I use std.process?
 
 when I do:
 
 import std.process;
 
 void main() {
 execvp(mkdir, [test]);
 }
 
 nothing happens.
 
 What am I doing wrong?
 I'm on Vista there, didn't try the equivalent under Linux.

Try giving an absolute path to 'mkdir'. I'm fairly sure that the exec* 
family does not use the PATH environment.

Best,
Graham


Re: Arithmetic conversions and a surprise with 'max'

2010-06-11 Thread div0

On 11/06/2010 20:00, Ali Çehreli wrote:

The following program demonstrates a problem that I just hit. It is a
known gotcha of arithmetic conversion rules.

The program is trying to center some text around an index of a char
array. To avoid negative index values, it calls 'max' to limit the
starting offset at 0.

import std.algorithm;

void main()
{
// An empty line
char[10] line = ' ';

// We want to center some text around the first quarter mark
int center_of_text = line.length / 4;
string text = 01234567;

// To be safe, we want to limit the starting index at 0.
// (No negative index please!)
int start = max(0, center_of_text - text.length / 2);

assert(start = 0); // FAILS!
}

The problem is due to converting the second argument of max to unsigned.

Ali


It's not a 'gotcha of arithmetic conversion rules'.

Write your own max function:

import std.algorithm;
import std.stdio;

/*
T max(T)(T a, T b)
{
if(a  b)
return a;
return b;
}*/

void main(){

// An empty line
char[10] line = ' ';

// We want to center some text around the first quarter mark
int center_of_text = line.length / 4;
string text = 01234567;

// To be safe, we want to limit the starting index at 0.
// (No negative index please!)
autov0 = 0;
autov1 = center_of_text - text.length / 2;
int start = max(v0, v1);

writeln(typeid(typeof(v0)).toString);
writeln(typeid(typeof(v1)).toString);
writefln(v1: %d, v1);
writefln(start: %d, start);
assert(start = 0); // FAILS!
}

If you uncomment the local max you get:

test.d(25): Error: template test.max(T) does not match any function 
template declaration
test.d(25): Error: template test.max(T) cannot deduce template function 
from argument types !()(int,uint)


There is special magic in std.algorithm.max to make it work.
Arguably the implementation in std.algorithm.max is wrong.

Allowing mixing of unsigned and signed seems like a bad idea to me.

--
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk


Re: Arithmetic conversions and a surprise with 'max'

2010-06-11 Thread div0

On 11/06/2010 21:02, div0 wrote:

On 11/06/2010 20:00, Ali Çehreli wrote:

The following program demonstrates a problem that I just hit. It is a
known gotcha of arithmetic conversion rules.

The program is trying to center some text around an index of a char
array. To avoid negative index values, it calls 'max' to limit the
starting offset at 0.

import std.algorithm;

void main()
{
// An empty line
char[10] line = ' ';

// We want to center some text around the first quarter mark
int center_of_text = line.length / 4;
string text = 01234567;

// To be safe, we want to limit the starting index at 0.
// (No negative index please!)
int start = max(0, center_of_text - text.length / 2);

assert(start = 0); // FAILS!
}

The problem is due to converting the second argument of max to unsigned.

Ali


It's not a 'gotcha of arithmetic conversion rules'.


It is an all. (though max is wrong as well)

uinta;
int b;
b = a;

Compiles cleanly. I'm going nuts, I thought that had been disallowed.



Write your own max function:

import std.algorithm;
import std.stdio;

/*
T max(T)(T a, T b)
{
if(a  b)
return a;
return b;
}*/

void main(){

// An empty line
char[10] line = ' ';

// We want to center some text around the first quarter mark
int center_of_text = line.length / 4;
string text = 01234567;

// To be safe, we want to limit the starting index at 0.
// (No negative index please!)
auto v0 = 0;
auto v1 = center_of_text - text.length / 2;
int start = max(v0, v1);

writeln(typeid(typeof(v0)).toString);
writeln(typeid(typeof(v1)).toString);
writefln(v1: %d, v1);
writefln(start: %d, start);
assert(start = 0); // FAILS!
}

If you uncomment the local max you get:

test.d(25): Error: template test.max(T) does not match any function
template declaration
test.d(25): Error: template test.max(T) cannot deduce template function
from argument types !()(int,uint)

There is special magic in std.algorithm.max to make it work.
Arguably the implementation in std.algorithm.max is wrong.

Allowing mixing of unsigned and signed seems like a bad idea to me.


--
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk


Synchronized const methods

2010-06-11 Thread Tomek Sowiński
If a synchronized method call sees that someone's already partying with  
its object, it waits. So far so good. Now here's something I don't quite  
get: if a *const* synchronized method call sees that another const  
synchronized method call is holding the lock, then why it needs to be held  
up? D features deep const; that's a promise strong enough to drop the  
sync'ing, no?


Example:

import std.stdio;
import core.thread;

shared class K {
string value;

this (string value) {
this.value = value;
}

synchronized void metoda() const {
foreach (i; 0..3) {
writeln(Thread.getThis().name,  is calling metoda. My value  
is , value);

Thread.sleep(1_000_000);
}
}
}

class My_Thread : Thread {
K k;

this(K k, string name) {
super(run);
this.k = k;
this.name = name;
}

void run() {
k.metoda();
}
}

void main() {
K k = new K(some value);
(new My_Thread(k, Thread 1)).start();
(new My_Thread(k, Thread 2)).start();
}


Now, sync'ing would be necessary if one of the threads could mutate K, but  
here?


The output is:

Thread 1 is calling metoda. My value is some value
Thread 1 is calling metoda. My value is some value
Thread 1 is calling metoda. My value is some value
Thread 2 is calling metoda. My value is some value
Thread 2 is calling metoda. My value is some value
Thread 2 is calling metoda. My value is some value


Tomek


Re: using std.process

2010-06-11 Thread BCS

Hello Graham,


On Fri, 11 Jun 2010 19:09:04 +, Philippe Sigaud wrote:


OK, this is a real newbie question:

How can I use std.process?

when I do:

import std.process;

void main() {
execvp(mkdir, [test]);
}
nothing happens.

What am I doing wrong?
I'm on Vista there, didn't try the equivalent under Linux.

Try giving an absolute path to 'mkdir'. I'm fairly sure that the exec*
family does not use the PATH environment.


That's what the 'p' in 'vp' stand for: path.

OTOH I don't remember if the exec's tack on the exe name to the args so you 
might need to do:


execvp(mkdir,[mkdir,test]);


--
... IXOYE





Re: Synchronized const methods

2010-06-11 Thread Daniel Keep

immutable means no one can modify this.

const means someone might be able to modify this, but not you can't.