• Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r4wp

[#Red] Red language group

Kaj
23-Dec-2012
[4799x2]
Use :cpu-count :len
What does len specify?
Jerry
23-Dec-2012
[4801]
len ... the length of buffer in the next parameter??? not sure.
DocKimbel
24-Dec-2012
[4802]
From Announce: Great Kaj! The optimizer should be able to do a pretty 
good job on simple cases like Fibonacci function. Still, you'll have 
the Red stack management overhead, which is currently unavoidable, 
but in the future, we might found ways to optimize it too. Actually, 
I have two options to reduce stack overhead: stack multiple openings/closing 
compression and inlining stack calls, but we'll see that in Red v2.
Jerry
25-Dec-2012
[4803]
Red/System Questions:


I am translating a piece of code from C to Red/System, I don't know 
how to translate the following C code:


success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone,  (void 
* volatile *)&_zone_); // C

my problems are: 

1. In Red/Sys, _sqliteZone_ is a struct! (which is a pointer in Red/Sys), 
so I cannot use :_zone_ to get it's address.
2. "(void * volatile *)" in C => "as byte-ptr!" in Red/Sys ??

Thanks!
DocKimbel
25-Dec-2012
[4804]
1) Is _zone_ a struct or a struct pointer? If it is a struct, then 
in Red/System, you just pass the struct! variable.

2) Correct, use "as byte-ptr!" in such case.
Jerry
25-Dec-2012
[4805]
1. _zone_ is a struct pointer. I need to pass a pointer to a struct 
pointer as the 3rd parameter, that's why I try to use get-word here.
DocKimbel
25-Dec-2012
[4806]
For 1., you can achieve it this way:

    s: declare struct! [n [integer!]]
    p: declare int-ptr!
    p/value: as-integer s

then `p` becomes a pointer on `s`.


I might extend get-word syntax to struct! and pointer! too, someone 
just needs to add a ticket to Github to remind me of that.
Jerry
25-Dec-2012
[4807]
Thanks, Doc
Kaj
25-Dec-2012
[4808x5]
I usually solve such double references by wrapping the needed pointer 
in an extra struct. See the *-reference! types here:
http://red.esperconsultancy.nl/Red-common/artifact/aad3c29d7fb6647bb1e5d8b404d05211f5ed33c1
Speaking about atomic operations, any thought to get them in Red/System? 
I could use them well
Perhaps an atomic! type for an integer with guarantees about the 
generated code?
What are the chances that current integer operations already have 
atomic behaviour?
DocKimbel
25-Dec-2012
[4813x3]
I'm not sure what you mean by "atomic operation"?
Red/System integer! operations are as atomic as the CPU permits.
Red/System maps its datatypes as closely as possible to the CPU (the 
same way C does).
Andreas
25-Dec-2012
[4816]
Re atomic operation: atomic "x: x + 1", for example.
Kaj
25-Dec-2012
[4817x3]
Atomic means safe from corruptions by other CPU cores accessing the 
same memory
http://syllable.cvs.sourceforge.net/viewvc/syllable/syllable/system/sys/include/atheos/atomic.h?view=markup
These become vital when you start doing multi-core concurrency
DocKimbel
25-Dec-2012
[4820]
Ok, I see what you mean. Like in C, you can already write "thread-safe" 
code in Red/System by using only variables on stack. If you need 
concurrent access to global variables, usually, the OS are already 
providing some API for that (locks, semaphores, mutex,...). I haven't 
chosen yet how it will be implemented at Red/System level, there 
are different options. For example, it could be handled by the language 
directly using a similar construct as in Java:

    synchronize [ x: x + 1 ]


In my early design notes, I have researched only how to handle concurrency 
at Red level, I've left the underlying Red/System part as an "implementation 
detail". I plan to start working on it after I/O will be implemented.
Kaj
25-Dec-2012
[4821x2]
I'm thinking about  the case where you want to implement operating 
system level code yourself. For example, I have been fixing many 
bugs in Syllable's PThreads implementation, but I would like to replace 
it with a Red/System implementation. PThreads adds quite a few constructs 
that Syllable's kernel primitives don't implement as such, so to 
add them you need to write extra concurrency code
For example, there are spinlock-like constructions in Syllable's 
PThreads implementation that can often prevent having to call a kernel 
mutex. This makes them much more efficient, but you need atomic operations 
on access flags and counters to implement the user-space spinlocks 
and mutex shells
DocKimbel
25-Dec-2012
[4823]
Modern CPU usually provide a way to make such atomic operations (e.g. 
CAS-type  instructions), Red/System might provide access to them 
if it is required.
Kaj
25-Dec-2012
[4824]
Something like that is what I have in mind. From what CPUs on are 
they supported?
DocKimbel
25-Dec-2012
[4825]
From a quick googling, I've found:


