473,395 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

atomic cross-window IPC?

Short: One window writes to <input>, the other reads the same <input>.
Is it a race condition or are the reads/writes atomic?

Long: My app has an option to print results in a nicely formatted,
print-friendly way. The main window is loaded with options, textboxes,
buttons, has some graphics etc, nice as an app but not really
printable, so clicking "print" icon opens a new window with the page
with a nicely formatted, print-friendly summary, some comment entry
textareas and tools for printing (whatever unnecessary on printout,
hidden through @media print{} stylesheets.) The app window, on any
value change fills in a hidden input field. The print window
periodically (every 2s) loads the input field and introduces changes to
itself if necessary. My code sniplets below. The question is: Am I too
paranoid, or will it -just work- or it won't work despite me being
paranoid? Other comments? Any way to do it better? For now the code
works, but you never know what bugs lurk in the IPC.

--app window--
function update() // called on any change to any value in the form
when the user types.
{
....
document.f.comm.value=val1+"!"+val2+"!"+fieldx+"!" .... etc.
}

--print window--
var datastring='';
var dataarr= new Array();

function initdoc() // called from <body onload="">
{
....
setInterval(pickdata,2000);
}

function pickdata()
{
if(datastring==opener.document.f.comm.value) return; // data
unchanged, no need to update

do {

datastring=opener.document.f.comm.value;

// if changed since previous read, likely broken, reread. Paranoia?
} while(datastring==opener.document.f.comm.value);

dataarr=String(datastring).split('!');
....

}

Dec 22 '05 #1
6 1777
bw****@gmail.com said the following on 12/22/2005 5:01 AM:
Short: One window writes to <input>, the other reads the same <input>.
Is it a race condition or are the reads/writes atomic?


Its a race condition.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 22 '05 #2
> The question is: Am I too > paranoid, or will it -just work- or it won't work despite me
being paranoid? Other comments? Any way to do it better?
I think you're just beeing paranoic, but it's really possible to occur
an error
document.f.comm.value=val1+"!"+val2+"!"+fieldx+"!" .... etc.
I preffer this way:

document.f.comm.value = [val1, val2, fieldx].join("!");

Anyway, the first one is faster :)
function initdoc() // called from <body onload="">
You shouldn't use onload since it depends on images loading...

Try to add this on the end of your html, like bellow:

<script type="text/javascript">
trigger();
</script>
</body>
</html>
do datastring = opener.document.f.comm.value;
while(datastring == opener.document.f.comm.value);


This paranoia will lead you to an endless loop... If the content
doesn't get changed =/

Dispite of the things that I would change, I think you should change
your solution... You could call the popup's "pickdata" on your "update"
function...

Something like:

if(!popup || popup.closed)
popup = open("uri", "an unique name would be good here", "params");
//it's good to check if the function exists since the user can go to
another url or the page may be just loading yet...
popup.pickdata && popup.pickdata();

Well, good luck :D
--
"Invente, Tente!!! Faça um código eficiente" (eu)

Jonas Raoni Soares Silva
---------------------------
jonasraoni at gmail dot com
http://www.jsfromhell.com

Dec 22 '05 #3
> document.f.comm.value = [val1, val2, fieldx].join("!");

True, prettier. Speed is really of no concern here.
You shouldn't use onload since it depends on images loading... a single small gif with printer icon... may it fail somehow? (user
turns images off or something).
while(datastring == opener.document.f.comm.value);
of course I screwed up typing instead of pasting. That's != obviously.
Rereading the string so if it changed between the first and second
read, it's likely broken. But if the time slice the window gets is long
enough, the same broken string will be read twice so that would be
useless. Some kind of semaphore/"taint bit" would come in handy. The
problem is I can't block writing, because it is called onChange, so I
don't know how long till it will be called again, and the changes won't
make it to the print form until then. The corrupted readout isn't that
much of a problem, because it will fix itself in 2s anyway, but I'd
like to learn how it is done -right- and not just "satisfactory well".
popup.pickdata && popup.pickdata();


Thanks, I think I'll go this way (maybe even passing data directly,
popup.pickdata(data); - just couldn't find any cross-window scripting
example/manual that shows anything else than reading forms of the other
window, nor to figure out descendants of what object are the top-level
functions. Seems the simplest is true and all tries like
popup.document.pickdata(), popup.window.pickdata(),
popup.getElementById('readscript').pickdata() were just
over-complicating the problem.).

Nevertheless I'd still love to learn how to transfer the data
asynchronously, just for educational purpose.

Thanks for a helpful answer.

Dec 22 '05 #4
Jonas Raoni said the following on 12/22/2005 10:32 AM:

function initdoc() // called from <body onload="">

You shouldn't use onload since it depends on images loading...

Try to add this on the end of your html, like bellow:

<script type="text/javascript">
trigger();
</script>
</body>
</html>


In that scenario, the onload is preferable. The onload will ensure the
document has loaded. Adding a script call at the end of the body won't
ensure it is loaded.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 22 '05 #5
On 2005-12-22, bw****@gmail.com <bw****@gmail.com> wrote:
Short: One window writes to <input>, the other reads the same <input>.
Is it a race condition or are the reads/writes atomic?


I have been led to believe that all javascript processes are atomic.
OTOH some claim that separate documents opperate synchronously, I have never
noticed this.
--

Bye.
Jasen
Dec 23 '05 #6
Jasen Betts said the following on 12/23/2005 12:58 AM:
On 2005-12-22, bw****@gmail.com <bw****@gmail.com> wrote:
Short: One window writes to <input>, the other reads the same <input>.
Is it a race condition or are the reads/writes atomic?

I have been led to believe that all javascript processes are atomic.


You were led to believe incorrectly when it comes to two windows.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 24 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Paul Moore | last post by:
I can't find anything which spells this out in the manuals. I guess that, at some level, the answer is "a single bytecode operation", but I'm not sure that explains it for me. This thought was...
8
by: Glenn Kasten | last post by:
I am wondering which operations in Python are guaranteed to be atomic in the presence of multi-threading. In particular, are assignment and reading of a dictionary entry atomic? For example,...
42
by: Shayan | last post by:
Is there a boolean flag that can be set atomically without needing to wrap it in a mutex? This flag will be checked constantly by multiple threads so I don't really want to deal with the overhead...
5
by: Pegboy | last post by:
What does it mean to make a function atomic? Something I read on it wasn't very clear, but made me think that I needed to disable interrupts for that function. Whether that's the case or not,...
28
by: robert | last post by:
In very rare cases a program crashes (hard to reproduce) : * several threads work on an object tree with dict's etc. in it. Items are added, deleted, iteration over .keys() ... ). The threads are...
6
by: blackstreetcat | last post by:
consider this code : int i; //gobal var Thread1: i=some value; Thread2: if (i==2) dosomething(); else dosomethingelse();
11
by: japhy | last post by:
Is there a way to read a line (a series of characters ending in a newline) from a file (either by descriptor or stream) atomically, without buffering additional contents of the file?
0
by: yogeeswar | last post by:
Hi All I wrote SP with number of delete commands in an atomic block.And there is a possibility of deleting records from parent table before child table so I wrote a handler to handle the...
2
by: Freedom fighter | last post by:
Hello, Is a singleton class the same as an atomic class? I know that a singleton class can only be instantiated once, but does that concept apply to an atomic class? Thank you.
11
by: Jon Harrop | last post by:
Can read locks on a data structure be removed safely when updates are limited to replacing a reference? In other words, is setting a reference an atomic operation? I have been assuming that all...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.