Bug 911341 - 2/2: Seperate the parcel header handling. r=yoshi
authorVicamo Yang <vyang@mozilla.com>
Wed, 04 Sep 2013 18:58:26 +0800
changeset 158436 8f545f56d6918f237308722b6cd25c7852e01054
parent 158435 862a062456b0200a0a9f3206048e4977685b655c
child 158437 6c6bf65089b6d2a1bc6148c97ad6e1afe5e87658
push id2961
push userlsblakk@mozilla.com
push dateMon, 28 Oct 2013 21:59:28 +0000
treeherdermozilla-beta@73ef4f13486f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersyoshi
bugs911341
milestone26.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 911341 - 2/2: Seperate the parcel header handling. r=yoshi
dom/system/gonk/ril_worker.js
dom/system/gonk/worker_buf.js
--- a/dom/system/gonk/ril_worker.js
+++ b/dom/system/gonk/ril_worker.js
@@ -96,16 +96,100 @@ let RILQUIRKS_HAVE_QUERY_ICC_LOCK_RETRY_
 // Marker object.
 let PENDING_NETWORK_TYPE = {};
 
 let Buf = {
   __proto__: (function(){
     return require("resource://gre/modules/workers/worker_buf.js").Buf;
   })(),
 
+  mToken: 0,
+  mTokenRequestMap: null,
+
+  init: function init() {
+    this._init();
+
+    // This gets incremented each time we send out a parcel.
+    this.mToken = 1;
+
+    // Maps tokens we send out with requests to the request type, so that
+    // when we get a response parcel back, we know what request it was for.
+    this.mTokenRequestMap = {};
+  },
+
+  /**
+   * Process one parcel.
+   */
+  processParcel: function processParcel() {
+    let response_type = this.readUint32();
+
+    let request_type, options;
+    if (response_type == RESPONSE_TYPE_SOLICITED) {
+      let token = this.readUint32();
+      let error = this.readUint32();
+
+      options = this.mTokenRequestMap[token];
+      if (!options) {
+        if (DEBUG) {
+          debug("Suspicious uninvited request found: " + token + ". Ignored!");
+        }
+        return;
+      }
+
+      delete this.mTokenRequestMap[token];
+      request_type = options.rilRequestType;
+
+      options.rilRequestError = error;
+      if (DEBUG) {
+        debug("Solicited response for request type " + request_type +
+              ", token " + token + ", error " + error);
+      }
+    } else if (response_type == RESPONSE_TYPE_UNSOLICITED) {
+      request_type = this.readUint32();
+      if (DEBUG) debug("Unsolicited response for request type " + request_type);
+    } else {
+      if (DEBUG) debug("Unknown response type: " + response_type);
+      return;
+    }
+
+    RIL.handleParcel(request_type, this.mReadAvailable, options);
+  },
+
+  /**
+   * Start a new outgoing parcel.
+   *
+   * @param type
+   *        Integer specifying the request type.
+   * @param options [optional]
+   *        Object containing information about the request, e.g. the
+   *        original main thread message object that led to the RIL request.
+   */
+  newParcel: function newParcel(type, options) {
+    if (DEBUG) debug("New outgoing parcel of type " + type);
+
+    // We're going to leave room for the parcel size at the beginning.
+    this.mOutgoingIndex = this.PARCEL_SIZE_SIZE;
+    this.writeUint32(type);
+    this.writeUint32(this.mToken);
+
+    if (!options) {
+      options = {};
+    }
+    options.rilRequestType = type;
+    options.rilRequestError = null;
+    this.mTokenRequestMap[this.mToken] = options;
+    this.mToken++;
+    return this.mToken;
+  },
+
+  simpleRequest: function simpleRequest(type, options) {
+    this.newParcel(type, options);
+    this.sendParcel();
+  },
+
   onSendParcel: function onSendParcel(parcel) {
     postRILMessage(CLIENT_ID, parcel);
   }
 };
 
 /**
  * The RIL state machine.
  *
--- a/dom/system/gonk/worker_buf.js
+++ b/dom/system/gonk/worker_buf.js
@@ -1,81 +1,72 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 const INT32_MAX   = 2147483647;
 const UINT8_SIZE  = 1;
 const UINT16_SIZE = 2;
 const UINT32_SIZE = 4;
-const PARCEL_SIZE_SIZE = UINT32_SIZE;
 
 /**
  * This object contains helpers buffering incoming data & deconstructing it
  * into parcels as well as buffering outgoing data & constructing parcels.
  * For that it maintains two buffers and corresponding uint8 views, indexes.
  *
  * The incoming buffer is a circular buffer where we store incoming data.
  * As soon as a complete parcel is received, it is processed right away, so
  * the buffer only needs to be large enough to hold one parcel.
  *
  * The outgoing buffer is to prepare outgoing parcels. The index is reset
  * every time a parcel is sent.
  */
 
 let Buf = {
+  PARCEL_SIZE_SIZE: UINT32_SIZE,
+
   mIncomingBufferLength: 1024,
   mIncomingBuffer: null,
   mIncomingBytes: null,
   mIncomingWriteIndex: 0,
   mIncomingReadIndex: 0,
   mReadIncoming: 0,
   mReadAvailable: 0,
   mCurrentParcelSize: 0,
 
   mOutgoingBufferLength: 1024,
   mOutgoingBuffer: null,
   mOutgoingBytes: null,
   mOutgoingIndex: 0,
   mOutgoingBufferCalSizeQueue: null,
 
-  mToken: 0,
-  mTokenRequestMap: null,
-
-  init: function init() {
+  _init: function _init() {
     this.mIncomingBuffer = new ArrayBuffer(this.mIncomingBufferLength);
     this.mOutgoingBuffer = new ArrayBuffer(this.mOutgoingBufferLength);
 
     this.mIncomingBytes = new Uint8Array(this.mIncomingBuffer);
     this.mOutgoingBytes = new Uint8Array(this.mOutgoingBuffer);
 
     // Track where incoming data is read from and written to.
     this.mIncomingWriteIndex = 0;
     this.mIncomingReadIndex = 0;
 
     // Leave room for the parcel size for outgoing parcels.
-    this.mOutgoingIndex = PARCEL_SIZE_SIZE;
+    this.mOutgoingIndex = this.PARCEL_SIZE_SIZE;
 
     // How many bytes we've read for this parcel so far.
     this.mReadIncoming = 0;
 
     // How many bytes available as parcel data.
     this.mReadAvailable = 0;
 
     // Size of the incoming parcel. If this is zero, we're expecting a new
     // parcel.
     this.mCurrentParcelSize = 0;
 
-    // This gets incremented each time we send out a parcel.
-    this.mToken = 1;
-
-    // Maps tokens we send out with requests to the request type, so that
-    // when we get a response parcel back, we know what request it was for.
-    this.mTokenRequestMap = {};
-
     // Queue for storing outgoing override points
     this.mOutgoingBufferCalSizeQueue = [];
   },
 
   /**
    * Mark current mOutgoingIndex as start point for calculation length of data
    * written to mOutgoingBuffer.
    * Mark can be nested for here uses queue to remember marks.
@@ -494,28 +485,28 @@ let Buf = {
       debug("Already read " + this.mReadIncoming);
     }
 
     this.writeToIncoming(incoming);
     this.mReadIncoming += incoming.length;
     while (true) {
       if (!this.mCurrentParcelSize) {
         // We're expecting a new parcel.
-        if (this.mReadIncoming < PARCEL_SIZE_SIZE) {
+        if (this.mReadIncoming < this.PARCEL_SIZE_SIZE) {
           // We don't know how big the next parcel is going to be, need more
           // data.
           if (DEBUG) debug("Next parcel size unknown, going to sleep.");
           return;
         }
         this.mCurrentParcelSize = this.readParcelSize();
         if (DEBUG) {
           debug("New incoming parcel of size " + this.mCurrentParcelSize);
         }
         // The size itself is not included in the size.
-        this.mReadIncoming -= PARCEL_SIZE_SIZE;
+        this.mReadIncoming -= this.PARCEL_SIZE_SIZE;
       }
 
       if (this.mReadIncoming < this.mCurrentParcelSize) {
         // We haven't read enough yet in order to be able to process a parcel.
         if (DEBUG) debug("Read " + this.mReadIncoming + ", but parcel size is "
                          + this.mCurrentParcelSize + ". Going to sleep.");
         return;
       }
@@ -557,112 +548,54 @@ let Buf = {
       }
       this.mReadIncoming -= this.mCurrentParcelSize;
       this.mReadAvailable = 0;
       this.mCurrentParcelSize = 0;
     }
   },
 
   /**
-   * Process one parcel.
-   */
-  processParcel: function processParcel() {
-    let response_type = this.readUint32();
-
-    let request_type, options;
-    if (response_type == RESPONSE_TYPE_SOLICITED) {
-      let token = this.readUint32();
-      let error = this.readUint32();
-
-      options = this.mTokenRequestMap[token];
-      if (!options) {
-        if (DEBUG) {
-          debug("Suspicious uninvited request found: " + token + ". Ignored!");
-        }
-        return;
-      }
-
-      delete this.mTokenRequestMap[token];
-      request_type = options.rilRequestType;
-
-      options.rilRequestError = error;
-      if (DEBUG) {
-        debug("Solicited response for request type " + request_type +
-              ", token " + token + ", error " + error);
-      }
-    } else if (response_type == RESPONSE_TYPE_UNSOLICITED) {
-      request_type = this.readUint32();
-      if (DEBUG) debug("Unsolicited response for request type " + request_type);
-    } else {
-      if (DEBUG) debug("Unknown response type: " + response_type);
-      return;
-    }
-
-    RIL.handleParcel(request_type, this.mReadAvailable, options);
-  },
-
-  /**
-   * Start a new outgoing parcel.
-   *
-   * @param type
-   *        Integer specifying the request type.
-   * @param options [optional]
-   *        Object containing information about the request, e.g. the
-   *        original main thread message object that led to the RIL request.
-   */
-  newParcel: function newParcel(type, options) {
-    if (DEBUG) debug("New outgoing parcel of type " + type);
-
-    // We're going to leave room for the parcel size at the beginning.
-    this.mOutgoingIndex = PARCEL_SIZE_SIZE;
-    this.writeUint32(type);
-    this.writeUint32(this.mToken);
-
-    if (!options) {
-      options = {};
-    }
-    options.rilRequestType = type;
-    options.rilRequestError = null;
-    this.mTokenRequestMap[this.mToken] = options;
-    this.mToken++;
-    return this.mToken;
-  },
-
-  /**
-   * Communicate with the RIL IPC thread.
+   * Communicate with the IPC thread.
    */
   sendParcel: function sendParcel() {
     // Compute the size of the parcel and write it to the front of the parcel
     // where we left room for it. Note that he parcel size does not include
     // the size itself.
-    let parcelSize = this.mOutgoingIndex - PARCEL_SIZE_SIZE;
+    let parcelSize = this.mOutgoingIndex - this.PARCEL_SIZE_SIZE;
     this.writeParcelSize(parcelSize);
 
     // This assumes that postRILMessage will make a copy of the ArrayBufferView
     // right away!
     let parcel = this.mOutgoingBytes.subarray(0, this.mOutgoingIndex);
     if (DEBUG) debug("Outgoing parcel: " + Array.slice(parcel));
     this.onSendParcel(parcel);
-    this.mOutgoingIndex = PARCEL_SIZE_SIZE;
-  },
-
-  simpleRequest: function simpleRequest(type, options) {
-    this.newParcel(type, options);
-    this.sendParcel();
+    this.mOutgoingIndex = this.PARCEL_SIZE_SIZE;
   },
 
   getCurrentParcelSize: function getCurrentParcelSize() {
     return this.mCurrentParcelSize;
   },
 
   getReadAvailable: function getReadAvailable() {
     return this.mReadAvailable;
   }
 
   /**
+   * Process one parcel.
+   *
+   * |processParcel| is an implementation provided incoming parcel processing
+   * function invoked when we have received a complete parcel.  Implementation
+   * may call multiple read functions to extract data from the incoming buffer.
+   */
+  //processParcel: function processParcel() {
+  //  let something = this.readUint32();
+  //  ...
+  //},
+
+  /**
    * Write raw data out to underlying channel.
    *
    * |onSendParcel| is an implementation provided stream output function
    * invoked when we're really going to write something out.  We assume the
    * data are completely copied to some output buffer in this call and may
    * be destroyed when it's done.
    *
    * @param parcel