This is the mail archive of the libffi-discuss@sourceware.org mailing list for the libffi project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Caching the ffi_cif structure, thread-safety vs. stack for the ffi_type** array


On Tue, 2008-11-04 at 12:38 +0100, Philip Van Hoof wrote:
> If you want to cache the ffi_cif that ffi_prep_cif prepares for a

> function that will be called more than once and by different threads,
                                              ^^^^^^^^^^^^^^^^^^^^^^^^

> Is this the right way to do it then?

Of course if it has to be thread-safe then I would on top of this have
to add some locks like this:

> void 
> call_my_function (int a, int b) {
> 
>   SomeCachedInfo *info = get_my_function_info (...);
>   void *values[2];

    recursive_mutex_lock

>   if (!info->cif_cached) {
>     ffi_type **atypes = malloc (sizeof (ffi_type*) * 2); 
>     ffi_type  *rtype  = &ffi_type_something;
> 
>     /* prepare atypes*/
>   
>     ffi_prep_cif (&info->cif, FFI_DEFAULT_ABI, 
>                   2, rtype, atypes);
> 
>     info->cif_cached = 1;

	recursive_mutex_unlock
>   } else 
      recursive_mutex_unlock



>   /* prepare values with a and b */

recursive_mutex_lock
>   ffi_call (&info->cif, info->func, rtype, valies);
recursive_mutex_unlock

> }

Sure ok ..


> void 
> some_cached_info_free (SomeCachedInfo *info) {


But this is what the question is really about:


> 	if (info->cif_cached)
> 		free (info->cif.arg_types);

Is it guaranteed that ffi_prep_cif will have copied the ptr atypes to
cif.arg_types?


> 	free (info);
> }


-- 
Philip Van Hoof, freelance software developer
home: me at pvanhoof dot be 
gnome: pvanhoof at gnome dot org 
http://pvanhoof.be/blog
http://codeminded.be


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]