CEF框架中的一些宏定义(二):CEF_CURRENTLY_ON

CEF_CURRENTLY_ON

前面有一篇分析进程和线程的文章提到过:
CEF线程模型与初始化过程详解

在Browser进程中在CEF框架中,很多代码都需要由这个browser的主线程来执行,宏定义CEF_CURRENTLY_ON就是用于这个判断的。

这个宏定义及其相关的宏定义在thread_util.h中定义:

#define CEF_UIT content::BrowserThread::UI
#define CEF_IOT content::BrowserThread::IO

#define CEF_CURRENTLY_ON(id) content::BrowserThread::CurrentlyOn(id)
#define CEF_CURRENTLY_ON_UIT() CEF_CURRENTLY_ON(CEF_UIT)
#define CEF_CURRENTLY_ON_IOT() CEF_CURRENTLY_ON(CEF_IOT)

#define 
(id) DCHECK(CEF_CURRENTLY_ON(id))
#define CEF_REQUIRE_UIT() CEF_REQUIRE(CEF_UIT)
#define CEF_REQUIRE_IOT() CEF_REQUIRE(CEF_IOT)

#define CEF_REQUIRE_RETURN(id, var)              \
  if (!CEF_CURRENTLY_ON(id)) {                   \
    DCHECK(false) << "called on invalid thread"; \
    return var;                                  \
  }
#define CEF_REQUIRE_UIT_RETURN(var) CEF_REQUIRE_RETURN(CEF_UIT, var)
#define CEF_REQUIRE_IOT_RETURN(var) CEF_REQUIRE_RETURN(CEF_IOT, var)

#define CEF_REQUIRE_RETURN_VOID(id)              \
  if (!CEF_CURRENTLY_ON(id)) {                   \
    DCHECK(false) << "called on invalid thread"; \
    return;                                      \
  }
#define CEF_REQUIRE_UIT_RETURN_VOID() CEF_REQUIRE_RETURN_VOID(CEF_UIT)
#define CEF_REQUIRE_IOT_RETURN_VOID() CEF_REQUIRE_RETURN_VOID(CEF_IOT)

CEF_UIT & CEF_IOT

这个宏定义就是获取content::BrowserThread::UI,这个定义在chrome的源码content/public/browser/browser_thread.h中,是一个枚举类型:

  enum ID {
    // The main thread in the browser. It stops running tasks during shutdown
    // and is never joined.
    UI,

    // This is the thread that processes non-blocking I/O, i.e. IPC and network.
    // Blocking I/O should happen in base::ThreadPool. It is joined on shutdown
    // (and thus any task posted to it may block shutdown).
    //
    // The name is admittedly confusing, as the IO thread is not for blocking
    // I/O like calling base::File::Read. "The highly responsive, non-blocking
    // I/O thread for IPC" is more accurate but too long for an enum name. See
    // docs/transcripts/wuwt-e08-processes.md at 44:20 for more history.
    IO,

    // NOTE: do not add new threads here. Instead you should just use
    // base::ThreadPool::Create*TaskRunner to run tasks on the base::ThreadPool.

    // This identifier does not represent a thread.  Instead it counts the
    // number of well-known threads.  Insert new well-known threads before this
    // identifier.
    ID_COUNT
  };

CEF_CURRENTLY_ON

这个宏定义直接定义成了chrome源码中的content::BrowserThread::CurrentlyOn,这个函数同样存在于content/public/browser/browser_thread.h中:

函数定义在content/browser/browser_thread_impl.cc中:

// static
bool BrowserThread::CurrentlyOn(ID identifier) {
  DCHECK_GE(identifier, 0);
  DCHECK_LT(identifier, ID_COUNT);

  BrowserThreadGlobals& globals = GetBrowserThreadGlobals();

  // Thread-safe since |globals.task_runners| is read-only after being
  // initialized from main thread (which happens before //content and embedders
  // are kicked off and enabled to call the BrowserThread API from other
  // threads).
  return globals.task_runners[identifier] &&
         globals.task_runners[identifier]->RunsTasksInCurrentSequence();
}
  • DCHECK_GE和DCHECK_LT就是对这个ID进行断言判断,需要大于0,小于进程数
  • task_runners是chrome里面的任务runner,可以参考CEF线程模型与初始化过程详解。
  • 这个函数就是判断线程ID是否相同。
  • CEF_REQUIRE宏定义就是配合DCHECK一起使用。

CEF_TASK_RUNNER && CEF_POST_TASK

在CEF线程模型与初始化过程详解提到了chrome的基本线程模型,任何任务都是需要通过POST一个runner来执行的,在cef框架中也用到了chrome源码中的几个任务处理宏定义:CEF_TASK_RUNNER 和 CEF_POST_TASK。

template  = true>
auto CEF_TASK_RUNNER() {
  return content::GetUIThreadTaskRunner({});
}
template  = true>
auto CEF_TASK_RUNNER() {
  return content::GetIOThreadTaskRunner({});
}

#define CEF_POST_TASK(id, task) CEF_TASK_RUNNER()->PostTask(FROM_HERE, task)

这个宏定义简单来说,就是通过模板定义,在CEF_POST_TASK宏传入的id为UI或者IOT的时候,获得对应的TaskRunner,然后把task任务发送过去。

你可能感兴趣的:(CEF框架实战,CEF,CEF宏定义)