NSOperation GUNStep源码

2020-11-16  本文已影响0人  forping

gunstep foundation 代码下载
NSOperation GUNStep源码
NSInvocationOperation GUNStep源码
NSOperationQueue GUNStep源码

NSOperation

首先是初始化方法

- (id) init
{
  if ((self = [super init]) != nil)
    {
      GS_CREATE_INTERNAL(NSOperation); // 创建_internal 成员变量宏,_internal是 NSOperationInternal 类型的
      internal->priority = NSOperationQueuePriorityNormal; // 默认优先级
      internal->threadPriority = 0.5; //线程优先级
      internal->ready = YES; // 是否准备好
      internal->lock = [NSRecursiveLock new]; // 初始化递归锁 
      [internal->lock setName:
        [NSString stringWithFormat: @"lock-for-opqueue-%p", self]];
      internal->cond = [[NSConditionLock alloc] initWithCondition: 0];// 初始化条件锁
      [internal->cond setName:
        [NSString stringWithFormat: @"cond-for-opqueue-%p", self]];
      [self addObserver: self
             forKeyPath: @"isFinished"
                options: NSKeyValueObservingOptionNew
                context: isFinishedCtxt]; // 监听自己的完成状态
    }
  return self;
}

然后是dealloc函数

- (void) dealloc
{
  if (internal != nil)
    {
      NSOperation   *op;

      [self removeObserver: self forKeyPath: @"isFinished"]; // 移除监听完成
      while ((op = [internal->dependencies lastObject]) != nil)
    {
      [self removeDependency: op];// 移除依赖
    } 
      RELEASE(internal->dependencies);
      RELEASE(internal->cond);
      RELEASE(internal->lock);
      GS_DESTROY_INTERNAL(NSOperation);
    }
  [super dealloc];
}

main 方法

- (void) main;
{
  return;   // OSX default implementation does nothing
}

start 方法