- x86: CMPXCHG (compare and exchange) is supported from 486. LOCK 
instruction seems to be available from PentiumPro only.


- ARM: Starting with the ARMv6 architecture ARM has the LDREX/STREX 
instructions that can be used to implement an atomic compare-exchange 
operation.
Kaj
25-Dec-2012
[4826x2]
So it would bump Red's requirements a little, but I guess it's worth 
it
http://en.wikipedia.org/wiki/Compare-and-swap
Jerry
25-Dec-2012
[4828x2]
In current Red/System, there is no break/continue keywords (for loop), 
I wish they will show up in Red/System 2.0
So how can I break/continue a loop now?
DocKimbel
25-Dec-2012
[4830x2]
BREAK will probably be implemented before 2.0 (not sure for CONTINUE). 
So, you can't break from a loop currently. 


As a workaround, you can EXIT/RETURN instead. So if you write your 
loop in a separate function, you'll get similar effect as with BREAK.
I'm a bit late for the Xmas release, sorry...still fixing bugs...
Kaj
25-Dec-2012
[4832]
I thought we already had a Xmas release :-)
DocKimbel
25-Dec-2012
[4833x3]
Actually, it is not really a new release, as I did too many changes 
and it would need a week at least to stabilize and fully complete. 
But it will be a significant revision. ;-)
revision = commit
I've stressed the current Red runtime source code a bit so it keeps 
"tilting" as I try to make it work. :-)
Jerry
25-Dec-2012
[4836]
Doc, will you spend some time making Red/Sys support loop BREAK/CONTINUE 
in the near future? or at least GOTO. Thanks. Programming without 
them is like drinking soup without a spoon ...
DocKimbel
26-Dec-2012
[4837]
BREAK is on my todo list. I might add CONTINUE too. GOTO could be 
useful for implementing fast FSM, but having some specific feature 
for FSM support would probably be a cleaner option than GOTO.


I personally very rarely need BREAK/CONTINUE, their usage depends 
on your programming style and the way you implement algorithms.
Arnold
26-Dec-2012
[4838]
There are many way in which break and continue are being used amongst 
the different programming languages, most notable in case or switch 
statements.
DocKimbel
26-Dec-2012
[4839x3]
BREAK in CASE/SWITCH statement is IMHO a very bad design choice leading 
to countless bugs. I'm glad Red and REBOL are doing it the right 
way.
Xmas feature teaser: http://t.co/jTYwNzfb
Implementing that feature revealed a few deep bugs in the current 
Red runtime code, I hope to be able to finish fixing them by tonight.
Henrik
26-Dec-2012
[4842]
great stuff :-)
Endo
26-Dec-2012
[4843]
Console support! I'm waiting for it impatiently!!

BREAK/CONTINUE: I had to use those a few times in REBOL, it would 
be nice to have CONTINUE in Red too.
NickA
26-Dec-2012
[4844]
Awesome Doc!  Didn't expect to see a console so early.
DocKimbel
26-Dec-2012
[4845x3]
It's a full REPL, so it's an interpreter + console. I was thinking 
about adding an interpreter since this summer in order to fill the 
gaps until the JIT-compiler will be there. Even when Red will gain 
JIT-compilation ability, for dynamic code with no loops, interpreting 
the code will be faster. Also, on some platforms, JIT-compilation 
is not allowed, so the interpreter will be handy to workaround those 
limitations.
Now, you have a better picture about the "full-stack" approach of 
Red language. :-)
The console is currently just a Red script, once we get devices and 
ports, it will be replaced by a proper console:// port.
NickA
26-Dec-2012
[4848]
This will make it much easier for everyone to pick it up and experiment 
quickly and easily.