+
+ /***************** Resource ****************/
+ // SDH : 8-7-2009 : testing if there's more then one client allows
+ // the compiler to eliminate most of the logic when there's only one
+ // client.
+ task void grantTask() {
+
+
+ if (TINYOS_N_NETWORKS > 1) {
+ if (resource_owner == OWNER_NONE && !(call Queue.isEmpty())) {
+ resource_owner = call Queue.dequeue();
+
+ if (resource_owner != OWNER_NONE) {
+ signal Resource.granted[resource_owner]();
+ }
+ }
+ } else {
+ if (next_owner != resource_owner) {
+ resource_owner = next_owner;
+ signal Resource.granted[resource_owner]();
+ }
+ }
+ }
+
+ async command error_t Resource.request[uint8_t id]() {
+
+ post grantTask();
+
+ if (TINYOS_N_NETWORKS > 1) {
+ return call Queue.enqueue(id);
+ } else {
+ if (id == resource_owner) {
+ return EALREADY;
+ } else {
+ next_owner = id;
+ return SUCCESS;
+ }
+ }
+ }
+
+ async command error_t Resource.immediateRequest[uint8_t id]() {
+ if (resource_owner == id) return EALREADY;
+
+ if (TINYOS_N_NETWORKS > 1) {
+ if (resource_owner == OWNER_NONE && call Queue.isEmpty()) {
+ resource_owner = id;
+ return SUCCESS;
+ }
+ return FAIL;
+ } else {
+ resource_owner = id;
+ return SUCCESS;
+ }
+ }
+ async command error_t Resource.release[uint8_t id]() {
+ if (TINYOS_N_NETWORKS > 1) {
+ post grantTask();
+ }
+ resource_owner = OWNER_NONE;
+ return SUCCESS;
+ }
+ async command bool Resource.isOwner[uint8_t id]() {
+ return (id == resource_owner);
+ }
+