GetGiftRunLoop.m 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // GetGiftRunLoop.m
  3. // BuguLive
  4. //
  5. // Created by xfg on 16/7/6.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "GetGiftRunLoop.h"
  9. #include <sys/time.h>
  10. @implementation GetGiftRunLoop
  11. static void GetGiftEvent(void* info __unused)
  12. {
  13. // NSLog(@"MSFRunloop source perform");
  14. // do nothing
  15. }
  16. - (void)dealloc
  17. {
  18. DebugLog(@"%@ [%@] Release", self, [GetGiftRunLoop class]);
  19. }
  20. static GetGiftRunLoop *_sharedRunLoop = nil;
  21. + (GetGiftRunLoop *)sharedGetGiftRunLoop
  22. {
  23. static dispatch_once_t predicate;
  24. dispatch_once(&predicate, ^{
  25. _sharedRunLoop = [[GetGiftRunLoop alloc] init];
  26. });
  27. return _sharedRunLoop;
  28. }
  29. - (id)init
  30. {
  31. if(self = [super init])
  32. {
  33. [self start];
  34. }
  35. return self;
  36. }
  37. - (void)runloopThreadEntry
  38. {
  39. NSAssert(![NSThread isMainThread], @"runloopThread error");
  40. CFRunLoopSourceRef source;
  41. CFRunLoopSourceContext sourceContext;
  42. bzero(&sourceContext, sizeof(sourceContext));
  43. sourceContext.perform = GetGiftEvent;
  44. source = CFRunLoopSourceCreate(NULL, 0, &sourceContext);
  45. CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes);
  46. while (YES)
  47. {
  48. [[NSRunLoop currentRunLoop] run];
  49. }
  50. }
  51. - (void)start
  52. {
  53. if(_thread)
  54. {
  55. return;
  56. }
  57. _thread = [[NSThread alloc] initWithTarget:self selector:@selector(runloopThreadEntry) object:nil];
  58. [_thread setName:@"GetGiftRunLoopThread"];
  59. if ([[IOSDeviceConfig sharedConfig] isIOS7Later])
  60. {
  61. // 设置成线程优先级(优先级最好不要太高,需要保证AV正常)
  62. [_thread setQualityOfService:NSQualityOfServiceUtility];
  63. }
  64. else
  65. {
  66. [_thread setThreadPriority:1.0];
  67. }
  68. [_thread start];
  69. }
  70. @end