the wrappedJSObject hack and thread safety

Hello ---

I'm new to looking at Zotero, and am interested in using the data API for another extension I'm writing. I greatly appreciate the well written documentation you've already done for this, but there's a fundamental concept that isn't quite clicking for me. I'm at least superficially familiar with the concepts involved (XPCOM services, SQLite thread safety, and SpiderMonkey) but I'm ignorant of many implementation details.

Here's where I'm stuck:

http://developer.mozilla.org/en/docs/Storage pretty much says that there is no safe way to access a database from multiple threads. I thought the solution would be a service running in a proxy thread that does all the access to the database, but from a superficial glance at your (clear and well-commented) code it seems like you aren't doing anything like that. In fact, giving direct access to the wrappedJSObject seems like it would make this impossible.

Is this safe because there is a proxy occurring at the mozIStorageService level that I haven't discovered yet? Or perhaps because (according to some vague memory I have) that all JavaScript runs from a single thread, is thus will be automatically thread safe? Is there maybe some other locking that happens at the Zotero level that I didn't notice?

Thanks for any information!
  • dstillman Zotero Team
    I've had similar concerns, but as far as I know, all JavaScript in Firefox runs in a single thread (the main UI thread), so this isn't an issue (and UI blocking is). Even events on timers are, I believe, run from the same thread, and the timer is simply managed by an event scheduler.

    A lot may be changing with respect to threads in Firefox 3, so it's possible there will be additional issues in the future.
  • Thanks for the quick response, Dan. After doing more research, I'm convinced you are right. Withing Firefox (and IE), all Javascript is executed from a single thread, so the warnings on the http://developer.mozilla.org/en/docs/Storage page don't really apply. I've added a clarification to that page under the 'Thread Safety' section. Feel free to improve it if I've said anything inaccurate.

    Some useful information (and links to more such information) is here:
    http://www.neilmix.com/2007/02/14/what-mean-you-threads/
    The impression I got is that FF3 will almost definitely remain the same.

This is an old discussion that has not been active in a long time. Instead of commenting here, you should start a new discussion. If you think the content of this discussion is still relevant, you can link to it from your new discussion.