手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux+page+cache+里的几个函数的源码分析

[日期:2012-01-10] 来源:Linux社区  作者:raintungli [字体:大 中 小]

page 的flags 参数, 在page 的结构体里定义了flags参数,用bit位来标识page的状态,定义在page-flags.h文件里

这是在32位机 和 64位 系统的关于flags 定义

 32 bit  -------------------------------| FIELDS |       FLAGS         |
 64 bit  |           FIELDS             | ??????         FLAGS         |
            63                                  32                              0

从bit0-bit19是常用的,其他位保留给了mapping zone, node and SPARSEMEM 

  1. #define PG_locked        0  /* Page is locked. Don't touch. */   
  2. #define PG_error         1   
  3. #define PG_referenced        2   
  4. #define PG_uptodate      3   
  5.   
  6. #define PG_dirty         4   
  7. #define PG_lru           5   
  8. #define PG_active        6   
  9. #define PG_slab          7  /* slab debug (Suparna wants this) */   
  10.   
  11. #define PG_checked       8  /* kill me in 2.5.<early>. */   
  12. #define PG_arch_1        9   
  13. #define PG_reserved     10   
  14. #define PG_private      11  /* Has something at ->private */   
  15.   
  16. #define PG_writeback        12  /* Page is under writeback */   
  17. #define PG_nosave       13  /* Used for system suspend/resume */   
  18. #define PG_compound     14  /* Part of a compound page */   
  19. #define PG_swapcache        15  /* Swap page: swp_entry_t in private */   
  20.   
  21. #define PG_mappedtodisk     16  /* Has blocks allocated on-disk */   
  22. #define PG_reclaim      17  /* To be reclaimed asap */   
  23. #define PG_nosave_free      18  /* Free, should not be written */   
  24. #define PG_buddy        19  /* Page is free, on buddy lists */  

SetPageUptodate 原子设置bit PG_uptodate 状态为1,表示改页被更新

#define SetPageUptodate(page) set_bit(PG_uptodate, &(page)->flags)

 

ClearPageUptodate 原子设置bit PG_uptodate 状态为0,表示页没有被更新

#define ClearPageUptodate(page) clear_bit(PG_uptodate, &(page)->flags)

TestSetPageLocked 设置原子设置page locked状态,并返回改变前的原来状态

 
  1. #define TestSetPageLocked(page)     \  
  2.         test_and_set_bit(PG_locked, &(page)->flags)  

__lock_page 函数

  1. void fastcall __lock_page(struct page *page)  
  2. {  
  3.     DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);  
  4.   
  5.     __wait_on_bit_lock(page_waitqueue(page), &wait, sync_page,  
  6.                             TASK_UNINTERRUPTIBLE);  
  7. }  
  8. EXPORT_SYMBOL(__lock_page);  

将当前进程设置成Task_uninterruptible状态,并将进程挂载到 wait对队列中,如果PG_Locked的状态为1时,触发sync_page的方法,只有在sync_page方法中才会调用schedule()调度当前进程,直到PG_locked的状态为0,注意当执行完__wait_on_bit_lock  的时候PG_locked仍然是1,因为__wait_on_bit_lock是用test_and_set_bit来进行while条件判断的,最后将进程设置成 TASK_RUNNING 状态,把该进程从wait 队列中移除。 

unlock_page 函数

  1. void fastcall unlock_page(struct page *page)  
  2. {  
  3.     smp_mb__before_clear_bit();  
  4.     if (!TestClearPageLocked(page))  
  5.         BUG();  
  6.     smp_mb__after_clear_bit();   
  7.     wake_up_page(page, PG_locked);  
  8. }  
  9. EXPORT_SYMBOL(unlock_page);  

设置PG_Locked 的状态是0,遍历等待队列,执行唤醒函数

  1. static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,  
  2.                  int nr_exclusive, int sync, void *key)  
  3. {  
  4.     struct list_head *tmp, *next;  
  5.   
  6.     list_for_each_safe(tmp, next, &q->task_list) {  
  7.         wait_queue_t *curr = list_entry(tmp, wait_queue_t, task_list);  
  8.         unsigned flags = curr->flags;  
  9.   
  10.         if (curr->func(curr, mode, sync, key) &&  
  11.                 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)  
  12.             break;  
  13.     }  
  14. }  

其中func的定义是    

  1. .func       = autoremove_wake_function,  

在autoremove_wake_function里,调用sched.c 的default_wake_function -> try_to_wake_up

将等待队列里的线程状态置为 TASK_RUNNING 并放置到运行队列中去

linux
相关资讯       Linux基础教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款