RLMScheduler.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2023 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import <Realm/RLMConstants.h>
  19. #ifdef __cplusplus
  20. #include <memory>
  21. namespace realm::util {
  22. class Scheduler;
  23. }
  24. #endif
  25. RLM_HEADER_AUDIT_BEGIN(nullability, sendability)
  26. // A serial work queue of some sort which represents a thread-confinement context
  27. // of some sort which blocks can be invoked inside. Realms are confined to a
  28. // scheduler, which can be a thread (actually a run loop), a dispatch queue, or
  29. // an actor. The scheduler ensures that the Realm is only used on one thread at
  30. // a time, and allows us to dispatch work to the thread where we can access the
  31. // Realm safely.
  32. RLM_SWIFT_SENDABLE // is immutable
  33. @interface RLMScheduler : NSObject
  34. + (RLMScheduler *)mainRunLoop __attribute__((objc_direct));
  35. + (RLMScheduler *)currentRunLoop __attribute__((objc_direct));
  36. // A scheduler for the given queue if it's non-nil, and currentRunLoop otherwise
  37. + (RLMScheduler *)dispatchQueue:(nullable dispatch_queue_t)queue;
  38. + (RLMScheduler *)actor:(id)actor invoke:(void (^)(dispatch_block_t))invoke
  39. verify:(void (^)(void))verify;
  40. // Invoke the block on this scheduler. Currently not actually implement for run
  41. // loop schedulers.
  42. - (void)invoke:(dispatch_block_t)block;
  43. // Cache key for this scheduler suitable for use with NSMapTable. Only valid
  44. // when called from the current scheduler.
  45. - (void *)cacheKey;
  46. - (nullable id)actor;
  47. #ifdef __cplusplus
  48. // The object store Scheduler corresponding to this scheduler
  49. - (std::shared_ptr<realm::util::Scheduler>)osScheduler;
  50. #endif
  51. @end
  52. FOUNDATION_EXTERN void RLMSetMainActor(id actor);
  53. RLM_HEADER_AUDIT_END(nullability, sendability)