天天看点

从操作系统内核看设计模式--linux内核的Observer模式

<a>int atomic_notifier_chain_register(struct atomic_notifier_head *nh, struct notifier_block *n)</a>

<a>{</a>

<a>unsigned long flags;</a>

<a>int ret;</a>

<a>spin_lock_irqsave(&amp;nh-&gt;lock, flags);</a>

<a>ret = notifier_chain_register(&amp;nh-&gt;head, n);</a>

<a>spin_unlock_irqrestore(&amp;nh-&gt;lock, flags);</a>

<a>return ret;</a>

<a>}</a>

<a>static int notifier_chain_register(struct notifier_block **nl, struct notifier_block *n)</a>

<a>while ((*nl) != NULL) {</a>

<a>if (n-&gt;priority &gt; (*nl)-&gt;priority)</a>

<a>break;</a>

<a>nl = &amp;((*nl)-&gt;next);</a>

<a>n-&gt;next = *nl;</a>

<a>rcu_assign_pointer(*nl, n);</a>

<a>return 0;</a>

<a>int __kprobes __atomic_notifier_call_chain(struct atomic_notifier_head *nh, unsigned long val, void *v, int nr_to_call, int *nr_calls)</a>

<a>rcu_read_lock();</a>

<a>ret = notifier_call_chain(&amp;nh-&gt;head, val, v, nr_to_call, nr_calls);</a>

<a>rcu_read_unlock();</a>

<a>static int __kprobes notifier_call_chain(struct notifier_block **nl, unsigned long val, void *v, int nr_to_call, int *nr_calls)</a>

<a>int ret = NOTIFY_DONE;</a>

<a>struct notifier_block *nb, *next_nb;</a>

<a>nb = rcu_dereference(*nl);</a>

<a>while (nb &amp;&amp; nr_to_call) {</a>

<a>next_nb = rcu_dereference(nb-&gt;next);</a>

<a>if (nr_calls)</a>

<a>(*nr_calls)++;</a>

<a>if ((ret &amp; NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)</a>

<a>nb = next_nb;</a>

<a>nr_to_call--;</a>

 本文转自 dog250 51CTO博客,原文链接:http://blog.51cto.com/dog250/1273649

继续阅读