Bez popisu

FIRAuthSerialTaskQueue.m 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright 2017 Google
  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. #import "FIRAuthSerialTaskQueue.h"
  17. #import "FIRAuthGlobalWorkQueue.h"
  18. NS_ASSUME_NONNULL_BEGIN
  19. @implementation FIRAuthSerialTaskQueue {
  20. /** @var _dispatchQueue
  21. @brief The asyncronous dispatch queue into which tasks are enqueued and processed
  22. serially.
  23. */
  24. dispatch_queue_t _dispatchQueue;
  25. }
  26. - (instancetype)init {
  27. self = [super init];
  28. if (self) {
  29. _dispatchQueue = dispatch_queue_create("com.google.firebase.auth.serialTaskQueue", NULL);
  30. dispatch_set_target_queue(_dispatchQueue, FIRAuthGlobalWorkQueue());
  31. }
  32. return self;
  33. }
  34. - (void)enqueueTask:(FIRAuthSerialTask)task {
  35. // This dispatch queue will run tasks serially in FIFO order, as long as it's not suspended.
  36. dispatch_async(self->_dispatchQueue, ^{
  37. // But as soon as a task is started, stop other tasks from running until the task calls it's
  38. // completion handler, which allows the queue to resume processing of tasks. This allows the
  39. // task to perform other asyncronous actions on other dispatch queues and "get back to us" when
  40. // all of their sub-tasks are complete.
  41. dispatch_suspend(self->_dispatchQueue);
  42. task(^{
  43. dispatch_resume(self->_dispatchQueue);
  44. });
  45. });
  46. }
  47. @end
  48. NS_ASSUME_NONNULL_END