Re: Re: GCIC Fullscreen applications

Ivan Warren <[email protected]>
Newsgroups gmane.comp.emulators.hercules390.vm
Message-ID <[email protected]>
mfnoel wrote:
> I rely mostly on testing, but could you elaborate a bit on how reentrancy is a problem when recursion is OK? Want to know what to stay away from...
> 

Well..

I think because a function or module can be recursive without being 
reentrant.

***************
char foo[10];

int bar(char x)
{
	static char	z[2];
	if(x!=0)
	{
		foo[0]=0;
		z[0]=x;
		z[1]=0;
	}
	if(strlen(foo)==(sizeof(foo)-1)) return 0;
	strcat(foo,z);
	return bar(0);
}
***************

That's recursive, but not reentrant. If the task becomes interrupted and 
another task with another context invokes bar(), then unexpected results 
will occur - because both the external symbol "foo" and the internal "z" 
are shared between multiple contexts.

Task 1 invokes bar("A").. Expects foo to be a bunch of 'A's..
Task 1 is interrupt midflight
Task 2 invokes bar("B") and the function completes
Task 1 resumes and bar() completes. foo contains a bunch of 'B's

Of course, the example is totally trivial and useless - it's just an 
example.

So, to stay away from the "problem" :

- Keep information in task owned contexts (a stack or a chained list of 
buffers and registers)
- Serialize access to shared resources (lock/unlock mechanism for example).

--Ivan
smime.p7s (application/x-pkcs7-signature, 4 KB) - not displayed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.