Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running mutiple COScript and using background threads, anyway serialise them #59

Open
UXDart opened this issue May 11, 2018 · 6 comments

Comments

@UXDart
Copy link

UXDart commented May 11, 2018

Hello! I'm writing a plugin for Sketch, and inside the cocoa script I use performSelectorInBackground_withObject to start a new thread and don't block the execution of a log running process. that works perfect!
I call coscript.setShouldKeepAround(true); and coscript.setShouldKeepAround(false); when the thread ends. that works ok.

the problem is that, if I start (lets say) 3 scripts, different names, different COScript instances, this happens:

  • thread A works
  • when thread B starts, 'B' takes all work and A stops/waits
  • when C starts and B waits and C starts and finish
  • then B continue and finish
  • then A finish

so for some reason it seems something is making them stop/wait when other thread is working.
I know COScript does not really support threads, but they work... in some way... the question is, why this could be happening? I tried many things, but in all cases this happens. if someone has any tip I can work over that, but I just don't know what else to try... thx!

TIA!

@UXDart
Copy link
Author

UXDart commented May 11, 2018

hmm.. they are sharing the same JSVirtualMachine... so that makes sense then... that must be the reason right? where is COScript creating the JSVirtualMachine? or how can I create a new one for each COScript instance?

Thanks

@UXDart
Copy link
Author

UXDart commented May 11, 2018

found a way to create them with different JSVirtualMachine, so each one has its own Mocha instance, and it happens anyway, threads gets serialised... any tip? TIA!

@ccgus
Copy link
Owner

ccgus commented May 11, 2018

Are you creating the CocoaScript instances, or is this something that Sketch is handing off to you?

@UXDart
Copy link
Author

UXDart commented May 11, 2018

I tried with Sketch ones, didn't work. so I'm creating the instances myself, so I can do whatever is needed to make it work. atm I find a way to create each instance of Mocha inside COScript using a different JSVirtualMachine, using initWithGlobalContext method. but then nothing works, it seems it can't set a variable there for some reason. I think that is the issue, that Sketch is doing a Mocha.alloc.init so it using the same Mocha global context. it makes sense?

do you have a sample of how to use the method initWithGlobalContext? what do I need to send exactly so it has a different global context?

or what do you think may be the cause of the serialising of the threads?

Thanks!

@UXDart
Copy link
Author

UXDart commented May 11, 2018

I'm doing this atm:

JSContext *contextNew = [[JSContext alloc] initWithVirtualMachine:[[JSVirtualMachine alloc] init]];
JSGlobalContextRef globalCtx1 = contextNew.JSGlobalContextRef;
_mochaRuntime = [[Mocha alloc] initWithGlobalContext:globalCtx1 name:nil];

but that seems to work, but then the setvalue getvalue doesn't work...

@ccgus
Copy link
Owner

ccgus commented May 11, 2018

I'm not sure what would be causing it off the top of my head. You might ping the sketch folks.

afedor pushed a commit to afedor/CocoaScript that referenced this issue Dec 20, 2020
…mod-heads-in-release

Code Review: Fix submod heads in release/55 (24371)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants