Sunday, April 08, 2007

of children and parents..

More of the old Python-side API getting moved over to cdefs, today is the "Children" type 'list' becoming a C array w/ mutex locking and position shifting. I decided to do something a bit more by making this a cdef class:

cdef public class Children [object soyChildren, type soyChildrenType]:
'''PySoy Children

This is a thread-safe C array of Python objects for "Parent" classes.
It's append, index, and remove methods are identical to Python lists.
The up, down, top, bottom, and move methods move an object in the array.
The lock and unlock methods control the mutex.
cdef int size
cdef int current
cdef void **list
cdef PyThread_type_lock lock

def void __new__(self)
cdef void lock(self)
cdef void unlock(self)
cdef void append(self, void *child)
cdef int index(self, void *child)
cdef void swap(self, int first, int second)
cdef void up(self, void *child)
cdef void down(self, void *child)
cdef void top(self, void *child)
cdef void bottom(self, void *child)
cdef void remove(self, void *child)
def __dealloc__(self)

Instances of this class must be locked/unlocked externally. When the coreloop traverses the various trees it'll lock/unlock thus blocking Python's execution only if Python is trying to modify the children while it's being processed. Much better than using the GIL.

This is in a soy._imports module to keep it out of the way and attached as a cdef attribute to any "parent" class. A similar Child class will reference these methods and can be inherited by the base types which need to attach to a parent.

No comments: