Publish/Subscribe Model——Notification chain——观察者模式

内核中用的很多,整理时间子系统的时候又遇到了notification mechanism,因此做次记录:

参考:1、http://msdn.microsoft.com/en-us/library/ff649664.aspx

          2、http://blog.csdn.net/lovelion/article/details/7720232

          3、2.6.34

  观察者(Observer)模式是对象的行为型模式,又叫做发表-订阅(Publish/Subscribe)模式模型-视图(Model/View)模式源-收听者(Source/Listener)模式或从属者(Dependents)模式。

  当某件事情发生时,有多个应用需要知道该事件发生并作出相应的反映。

Solution:

  Enable listening applications to subscribe to specific messages. Create a mechanism that sends messages to all interested subscribers. The three variations of the Publish/Subscribe pattern you can use to create a mechanism that sends messages to all interested subscribers are List-Based Publish/Subscribe, Broadcast-Based Publish/Subscribe, and Content-Based Publish/Subscribe.

  实际上内核中使用的notification 机制就是基于List-Based Publish/Subscribe,下文也着重介绍List-Based Publish/Subscribe,另外两种请阅读第一个参考链接。

List-Based Publish/Subscribe

A List-Based Publish/Subscribe pattern advises you to identify a subject and to maintain a list of subscribers for that subject. When events occur, you have the subject notify each subscriber on the subscription list. When you use this pattern, you identifytwo classes: subjects and observers. Assuming you use a push model update, you add three methods to the subject: Attach(),Detach(),and Notify().You add one method to the observer—Update().
在使用基于链表的 发布——订阅 模式时,我们需要识别出一个subject,而且维护一个对该subject感兴趣的subcriber的链表。当相关事件发生时,我们就通过该subject通知subscription list上的每一个subscriber.当我们使用该模式时,需要鉴别出两个类:subject类和observer类。假设我们使用“push model update"(类似于用链表实现栈),那么我们需要对该subject添加三个方法:Attach(),Detach(),Notifiy(),对于observer则需要添加Update()方法

  To use an observer, all interested observers register with the subject by using the Attach()method. As changes occur to the subject, the subject then calls each registered observer by using the Notify()method.

  所有对事件感兴趣的observer(subscriber),通过subject的Attach()方法注册到subscriber list中。当事件发生时,subject通过Notify()方法调用注册在链表上的observer。

Publish/Subscribe的优势:

1、降低耦合度:Publisher不知到subcribers的具体信息
2、增加安全性:通信机制确保 发布的消息 只被送到注册的 订阅者

Publish/Subcribe的缺点:

1、降低性能:如果一个subject有多个直接或间接的observer,通知过程将会占用相当一部分时间
2、对程序员提出了高的要求:识别出两个类。

看下内核中的notification mechanism(通知链)是如何实现的:

 通知链的运作机制包括两个角色:
  1、被通知者:对某一事件感兴趣的一方。定义了当事件发生时,相应的处理函数,即回调函数。但需要事先将其注册到通知链中(被通知者注册的动作就是在通知链中增加一项)。
  2、通知者:事件的通知者。当检测到某个事件时,或者本身产生事件时,通知所有对该事件感兴趣的一方事件发生。它定义了一个通知链,其中保存了每一个被通知者对事件的处理函数(回调函数)。所谓“通知”就是遍历通知链中的每一项,然后调用相应的事件处理函数。
 
  被通知者调用notifier_chain_register函数注册回调函数,该函数按照优先级将回调函数加入到通知链中。
  注销回调 函数则使用notifier_chain_unregister函数,即将回调函数从通知链中删除。
  通知者调用notifier_call_chain函数通知事件的到达,这个函数会遍历通知链中所有的元素,然后依次调用每一个回调函数(即完成通知动作)。

  通知链技术可以概括为:事件的被通知者将事件发生时应该执行的操作通过函数指针方式保存在链表(通知链)中,然后当事件发生时通知者依次执行链表中每一个元素的回调函数完成通知。

通知链节点的数据类型:

struct notifier_block {                                                                                            
    int (*notifier_call)(struct notifier_block *, unsigned long, void *);
    struct notifier_block *next;
    int priority;
};
priority为优先级,对于notification,priority的数值越大意味着优先级越高,在同一链表中会先被调用,当具有相同优先级的节点被插入时,后插入的放在前面(先被调用,
push model)。


从节点中的函数原型可知,返回为int类型,可能的返回值及意义:
NOTIFY_DONE             0X0000    对该事件不感兴趣

NOTIFY_OK               0X0001    成功响应该事件

NOTIFY_STOP_MASK        0X8000    该回调函数返回后停止处理后续notifier block

NOTIFY_BAD              (NOFITY_STOP_MASK|0X0002)        出错,回调函数返回后停止处理后续notifier block

NOTIFY_STOP             (NOTIFY_OK|NOFITY_STOP_MASK)     成功响应事件,回调函数返回后停止处理后续notifier block           


关于节点中函数原型作用域的unsigned long类型变量的解释:
unsigned long型参数表表示发生的事件类型,因为一个chain可能支持多个事件,此参数用来对事件进行区分。(此处可以看出notification chain与List-Based Pub-Sub
有稍微的不同,因为此处支持多个事件,相当于第一个链接中所谈的topic处理方法

原子通知链:通知链元素的回调函数在中断上下文运行,不允许阻塞

struct atomic_notifier_head {
    spinlock_t lock;
    struct notifier_block *head;
};

可阻塞通知链:通知链元素的回调函数在进程上下文运行,允许阻塞:

struct blocking_notifier_head {
    struct rw_semaphore rwsem;
    struct notifier_block *head;
};

SRCU通知链:可阻塞通知链的一种变体:

struct srcu_notifier_head {
    struct mutex mutex;
    struct srcu_struct srcu;
    struct notifier_block *head;
};

原始通知链:对通知链没有任何限制,所有保护机制都有调用者维护:

struct raw_notifier_head {
    struct notifier_block *head;
};