[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference clt::cma

Title:DECthreads Conference
Moderator:PTHRED::MARYSTEON
Created:Mon May 14 1990
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:1553
Total number of notes:9541

1522.0. "Which DECthreads objects can be copied?" by SPECXN::DERAMO (Dan D'Eramo) Thu Apr 10 1997 18:18

        Which DECthreads objects can be copied?
        
        For example, I may have
        
        	struct XXX {
        	    cma_t_mutex mutex;
        	    cma_t_cond cond;
        	    /* ... other fields ... */
        	};
        
        	struct XXX a;
        	struct XXX b;
        
        Now after
        
        	cma_mutex_create(&a.mutex);
        	cma_cond_create(&a.cond);
        	b = a;
        
        does b.mutex refer to the same mutex as a.mutex?  If one
        thread locks a.mutex will a second thread block if it tries to
        lock b.mutex?
        
        I know that the identity of address exceptions is tied to
        their address in memory, so address exceptions cannot be
        copied like this.  But what about other objects like mutexes,
        condition variables, atomic queues, attributes, library
        attributes, thread handles, etc.?  What about in the
        1003.1c-1995 interface?
        
        Thanks,
        Dan
T.RTitleUserPersonal
Name
DateLines
1522.1DECthreads "handles" (references) vs "objects"DCETHD::BUTENHOFDave Butenhof, DECthreadsFri Apr 11 1997 10:1834
In CMA, all of the "object types" are handles, and can be copied freely.
(That doesn't necessarily mean it's a good idea, but you're allowed to, and
it'll always work.) So, yes, you can copy a cma_t_mutex, have one thread lock
the first and another lock the second, and they are guaranteed to be using
the same mutex. Similarly for condition variables, atomic queues, attributes,
etc.

Digital's implementation of POSIX 1003.4a/D4 (DCE threads) was designed on
top of the CMA "engine", and the DCE thread types (pthread_t,
pthread_mutex_t, etc.) were actually CMA handles. Thus, you could get away
with the same sort of behavior. HOWEVER, the POSIX draft standard did not
guarantee this, and vendors shipping DCE implementations were not required to
use our "reference code", and thus any such assumptions were nonportable.
Because of the way the draft standard was specified, it was safe to copy
thread identifiers (pthread_t) and attributes objects (pthread_*attr_t),
because the interface passed them by value already (essentially a copy
operation).

POSIX 1003.1c-1995 does not allow copying of any object -- mutex, condition
variable, attributes object. However, pthread_t isn't an "object", it's a
reference to an object -- and is required to be copyable for the interface to
work (it's passed by value -- copied -- to pthread_join, etc.)

IF you were to copy a pthread_mutex_t, for example, you hypothesized that
this might act as a different mutex. It's actually worse -- there's no
guarantee that the copy will even BE a mutex. Using it might not work, or
might break code using the original mutex. In standardese, the results of
using a copy of a synchronization object are "undefined". Error, incorrect
synchronization, program hang, system crash, earthquake, solar eclipse,
supernova, galactic black hole... you just don't know.

So protect the universe -- don't do it!

	/dave
1522.2SPECXN::DERAMODan D'EramoFri Apr 11 1997 20:2725
	Thanks!
        
>IF you were to copy a pthread_mutex_t, for example, you hypothesized that
>this might act as a different mutex.
        
        I did?  Wow. :-)  That certainly made me go back and reread my
        note!
        
>        does b.mutex refer to the same mutex as a.mutex?  If one
>        thread locks a.mutex will a second thread block if it tries to
>        lock b.mutex?
        
        Aha...the implicit alternative could be that b.mutex refers to
        a different mutex.  I was thinking more along the lines of the
        alternative being to RAISE(cma_e_existence) because DECthreads
        didn't recognize &b.mutex as being the address of anything it
        knew about.
        
>supernova, galactic black hole... you just don't know.
        
        I had thought that only violations of the ANSI C standard
        could do that, but I guess other standard writers hold that
        power as well. :-)
        
        Dan
1522.3DCETHD::BUTENHOFDave Butenhof, DECthreadsMon Apr 14 1997 15:526
Yeah, all standards bodies have "ultimate cosmic power". It's in the rules.

	/dave

"Let me control a planet's oxygen supply and I don't care who makes the
rules."
1522.4Caution: paradigm shift ahead.WTFN::SCALESDespair is appropriate and inevitable.Mon Apr 14 1997 17:499
.3> "Let me control a planet's oxygen supply and I don't care who makes the
.3> rules."

Ha!  Sez you, you carbon-based life form...


			;-)

				Webb
1522.5SPECXN::DERAMODan D'EramoWed Jun 04 1997 16:1324
        A quicky follow-up ... under any of the DECthreads interfaces,
        is there ever a problem letting a local variable of a created
        object "go out of scope" without having destroyed the object?
        For example, after
        
        	{
        	    cma_t_attr attr;
        	    cma_attr_create(&attr, &cma_c_null);
        	} /* no call to cma_attr_delete */
        
        or
        
        	{
        	    pthread_attr_t attr;
        	    int status = pthread_attr_init(&attr);
        
        	    if (status != 0) { /* error handling ... */ }
        	} /* no call to pthread_attr_destroy */
        
        will later re-use of the stack location occupied by "attr"
        corrupt DECthreads, or is this "merely" an otherwise "safe"
        resource leak?
        
        Dan
1522.6It's just a leak.WTFN::SCALESDespair is appropriate and inevitable.Wed Jun 04 1997 16:3111
.5> under any of the DECthreads interfaces, is there ever a problem letting a
.5> local variable of a created object "go out of scope" without having
.5> destroyed the object?

Provided that the thread has not given the address of the stack-local
variable to any other thread, the worst that will happen is that the process
will leak memory.  (We foresaw this case, and we've tried to minimize the
impact.)


				Webb
1522.7DCETHD::BUTENHOFDave Butenhof, DECthreadsWed Jun 04 1997 16:4010
To emphasize the secondary aspect of Webb's reply, it definitely MAY leak
memory. And that can lead to other application problems.

If you create/init an object, you must destroy it, not simply allow it to go
out of scope. Failing to do so is an error. The exception is that statically
initialized mutexes and condition variables (declared with
PTHREAD_MUTEX_INITIALIZER or PTHREAD_COND_INITIALIZER) need not be destroyed,
although it's a good idea to do so if you know you won't use them anymore.

	/dave