]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/system/SharedArbiterP.nc
Fix TMI copyright attributions
[tinyos-2.x.git] / tos / system / SharedArbiterP.nc
index c3503131cde02a93edb6d775de6a5b9d807ad0d6..094dbc1b7ead6bf56840b2d5fd5573adce4264d6 100644 (file)
@@ -10,7 +10,7 @@
  * - Redistributions in binary form must reproduce the above copyright
  *   notice, this list of conditions and the following disclaimer in the
  *   documentation and/or other materials provided with the distribution.
- * - Neither the name of the Technische Universität Berlin nor the names
+ * - Neither the name of the Titanium Mirror, Inc. nor the names
  *   of its contributors may be used to endorse or promote products derived
  *   from this software without specific prior written permission.
  *
  * switched subsystem.  For example, a communications bus that can handle IO
  * for multiple clients simultaneously, but is powered down when not in use.
  *
- * p_dfltOwnerId = uniqueCount() use by the resource.  see SharedArbiterC
- *
  * @author R. Steve McKown <rsmckown@gmail.com>
  */
 
-generic module SharedArbiterP(uint8_t p_dfltOwnerId) {
+generic module SharedArbiterP() {
   provides {
-    interface Init;
     interface Resource[uint8_t id];
     interface ResourceDefaultOwner;
     interface ArbiterInfo;
@@ -55,6 +52,7 @@ implementation {
   enum {
     S_IDLE = 0,                /* Resource is not in use; owned by default owner */
     S_REQUESTING,      /* Requesting the resource from the default owner */
+    S_IMMREQUESTING,   /* Immediate resource request from the default owner */
     S_GRANTED,         /* One or more clients have been granted the resource */
 
     NO_CLIENT = 0xff,
@@ -119,33 +117,37 @@ implementation {
     }
   }
 
-  command error_t Init.init()
-  {
-    call GrantedVector.clearAll();
-    call RequestingVector.clearAll();
-    return SUCCESS;
-  }
-
   async command error_t Resource.request[uint8_t id]()
   {
+    /* The Resource docs do not say what happens when a user calls
+     * Resource.request() after the resource has already been granted.  We
+     * elect to call EBUSY, as if the request was still in process.
+     */
     if (call RequestingVector.get(id) || call GrantedVector.get(id))
-      return FAIL;
+      return EBUSY;
 
     addRequesting(id);
-    if (call State.isIdle())
+    if (call State.requestState(S_REQUESTING) == SUCCESS)
       signal ResourceDefaultOwner.requested();
-    if (call State.isState(S_GRANTED))
+    else if (call State.isState(S_GRANTED))
       post grantedTask();
     return SUCCESS;
   }
 
   async command error_t Resource.immediateRequest[uint8_t id]()
   {
+    /* The Resource docs do not say what happens when a user calls
+     * Resource.request() after the resource has already been granted.  We
+     * elect to call EBUSY, as if the request was still in process.
+     */
     if (call RequestingVector.get(id) || call GrantedVector.get(id))
-      return FAIL;
+      return EBUSY;
 
-    if (call State.isIdle())
+    if (call State.requestState(S_IMMREQUESTING) == SUCCESS) {
       signal ResourceDefaultOwner.immediateRequested();
+      if (call State.isState(S_IMMREQUESTING))
+       call State.toIdle();
+    }
     if (call State.isState(S_GRANTED)) {
       addGranted(id);
       return SUCCESS;
@@ -167,14 +169,15 @@ implementation {
 
   async command error_t ResourceDefaultOwner.release()
   {
-    atomic {
-      if (m_requesting > 0) {
-       call State.forceState(S_GRANTED);
-       post grantedTask();
-       return SUCCESS;
-      } else
-       return FAIL;
-    }
+    if (call State.isState(S_REQUESTING)) {
+      call State.forceState(S_GRANTED);
+      post grantedTask();
+      return SUCCESS;
+    } else if (call State.isState(S_IMMREQUESTING)) {
+      call State.forceState(S_GRANTED);
+      return SUCCESS;
+    } else
+      return FAIL;
   }
 
   async command bool ArbiterInfo.inUse()