MSL_C_Carbon.Lib re-entrancy?

MSL_C_Carbon.Lib re-entrancy?

Post by timote » Mon, 01 Mar 2004 09:42:51


Does anyone know whether va_start() and vsprintf() in MSL_C_Carbon.Lib
(CW8.3)are re-entrant? I recall I used to get crashes if I called
them from interrupt level on MacOS. Perhaps they were allocating
memory, or using static internal storage. That was a long time ago.
Unfortunately, this is not an easy one to test.
 
 
 

MSL_C_Carbon.Lib re-entrancy?

Post by froeth » Mon, 01 Mar 2004 20:33:28


The default libraries are not. However, the libraries do have an
option to be compiled as thread-safe. You need to recompile them with

#define _MSL_THREADSAFE 1

put in the prefix files. Note that you will also need to recompile the
runtime library and the C++ library (if you use it), in addition to
the C library!

Thorsten

 
 
 

MSL_C_Carbon.Lib re-entrancy?

Post by timote » Wed, 03 Mar 2004 13:42:45


Okay, thanks! Now, I wonder, does this apply to a Carbon app running
at interrupt level (Deferred Task) on OS9? Would it be safe to call
malloc() from interrupt on OS9, or does it ultimately call NewPtr()?
I guess, Carbon or not, I need to be running on OSX in order to
allocate memory in a pre-emptive context?
 
 
 

MSL_C_Carbon.Lib re-entrancy?

Post by Frederick » Wed, 03 Mar 2004 18:46:42


The flag makes malloc call through to MPAllocateAligned.
You'd have to check whether that is safe at the deferred task, but i seem
to remember that it is. OTAllocMem is safe from deferred tasks though.

Fred
 
 
 

MSL_C_Carbon.Lib re-entrancy?

Post by froeth » Wed, 03 Mar 2004 20:52:59


The define will enable use of the Multiprocessing Services API for
memory allocation and everything else that needs to be thread-safe.
The documentation of MP Services (version 2.0 and newer) details what
the API is useful for and what not. The API is fully supported in Mac
OS 9 without Carbon, but I do not recall if it can be used in a
deferred task or not.


Multiprocessing Services provides preemptive threading on Mac OS 9 and
earlier. You do not need CarbonLib to use it. However, you will need
to pay close attention to which other APIs you may use in a preemptive
thread. See the MP Services documentation for all the details - the
documentation used to be included in the MP Services SDK on the Apple
developer website as well as older ADC SDK CDs.

Thorsten
 
 
 

MSL_C_Carbon.Lib re-entrancy?

Post by timote » Thu, 04 Mar 2004 10:40:47

Thanks again for helpful info. Research indicates that it is not safe
to call MP services at interrupt level, which I assume would include
DeferredTask on OS9. OTAllocMem is safe at interrupt, by drawing from
special pools that are replenished by ordinary allocations at safe
times. Probably better to just do that myself, rather than learning
to use OT memory properly. Or, re-write my DeferredTask as an MP
Task.... not sure if there are any other advantages there.

Also, on my original concern regarding va_start() and vsprintf() at
interrupt time, I did find Apple sample code, debugf.c, for
interrupt-safe formatted printing. It uses va_start, but
re-implements vsprintf. So that must be where the interrupt problems
I ran into before came from.
 
 
 

MSL_C_Carbon.Lib re-entrancy?

Post by froeth » Thu, 04 Mar 2004 18:54:21


If Mac OS 8.x compatibility is not a major concern (also it is
available for those, fewer system functions can be called then), to
switch to MP Services is a good idea. Not to mention that they are
available under Carbon on Mac OS 9 and Mac OS X.

Plus, under Carbon many more (however, this is/was not documented
except for the Gestalt.h flag gestaltMPTrapCalls) functions can be
called from inside MP Services tasks as the Carbon library provides
the necessary logic to run them single-threaded. Even without, having
file-io in MP tasks (even without CarbonLib) is already good to handle
a lot of complex operations.

Thorsten