stateful state-threads

Cuong Nguyen <[email protected]> Mon, 24 Nov 2003 22:20:22 +1100 (EST)
Newsgroups gmane.comp.lib.state-threads.user
Message-ID <[email protected]>
I'm a little bit confused here...I thought I understood this before, but...

Can someone correct me where I'm wrong please:
- I/O operations should use one of the state thread library's methods (eg. st_read)
- On a call to a long wait read/write, the library is able to switch "context" to 
another state-thread that is ready to be processed.

Does that mean that on return from a read/write wait state, any shared variables may 
not be in the same state (pardon the pun) that it was left in before the long 
read/write operation?
Using the "server" example to illustrate (and simplifying for illustration):


/* global */ 
int r1;

static void *handle_connections(void *arg)
{
[1]  st_netfd_t cli_nfd;

[2]  while (WAIT_THREADS(i) <= max_wait_threads) {
[3]    cli_nfd = st_accept(srv_nfd, (struct sockaddr *)&from, &fromlen, -1);

[4]    st_netfd_setspecific(cli_nfd, &from.sin_addr, NULL);
[5]    st_thread_create(handle_connections, (void *)i, 0, 0);

[6]    handle_session(i, cli_nfd);

[7]    st_netfd_close(cli_nfd);
[8]  }
[9]  return NULL;
}


void handle_session(long srv_socket_index, st_netfd_t cli_nfd)
{
[10]  int r2;
[11]  while ( 1 ) {
[12]     r1 = (int)rand();
[13]     r2 = (int)rand();
[14]     st_read(cli_nfd, buf, sizeof(buf), INFINITE_WAIT);
[15]     DoSomething();
[16]  }

}



The handle_session() function has been converted to be an infinite loop as shown.
If two threads running handle_session() at line 14 had to wait on the client nfd
for a random time interval, does that mean that on return from the read, the global 
variable 'r1' and local variable 'r2' are not guaranteed to be the same as before
the call to st_read() ?
ie.
- thread1 enters handle_session and sets r1 = 111, r2 = 222, then waits via st_read
 for 10 seconds.
- thread2 enters handle_session and sets r2 = 333, r2 = 444, then waits via st_read 
for 15 seconds.
- when thread1 returns, r1 should be 333, but what is r2?...and for that matter, is 
the cli_nfd pointer
different to before thread1 went into wait state via st_read() ???

I'm a little confused because I know nothing about the impact of long_jmp and 
preservation of stack space...well, I know nothing about long_jmp period!


http://personals.yahoo.com.au - Yahoo! Personals
New people, new possibilities. FREE for a limited time.


-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?  SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/