Jan Jancar on Sun, 16 Apr 2017 19:12:58 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: libpari: threads and argument passing


Greetings Bill!

On 04/16/2017 03:33 PM, Bill Allombert wrote:
> On Sun, Apr 16, 2017 at 03:01:38PM +0200, Jan Jancar wrote:
>> Hello PARI developers,
>> looking at the "PARI and threads" part of the libpari
> 
> Hello Jan,
> Thanks for for reviewing the documentation!

Got to read it to use it! :)

>> However it could be quite useful to be able to pass an
>> arbitrary void* as the pthread API allows. A void* is
>> guaranteed to be able to hold a pointer to any type, 
>> so passing a GEN is still possible, while a long* 
>> (what a GEN really is) is not guaranteed to be able to
>> hold a pointer to any type. This generally makes passing
>> a pointer to a custom structure and casting it to a GEN
>> risky business on architectures where it doesn't fit
>> (are there any?).
> 
> Maybe I am wrong but as I understand it, the C standard only
> differentiate pointers to data and pointers to functions.
> So as far as I am concerned, you are welcome to cast your void*
> to a GEN and pass it to pari_thread_alloc.

I was mainly going from [1].
Looking at the 6.3.2.3 section of C99 [2] or C11 [3]:

###
1) A pointer to void may be converted to or from a pointer
to any incomplete or object type. A pointer to any incomplete
or object type may be converted to a pointer to void and back
again; the result shall compare equal to the original pointer.

7) A pointer to an object or incomplete type may be converted to a 
pointer to a different object or incomplete type. If the resulting 
pointer is not correctly aligned for the pointed-to type, the 
behavior is undefined. Otherwise, when converted back again, the
result shall compare equal to the original pointer. When a pointer 
to an object is converted to a pointer to a character type, the
result points to the lowest addressed byte of the object.
###

And then at 6.2.5:

###
28) A pointer to void shall have the same representation and
alignment requirements as a pointer to a character type. Similarly,
pointers to qualified or unqualified versions of compatible types
shall have the same representation and alignment requirements.
All pointers to structure types shall have the same representation
and alignment requirements as each other. All pointers to union
types shall have the same representation and alignment requirements
as each other. Pointers to other types need not have the same
representation or alignment requirements. 
###


I don't think it explicitly says / requires that a pointer to
any object type can by converted to a pointer to any other object
type and back and be valid. It explicitly says so only for pointer to
void.

> 
>> Is there some reason I'm not seeing as to why the
>> argument is restricted to a GEN type?
> 
> I did it that way because it saves a lot of casts in the common case when
> the argument is a GEN.
> But actually, the API does not require pthread_create() to be called with
> the output of pari_thread_alloc().
> You can create a struct:
> struct my_pari_thread
> {
>   struct pari_thread pth;
>   void * args; /* Or whatever */
> } my_pth;
> 
> pari_thread_alloc(&my_pth.pth,4000000,NULL);
> pthread_create(&th,NULL,&myfun,(void*)&my_pth);
> 
> and then do
> 
> void *
> myfun(void *arg)
> {
>   GEN F, M;
>   struct my_pari_thread *marg = (struct my_pari_thread) arg; 
>   /* Set up thread stack and get thread parameter */
>   (void) pari_thread_start(&marg.pth);
>   ... do something with marg.args ...
> }
> 
> Cheers,
> Bill.
> 

I don't know how I haven't thought of that, thanks! I only
wrapped my structure in the pari_thread struct via casting to GEN,
and this way I can wrap the pari_thread struct* in my structure.

This would be a great example for the docs, to show passing anything
to a PARI thread, I could work it into a patch if such an addition is
wanted.

[1]: https://stackoverflow.com/questions/1241205/are-all-data-pointers-the-same-size-in-one-platform-for-all-data-types
[2]: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
[3]: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

Kind regards,
-- 
Jan
______________________________________________________
   /\  # PGP: 362056ADA8F2F4E421565EF87F4A448FE68F329D
  /__\  # https://neuromancer.sk
 /\  /\  # Eastern Seaboard Phishing Authority
/__\/__\  # 

Attachment: signature.asc
Description: OpenPGP digital signature