You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.7 KiB

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