Add a Utils.lock as a sync. version of Wrap.localLock.
authorEdward Lee <edilee@mozilla.com>
Sat, 06 Jun 2009 11:52:49 -0700
changeset 45537 f4293ee8da832e80a29aa67fe8d485db8d4f951b
parent 45536 1853c30a869cf06f7402405feca2f6815c47fe5c
child 45538 dbdd990accd1cef5e7e29ee2711856340d7291a5
push id14033
push useredward.lee@engineering.uiuc.edu
push dateWed, 23 Jun 2010 22:21:35 +0000
treeherdermozilla-central@227db4ad8cdf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
Add a Utils.lock as a sync. version of Wrap.localLock.
services/sync/modules/service.js
services/sync/modules/util.js
--- a/services/sync/modules/service.js
+++ b/services/sync/modules/service.js
@@ -147,16 +147,17 @@ StatusRecord.prototype = {
  */
 
 function WeaveSvc() {
   this._notify = Utils.notify("weave:service:");
   this._notifyAsync = Wrap.notify("weave:service:");
 }
 WeaveSvc.prototype = {
 
+  _lock: Utils.lock,
   _localLock: Wrap.localLock,
   _catch: Utils.catch,
   _catchAll: Wrap.catchAll,
   _isQuitting: false,
   _loggedIn: false,
   _syncInProgress: false,
   _keyGenEnabled: true,
 
--- a/services/sync/modules/util.js
+++ b/services/sync/modules/util.js
@@ -67,16 +67,37 @@ let Utils = {
       catch(ex) {
         thisArg._log.debug(["Caught exception:", Utils.exceptionStr(ex),
           Utils.stackTrace(ex)].join(" ").replace(/\n/g, " "));
       }
     };
   },
 
   /**
+   * Wrap a function to call lock before calling the function then unlock.
+   *
+   * @usage MyObj._lock = Utils.lock;
+   *        MyObj.foo = function() { this._lock(func)(); }
+   */
+  lock: function Utils_lock(func) {
+    let thisArg = this;
+    return function WrappedLock() {
+      if (!thisArg.lock())
+        throw "Could not acquire lock";
+
+      try {
+        return func.call(thisArg);
+      }
+      finally {
+        thisArg.unlock();
+      }
+    };
+  },
+
+  /**
    * Wrap functions to notify when it starts and finishes executing or if it got
    * an error. The message is a combination of a provided prefix and local name
    * with the current state and the subject is the provided subject.
    *
    * @usage function MyObj() { this._notify = Utils.notify("prefix:"); }
    *        MyObj.foo = function() { this._notify(name, subject, func)(); }
    */
   notify: function Utils_notify(prefix) {