[M3devel] pthread_self == null?

Tony Hosking hosking at cs.purdue.edu
Mon Jul 26 21:00:29 CEST 2010


The standard doesn't even say that a pthread_t is a pointer.
It might be an int, in which case 0 might be a valid pthread_t.

On 26 Jul 2010, at 07:18, Jay K wrote:

> 
> I don't see that the standard prohibits NULL from being a valid pthread_t.
> ?
> I happened upon this only due to a problem though: OpenBSD x86 4.6 -lX11 -static gives a broken pthreads.
> 
> 
> We should add an extra boolean to this code, like, before:
> 
> 
> VAR
>   holder: pthread_t;
>   inCritical := 0;
> 
> PROCEDURE LockHeap () =
>   VAR self := pthread_self();
>   BEGIN
>     IF pthread_equal(holder, self) = 0 THEN
>       WITH r = pthread_mutex_lock(heapMu) DO <*ASSERT r=0*> END;
>       holder := self;
>     END;
>     INC(inCritical);
>   END LockHeap;
> 
> PROCEDURE UnlockHeap () =
>   BEGIN
>     <*ASSERT pthread_equal(holder, pthread_self()) # 0*>
>     DEC(inCritical);
>     IF inCritical = 0 THEN
>       holder := NIL;
>       WITH r = pthread_mutex_unlock(heapMu) DO <*ASSERT r=0*> END;
>     END;
>   END UnlockHeap;
> 
> 
> 
> after:
> 
> VAR
>   holder: pthread_t;
> +  holder_valid: BOOLEAN;
>   inCritical := 0;
> 
> PROCEDURE LockHeap () =
>   VAR self := pthread_self();
>   BEGIN
> *    IF NOT holder_valid OR pthread_equal(holder, self) = 0 THEN
>       WITH r = pthread_mutex_lock(heapMu) DO <*ASSERT r=0*> END;
>       holder := self;
> +     holder_valid := TRUE;  
>     END;
>     INC(inCritical);
>   END LockHeap;
> 
> PROCEDURE UnlockHeap () =
>   BEGIN
> *    <*ASSERT holder_valid AND pthread_equal(holder, pthread_self()) # 0*>
>     DEC(inCritical);
>     IF inCritical = 0 THEN
>       holder := NIL;
> +     holder_valid := FALSE  
> 
>       WITH r = pthread_mutex_unlock(heapMu) DO <*ASSERT r=0*> END;
>     END;
>   END UnlockHeap;
> 
> 
> I think so.
> 
> 
>  - Jay
> 
> 		 	   		  




More information about the M3devel mailing list