LuaEngineImpl synchronisation / accelerometer support

Hiyah,

I began writing some accelerometer support by hijacking the existing touch/es infrastructure. My first stab (before re-factoring out the common script hook handling code to a seperate (touch-agnostic) location) can be found here : https://github.com/ciaranj/cocos2d-x/tree/add_accelerometer_bindings_to_lua

This works, ‘woot!’ … however it suggests a somewhat fundamental issue. There appears to be no locking within LuaEngineImpl :frowning: Which means that I have both:

sceneLayer.CCTouchDelegate:registerScriptTouchHandler(cocos2d.CCDIDACCELERATE, “didAccelerate”)

and

cocos2d.CCScheduler:sharedScheduler():scheduleScriptFunc(“tick”, 0.01, false)

firing, sometimes (and oddly only for android?) they (I think) occur con-currently so lua’s global state ends up in an in-consistent manner (and seg-faults)

I’m not too clear on the threading models involved, can anyone explain them? … I’m surprised that I don’t see the issue on iOS tbh, so I can only assume that the thread-locking/spawning is being dealt with outside of the code, but not for the android, I also don’t understand why this doesn’t affect the touch/es handlers ?

Ok, so after adding CCLock->lock/unlock to LuaEngineImpl, I discover:

// cocos2d-x havn’t support multi-thread yet
#undef CC_SUPPORT_MULTITHREAD
#define CC_SUPPORT_MULTITHREAD 0

So presumably, what I’m doing is wrong :slight_smile: … Any hints ??

CCLock isn’t implemented on ios. And on android port, I haven’t write a strict unit test on it.
We plan to do the multi-thread feature at first in #354, then #465.

Okay. in the meantime is there an approach you would suggest for doing synchronisation ? (I still don’t understand why the accelerometer -> lua is Ok for iOs,but not for android :frowning: !)