wrapping pthread

302 views
Skip to first unread message

houbaastef .

unread,
Aug 20, 2015, 5:57:01 PM8/20/15
to cython...@googlegroups.com
Hello folks,

i need to call some functions from the pthread library.

eg : int pthread_mutex_init (pthread_mutex_t *__mutex, const pthread_mutexattr_t *__mutexattr)

so i have to declare the opaque pthread_mutex_t and pthread_mutexattr_t

by reading some headers, pthread_mutex_t looks implemented as a struct on OSX, as a union on Linux, and maybe as pointers on some BSDs.

Is there a way to properly declare that in cython, without explicite and painful conditional compilation for each system ?

Thank you,
Stephane

houbaastef .

unread,
Aug 20, 2015, 9:15:51 PM8/20/15
to cython...@googlegroups.com
After digging a bit, it seems that when in cython we declare something like :

cdef extern from "pthread.h" nogil:
ctypedef struct pthread_t:
pass

In fact the struct nature of pthread_t does not really appear in the generated C code.

So i guess the answer for my previous question is: just pretend pthread_mutex_t is an opaque struct in cython, and don't care if it's in fact an union. Is that right ?

houbaastef .

unread,
Aug 21, 2015, 9:08:41 PM8/21/15
to cython...@googlegroups.com
Anyone ? Please don't let me alone in the dark (tm) with picky C pointers...

Stefan Behnel

unread,
Aug 23, 2015, 7:41:21 AM8/23/15
to cython...@googlegroups.com
houbaastef . schrieb am 21.08.2015 um 03:15:
> 2015-08-20 23:56 GMT+02:00 houbaastef:
>> i need to call some functions from the pthread library.
>>
>> eg : int pthread_mutex_init (pthread_mutex_t *__mutex, const pthread_mutexattr_t
>> *__mutexattr)
>>
>> so i have to declare the opaque pthread_mutex_t and pthread_mutexattr_t
>>
>> by reading some headers, pthread_mutex_t looks implemented as a struct on
>> OSX, as a union on Linux, and maybe as pointers on some BSDs.
>>
>> Is there a way to properly declare that in cython, without explicite and
>> painful conditional compilation for each system ?
>
> After digging a bit, it seems that when in cython we declare something like
>
> cdef extern from "pthread.h" nogil:
> ctypedef struct pthread_t:
> pass
>
> In fact the struct nature of pthread_t does not really appear in the
> generated C code.
>
> So i guess the answer for my previous question is: just pretend
> pthread_mutex_t is an opaque struct in cython, and don't care if it's in
> fact an union. Is that right ?

Right. In fact, you can even remove the "pass" and shorten it to

ctypedef struct pthread_t

to make it clearer that there really is nothing interesting in it.

BTW, we don't currently ship declarations for the pthreads library. If
someone could come up with a .pxd file for it, I'd appreciate a pull
request for the Cython/Includes/ directory.

Stefan

houbaastef .

unread,
Sep 19, 2015, 12:53:11 PM9/19/15
to cython...@googlegroups.com
> Right. In fact, you can even remove the "pass" and shorten it to
>
> ctypedef struct pthread_t
>
> to make it clearer that there really is nothing interesting in it.

when i use pthread_t in the pyx, after declaring it this way in the
pxd, it gives error: Variable type 'pthread_t' is incomplete

regards,
stephane

Robert Bradshaw

unread,
Sep 20, 2015, 2:45:06 AM9/20/15
to cython...@googlegroups.com
Yep, you need the pass in there, otherwise it's treated like a C
forward declaration.

- Robert
Reply all
Reply to author
Forward
0 new messages