Binder.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (C) 2005 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <hwbinder/Binder.h>
  17. #include <atomic>
  18. #include <utils/misc.h>
  19. #include <hwbinder/BpHwBinder.h>
  20. #include <hwbinder/IInterface.h>
  21. #include <hwbinder/Parcel.h>
  22. #include <sched.h>
  23. #include <stdio.h>
  24. namespace android {
  25. namespace hardware {
  26. // ---------------------------------------------------------------------------
  27. IBinder::IBinder()
  28. : RefBase()
  29. {
  30. }
  31. IBinder::~IBinder()
  32. {
  33. }
  34. // ---------------------------------------------------------------------------
  35. BHwBinder* IBinder::localBinder()
  36. {
  37. return nullptr;
  38. }
  39. BpHwBinder* IBinder::remoteBinder()
  40. {
  41. return nullptr;
  42. }
  43. bool IBinder::checkSubclass(const void* /*subclassID*/) const
  44. {
  45. return false;
  46. }
  47. // ---------------------------------------------------------------------------
  48. class BHwBinder::Extras
  49. {
  50. public:
  51. // unlocked objects
  52. bool mRequestingSid = false;
  53. // for below objects
  54. Mutex mLock;
  55. BpHwBinder::ObjectManager mObjects;
  56. };
  57. // ---------------------------------------------------------------------------
  58. BHwBinder::BHwBinder() : mSchedPolicy(SCHED_NORMAL), mSchedPriority(0), mExtras(nullptr)
  59. {
  60. }
  61. int BHwBinder::getMinSchedulingPolicy() {
  62. return mSchedPolicy;
  63. }
  64. int BHwBinder::getMinSchedulingPriority() {
  65. return mSchedPriority;
  66. }
  67. bool BHwBinder::isRequestingSid() {
  68. Extras* e = mExtras.load(std::memory_order_acquire);
  69. return e && e->mRequestingSid;
  70. }
  71. void BHwBinder::setRequestingSid(bool requestingSid) {
  72. Extras* e = mExtras.load(std::memory_order_acquire);
  73. if (!e) {
  74. // default is false. Most things don't need sids, so avoiding allocations when possible.
  75. if (!requestingSid) {
  76. return;
  77. }
  78. e = getOrCreateExtras();
  79. if (!e) return; // out of memory
  80. }
  81. e->mRequestingSid = requestingSid;
  82. }
  83. status_t BHwBinder::transact(
  84. uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags, TransactCallback callback)
  85. {
  86. data.setDataPosition(0);
  87. status_t err = NO_ERROR;
  88. switch (code) {
  89. default:
  90. err = onTransact(code, data, reply, flags,
  91. [&](auto &replyParcel) {
  92. replyParcel.setDataPosition(0);
  93. if (callback != nullptr) {
  94. callback(replyParcel);
  95. }
  96. });
  97. break;
  98. }
  99. return err;
  100. }
  101. status_t BHwBinder::linkToDeath(
  102. const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
  103. uint32_t /*flags*/)
  104. {
  105. return INVALID_OPERATION;
  106. }
  107. status_t BHwBinder::unlinkToDeath(
  108. const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
  109. uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
  110. {
  111. return INVALID_OPERATION;
  112. }
  113. void BHwBinder::attachObject(
  114. const void* objectID, void* object, void* cleanupCookie,
  115. object_cleanup_func func)
  116. {
  117. Extras* e = getOrCreateExtras();
  118. if (!e) return; // out of memory
  119. AutoMutex _l(e->mLock);
  120. e->mObjects.attach(objectID, object, cleanupCookie, func);
  121. }
  122. void* BHwBinder::findObject(const void* objectID) const
  123. {
  124. Extras* e = mExtras.load(std::memory_order_acquire);
  125. if (!e) return nullptr;
  126. AutoMutex _l(e->mLock);
  127. return e->mObjects.find(objectID);
  128. }
  129. void BHwBinder::detachObject(const void* objectID)
  130. {
  131. Extras* e = mExtras.load(std::memory_order_acquire);
  132. if (!e) return;
  133. AutoMutex _l(e->mLock);
  134. e->mObjects.detach(objectID);
  135. }
  136. BHwBinder* BHwBinder::localBinder()
  137. {
  138. return this;
  139. }
  140. BHwBinder::~BHwBinder()
  141. {
  142. Extras* e = mExtras.load(std::memory_order_relaxed);
  143. if (e) delete e;
  144. }
  145. status_t BHwBinder::onTransact(
  146. uint32_t /*code*/, const Parcel& /*data*/, Parcel* /*reply*/, uint32_t /*flags*/,
  147. TransactCallback /*callback*/)
  148. {
  149. return UNKNOWN_TRANSACTION;
  150. }
  151. BHwBinder::Extras* BHwBinder::getOrCreateExtras()
  152. {
  153. Extras* e = mExtras.load(std::memory_order_acquire);
  154. if (!e) {
  155. e = new Extras;
  156. Extras* expected = nullptr;
  157. if (!mExtras.compare_exchange_strong(expected, e,
  158. std::memory_order_release,
  159. std::memory_order_acquire)) {
  160. delete e;
  161. e = expected; // Filled in by CAS
  162. }
  163. if (e == nullptr) return nullptr; // out of memory
  164. }
  165. return e;
  166. }
  167. // ---------------------------------------------------------------------------
  168. enum {
  169. // This is used to transfer ownership of the remote binder from
  170. // the BpHwRefBase object holding it (when it is constructed), to the
  171. // owner of the BpHwRefBase object when it first acquires that BpHwRefBase.
  172. kRemoteAcquired = 0x00000001
  173. };
  174. BpHwRefBase::BpHwRefBase(const sp<IBinder>& o)
  175. : mRemote(o.get()), mRefs(nullptr), mState(0)
  176. {
  177. if (mRemote) {
  178. mRemote->incStrong(this); // Removed on first IncStrong().
  179. }
  180. }
  181. BpHwRefBase::~BpHwRefBase()
  182. {
  183. if (mRemote) {
  184. if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
  185. mRemote->decStrong(this);
  186. }
  187. }
  188. }
  189. void BpHwRefBase::onFirstRef()
  190. {
  191. mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
  192. }
  193. void BpHwRefBase::onLastStrongRef(const void* /*id*/)
  194. {
  195. if (mRemote) {
  196. mRemote->decStrong(this);
  197. }
  198. }
  199. bool BpHwRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
  200. {
  201. return false;
  202. }
  203. // ---------------------------------------------------------------------------
  204. }; // namespace hardware
  205. }; // namespace android