public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
* [ECOS] the pthread_once() implementation (bug?)
@ 2001-07-23 14:11 I-Jui Sung
  2001-07-26  0:36 ` Jonathan Larmour
  0 siblings, 1 reply; 2+ messages in thread
From: I-Jui Sung @ 2001-07-23 14:11 UTC (permalink / raw)
  To: eCos mailing list

[-- Attachment #1: Type: text/plain, Size: 1237 bytes --]

Hi, folks:
The pthread_once() implementation seems have different semantics with the
one in POSIX standard pthread.
(The reference is in page 118 in O'Reilly's book"Pthreads programming" 1st
ed. I assume that the book is correct on this.)

This is the text describing pthread_once() semantics on the book: "No caller
will exit from pthread_once mechanism until the routine's first caller has
returned."

In order to verify the pthread_once() semantics of eCos pthread
implementation, I wrote a small program (see attachment) to do it.
Basically it creates 2 threads, and of course both of them invoke
pthread_once(). The initialization routine simply sleeps for 10 ticks.

However the result was not the same as I think, because the 2nd caller
immediately returns while the 1st caller still blocks in
pthread_once!
(Here I assume the second caller of pthread_once() will also block in
pthread_once() until the 1st caller returns from it, which is correct (I
think) semantics of the pthread_once().)

In a word, I think there should be a pair of mutex lock/unlocks around the
invocation of the function pointer passed by user in pthread_once() in
compat/posix/current/src/pthread.cxx to guarantee the correct semantics.

Best,
I-Jui Sung

[-- Attachment #2: pthread_test.c --]
[-- Type: text/x-c, Size: 537 bytes --]

#include <pthread.h>
#include <cyg/kernel/kapi.h>
#include <stdlib.h>

static pthread_once_t oncelock=PTHREAD_ONCE_INIT;
void myinitializer(void)
{
	printf("In pthread_once..\n");
	cyg_thread_delay(10);
}


void * thread(void * arg)
{
	printf("(%s)Enter pthread_once\n",(char *)arg);
	pthread_once(&oncelock, myinitializer);
	printf("(%s)Leave pthread_once\n",(char *)arg);
	return NULL;
}

int main(void)
{
	pthread_t handle1,handle2;
	pthread_create(&handle1,NULL, thread,"1");
	pthread_create(&handle2,NULL, thread,"2");
	return 0;
}

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [ECOS] the pthread_once() implementation (bug?)
  2001-07-23 14:11 [ECOS] the pthread_once() implementation (bug?) I-Jui Sung
@ 2001-07-26  0:36 ` Jonathan Larmour
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Larmour @ 2001-07-26  0:36 UTC (permalink / raw)
  To: I-Jui Sung; +Cc: eCos mailing list

I-Jui Sung wrote:
> 
> Hi, folks:
> The pthread_once() implementation seems have different semantics with the
> one in POSIX standard pthread.
> (The reference is in page 118 in O'Reilly's book"Pthreads programming" 1st
> ed. I assume that the book is correct on this.)
> 
> This is the text describing pthread_once() semantics on the book: "No caller
> will exit from pthread_once mechanism until the routine's first caller has
> returned."

You are right.
 
> In a word, I think there should be a pair of mutex lock/unlocks around the
> invocation of the function pointer passed by user in pthread_once() in
> compat/posix/current/src/pthread.cxx to guarantee the correct semantics.

That wouldn't be ideal as _all_ pthread_once's would be serialized. This
would be fatal if the called routine itself called pthread_once. Also we
have to worry about the thread being cancelled in the called routine.
Instead, at a guess, the thing to do is to use the pthread_once_t to record
whether the routine is currently still being processed, and then broadcast
a condition variable to cause it to be checked by any waiting routines that
have called it.

It's a bit much for me to deal with "for free", so I'm just going to file
it as a bug and leave it for some other time; unless you want to have a go?

Jifl
-- 
Red Hat, Rustat House, Clifton Road, Cambridge, UK. Tel: +44 (1223) 271062
Maybe this world is another planet's Hell -Aldous Huxley || Opinions==mine
Come to the Red Hat TechWorld open source conference in Brussels!
Keynotes, techie talks and exhibitions    http://www.redhat-techworld.com/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2001-07-26  0:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-23 14:11 [ECOS] the pthread_once() implementation (bug?) I-Jui Sung
2001-07-26  0:36 ` Jonathan Larmour

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).