- (void) start
{
  NSAutoreleasePool *pool = [NSAutoreleasePool new]; // 自动释放池
  double        prio = [NSThread  threadPriority]; // 线程的优先级

  AUTORELEASE(RETAIN(self));    // Make sure we exist while running.
  [internal->lock lock]; // 加锁
  NS_DURING // 异常处理
    {
      if (YES == [self isExecuting]) // 如果正在运行,抛出异常
    {
      [NSException raise: NSInvalidArgumentException
              format: @"[%@-%@] called on executing operation",
        NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
    }
      if (YES == [self isFinished]) // 如果完成,抛出异常
    {
      [NSException raise: NSInvalidArgumentException
              format: @"[%@-%@] called on finished operation",
        NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
    }
      if (NO == [self isReady]) // 如果没有准备就绪,抛出异常
    {
      [NSException raise: NSInvalidArgumentException
              format: @"[%@-%@] called on operation which is not ready",
        NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
    }
      if (NO == internal->executing) //  修改为正在运行,
    {
      [self willChangeValueForKey: @"isExecuting"];
      internal->executing = YES;
      [self didChangeValueForKey: @"isExecuting"];
    }
    }
  NS_HANDLER
    {
      [internal->lock unlock];
      [localException raise];
    }
  NS_ENDHANDLER
  [internal->lock unlock]; // 解锁

  NS_DURING
    {
      if (NO == [self isCancelled])
    {
      [NSThread setThreadPriority: internal->threadPriority]; // 设置线程优先级,然后执行main函数
      [self main];
    }
    }
  NS_HANDLER
    {
      [NSThread setThreadPriority:  prio];
      [localException raise];
    }
  NS_ENDHANDLER;

  [self _finish]; // 
  [pool release];
}

_finlish 方法

- (void) _finish
{
  /* retain while finishing so that we don't get deallocated when our
   * queue removes and releases us.
   */
  [self retain];
  [internal->lock lock]; // 加锁
  if (NO == internal->finished) 
    {
      if (YES == internal->executing) // 如果正在执行, 改为finlish
        {
      [self willChangeValueForKey: @"isExecuting"];
      [self willChangeValueForKey: @"isFinished"];
      internal->executing = NO;
      internal->finished = YES;
      [self didChangeValueForKey: @"isFinished"];
      [self didChangeValueForKey: @"isExecuting"];
    }
      else // 否则直接改为finlish
    {
      [self willChangeValueForKey: @"isFinished"];
      internal->finished = YES;
      [self didChangeValueForKey: @"isFinished"];
    }
      if (NULL != internal->completionBlock)
    { // 当前线程 回调block
      CALL_BLOCK_NO_ARGS(internal->completionBlock); 
    }
    }
  [internal->lock unlock];
  [self release];
}

添加依赖

- (void) addDependency: (NSOperation *)op // 添加依赖
{
// 异常判定
  if (NO == [op isKindOfClass: [NSOperation class]])
    {
      [NSException raise: NSInvalidArgumentException
          format: @"[%@-%@] dependency is not an NSOperation",
    NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
    }
  if (op == self)
    {
      [NSException raise: NSInvalidArgumentException
          format: @"[%@-%@] attempt to add dependency on self",
    NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
    }
//加锁
  [internal->lock lock];
  if (internal->dependencies == nil)
    {
      internal->dependencies = [[NSMutableArray alloc] initWithCapacity: 5]; // 创建依赖数组
    }

  NS_DURING
    {
      if (NSNotFound == [internal->dependencies indexOfObjectIdenticalTo: op]) // 判断是否已经有依赖了
    {
      [self willChangeValueForKey: @"dependencies"];
          [internal->dependencies addObject: op]; // 添加依赖
      /* We only need to watch for changes if it's possible for them to
       * happen and make a difference.
       */
      if (NO == [op isFinished]
        && NO == [self isCancelled]
        && NO == [self isExecuting]
        && NO == [self isFinished]) // 如果依赖的操作还没有完成,或者自己还没执行/取消
        {
          /* Can change readiness if we are neither cancelled nor
           * executing nor finished.  So we need to observe for the
           * finish of the dependency.
           */
          [op addObserver: self
           forKeyPath: @"isFinished"
              options: NSKeyValueObservingOptionNew
              context: isFinishedCtxt]; // 监听依赖的完成情况
          if (internal->ready == YES) // 如果自己已经准备就绪了,取消准备就绪
        {
          /* The new dependency stops us being ready ...
           * change state.
           */
          [self willChangeValueForKey: @"isReady"];
          internal->ready = NO; // 没有准备好
          [self didChangeValueForKey: @"isReady"];
        }
        }
      [self didChangeValueForKey: @"dependencies"];
    }
    }
  NS_HANDLER // 处理异常
    {
      [internal->lock unlock];
      NSLog(@"Problem adding dependency: %@", localException);
      return;
    }
  NS_ENDHANDLER
  [internal->lock unlock];
}

kvo的回调

- (void) observeValueForKeyPath: (NSString *)keyPath
               ofObject: (id)object
                         change: (NSDictionary *)change
                        context: (void *)context
{
  [internal->lock lock];

  /* We only observe isFinished changes, and we can remove self as an
   * observer once we know the operation has finished since it can never
   * become unfinished.
   */
    // 已经完成了就不监听了
  [object removeObserver: self forKeyPath: @"isFinished"];

  if (object == self)
    { // 如果是自己完成了
      /* We have finished and need to unlock the condition lock so that
       * any waiting thread can continue.
       */
      [internal->cond lock];
      [internal->cond unlockWithCondition: 1];
      [internal->lock unlock];
      return;
    }

  if (NO == internal->ready) // 如果自己还没准备好, 判断所有的依赖,如果没有依赖了,就修改自己的状态
    {
      NSEnumerator  *en;
      NSOperation   *op;

      /* Some dependency has finished (or been removed) ...
       * so we need to check to see if we are now ready unless we know we are.
       * This is protected by locks so that an update due to an observed
       * change in one thread won't interrupt anything in another thread.
       */
      en = [internal->dependencies objectEnumerator]; // 获得依赖
      while ((op = [en nextObject]) != nil)
        {
          if (NO == [op isFinished])
        break;
        }
      if (op == nil)
    {
          [self willChangeValueForKey: @"isReady"];
      internal->ready = YES; // 准备好
          [self didChangeValueForKey: @"isReady"];
    }
    }
  [internal->lock unlock];
}

移除依赖

- (void) removeDependency: (NSOperation *)op
{
  [internal->lock lock];
  NS_DURING
    {
      if (NSNotFound != [internal->dependencies indexOfObjectIdenticalTo: op]) // 如果依赖关系存在
    {
      [op removeObserver: self forKeyPath: @"isFinished"]; // 移除对依赖的finlish的监听
      [self willChangeValueForKey: @"dependencies"];
      [internal->dependencies removeObject: op]; // 移除依赖
      if (NO == internal->ready) // 如果自己是没准备好的状态
        {
          /* The dependency may cause us to become ready ...
           * fake an observation so we can deal with that.
           */
          [self observeValueForKeyPath: @"isFinished"
                  ofObject: op
                    change: nil
                   context: isFinishedCtxt]; // 手动触发一次kvo回调,认为依赖操作已经完成
        }
      [self didChangeValueForKey: @"dependencies"];
    }
    }
  NS_HANDLER
    {
      [internal->lock unlock];
      NSLog(@"Problem removing dependency: %@", localException);
      return;
    }
  NS_ENDHANDLER
  [internal->lock unlock];
}

返回依赖

- (NSArray *) dependencies
{
  NSArray   *a;

  if (internal->dependencies == nil)
    {
      a = empty;    // OSX return an empty array, 静态的空数组
    }
  else
    {
      [internal->lock lock];
      a = [NSArray arrayWithArray: internal->dependencies];
      [internal->lock unlock];
    }
  return a;
}

cancel方法

- (void) cancel
{
  if (NO == internal->cancelled && NO == [self isFinished]) // 判断条件
    {
      [internal->lock lock];
      if (NO == internal->cancelled && NO == [self isFinished]) // 加锁后再判断一下
    {
      NS_DURING
        { 设置自己的状态
          [self willChangeValueForKey: @"isCancelled"];
          internal->cancelled = YES;
          if (NO == internal->ready)
        {
              [self willChangeValueForKey: @"isReady"];
          internal->ready = YES;
              [self didChangeValueForKey: @"isReady"];
        }
          [self didChangeValueForKey: @"isCancelled"];
        }
      NS_HANDLER
        {
          [internal->lock unlock];
          NSLog(@"Problem cancelling operation: %@", localException);
          return;
        }
      NS_ENDHANDLER
    }
      [internal->lock unlock];
    }
}

等待直到完成:就是一个条件锁, 初始条件设置为0, 只有当自己的任务完成时,条件设置为1 ,这个代码才会执行完成,

- (void) waitUntilFinished
{
  [internal->cond lockWhenCondition: 1];    // Wait for finish
  [internal->cond unlockWithCondition: 1];  // Signal any other watchers
}

简单总结: 以上就是NSOperation的主要代码, 没有涉及到创建线程的概念,执行的时候会调用start方法,start方法里面修改状态,并调用main方法. 如果要继承,只需要重写main方法即可

NSOperaion的 queuePriority 属性用于 NSOperationQueue waiting 数组排序.

上一篇下一篇

猜你喜欢

热点阅读