⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kernel_events.html

📁 ADI 公司blackfin系列的用户使用文挡。
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head>  <title></title>  <link rel="stylesheet" media="screen" type="text/css" href="./style.css" />  <link rel="stylesheet" media="screen" type="text/css" href="./design.css" />  <link rel="stylesheet" media="print" type="text/css" href="./print.css" />  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><a href=start.html>start</a></br><h2><a name="kernel_events" id="kernel_events">Kernel Events</a></h2><div class="level2"><p> This is really all about the work queue.</p><p>Work Queues can be used when a kernel system  or device driver wants to schedule a deferred task and get on with the work in hand knowing that the deferred task will be processed as soon at possible. </p><p>This detection of deferred work could possibly have happened during an interrupt routine, The function that needed to be executed may not have to be completed immediately but  could be deferred until after the interrupt routine has completed.</p><p>There is a special kernel <strong>events</strong> task set up for each CPU in a system.</p><p>The command <strong>ps ax</strong> will show these tasks</p><pre class="code">   PID TTY      STAT   TIME COMMAND  1 ?        S      0:04 init [5]  2 ?        SN     0:00 [ksoftirqd/0]  3 ?        S&lt;     0:06 [events/0] &lt; this is the event processor  [... the rest are missing  ...]</pre><p> The <strong>events</strong> thread is created by this code from <strong>kernel/workqueue.c</strong></p><pre class="code c"><span class="kw4">void</span> init_workqueues<span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>&nbsp;<span class="br0">&#123;</span>        hotcpu_notifier<span class="br0">&#40;</span>workqueue_cpu_callback, <span class="nu0">0</span><span class="br0">&#41;</span>;        keventd_wq = create_workqueue<span class="br0">&#40;</span><span class="st0">"events"</span><span class="br0">&#41;</span>;        BUG_ON<span class="br0">&#40;</span>!keventd_wq<span class="br0">&#41;</span>;<span class="br0">&#125;</span></pre><p>This code is called from the kernel startup program <strong>init/main.c</strong> ( Yes the kernel does have a main or at least a main.c )</p><pre class="code c">&nbsp;<span class="kw4">static</span> <span class="kw4">void</span> __init do_basic_setup<span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span><span class="br0">&#123;</span>        <span class="coMULTI">/* drivers will send hotplug events */</span>        init_workqueues<span class="br0">&#40;</span><span class="br0">&#41;</span>;&nbsp;        <span class="br0">&#91;</span>... <span class="me1">rest</span> missing ...<span class="br0">&#93;</span>&nbsp;</pre><p>The <strong>create_workqueue</strong> code  creates a <strong>worker_thread</strong> for each CPU. This thread sets up a workqueue and a waitqueue and then sleeps waiting work to arrive on the workqueue.</p><p>  The workqueue is a list of functions that need to be executed. They are stored in a structure containing a pointer to the function to be executed and some optional data to pass to  the function.</p><pre class="code c">&nbsp;<span class="kw4">struct</span> work_struct <span class="br0">&#123;</span>        <span class="kw4">unsigned</span> <span class="kw4">long</span> pending;        <span class="kw4">struct</span> list_head entry;        <span class="kw4">void</span> <span class="br0">&#40;</span>*func<span class="br0">&#41;</span><span class="br0">&#40;</span><span class="kw4">void</span> *<span class="br0">&#41;</span>;        <span class="kw4">void</span> *data;        <span class="kw4">void</span> *wq_data;        <span class="kw4">struct</span> timer_list timer;<span class="br0">&#125;</span>;&nbsp;</pre><p>The process of declaring or initializing work consists of setting up such a structure. A function and possibly some data are provided and the whole thing is given a name.</p><p> The process of scheduling work is the action of adding this structure to a workqueue and waking up the waitqueue.</p></div><!-- SECTION [1-2288] --><h3><a name="event_code_examples" id="event_code_examples">Event Code Examples</a></h3><div class="level3"><p> Some code examples help explain this.</p><p>   from <strong>drivers/char/tty_io.c</strong> Associate a function and some data with a work queue name</p><pre class="code c">  <span class="co1">//this element is inside the tty structure</span>&nbsp;  <span class="kw4">struct</span> work_struct hangup_work;&nbsp;  <span class="co1">// when setting up a tty device this is done</span>  INIT_WORK<span class="br0">&#40;</span>&amp;tty-&gt;hangup_work, do_tty_hangup, tty<span class="br0">&#41;</span>;&nbsp;</pre><p>Then when the tty driver detects a hangup the following is used.</p><pre class="code c">&nbsp;   <span class="co1">// kick off the hangup task</span>   schedule_work<span class="br0">&#40;</span>&amp;tty-&gt;hangup_work<span class="br0">&#41;</span>;&nbsp;</pre><p> A nice touch is this fact that you can wait for the hangup task to complete its execution</p><pre class="code c">&nbsp;   <span class="co1">// wait for the hangup task to complete</span>   flush_scheduled_work<span class="br0">&#40;</span><span class="br0">&#41;</span>;</pre><p> You can also schedule the work to occur at the next system tick. This is useful when you expect a whole list of events  to occur and feel safe in letting a bunch of events build up before servicing the lot of them.</p><pre class="code c">&nbsp;   <span class="co1">// schedule the work but do it later</span>   schedule_delayed_work<span class="br0">&#40;</span>&amp;tty-&gt;flip.<span class="me1">work</span>, <span class="nu0">1</span><span class="br0">&#41;</span>;</pre><p>The scheduled work can also be cancelled. </p><p>In the tty driver this is done when the device is to be closed and any characters received are to be discarded.</p><pre class="code c">&nbsp;   <span class="co1">// OK I've changed my mind </span>   cancel_delayed_work<span class="br0">&#40;</span>&amp;tty-&gt;flip.<span class="me1">work</span><span class="br0">&#41;</span>;</pre></div><!-- SECTION [2289-] --></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -