ºìÁªLinuxÃÅ»§
Linux°ïÖú

¸ßÊÖ½ø½×±Ø¶Á£ºLinuxÄں˵Äͬ²½»úÖÆ

·¢²¼Ê±¼ä:2005-09-14 15:11:50À´Ô´:ºìÁª×÷Õß:uu
±¾ÎÄÏêϸµÄ½éÉÜÁËLinuxÄÚºËÖеÄͬ²½»úÖÆ£ºÔ­×Ó²Ù×÷¡¢ÐźÅÁ¿¡¢¶ÁдÐźÅÁ¿ºÍ×ÔÐýËøµÄAPI£¬Ê¹ÓÃÒªÇóÒÔ¼°Ò»Ð©µäÐÍʾÀý

Ò»¡¢ÒýÑÔ

ÔÚÏÖ´ú²Ù×÷ϵͳÀͬһʱ¼ä¿ÉÄÜÓжà¸öÄÚºËÖ´ÐÐÁ÷ÔÚÖ´ÐУ¬Òò´ËÄÚºËÆäʵÏó¶à½ø³Ì¶àÏ̱߳à³ÌÒ»ÑùÒ²ÐèҪһЩͬ²½»úÖÆÀ´Í¬²½¸÷Ö´Ðе¥Ôª¶Ô¹²ÏíÊý¾ÝµÄ·ÃÎÊ¡£ÓÈÆäÊÇÔڶദÀíÆ÷ϵͳÉÏ£¬¸üÐèҪһЩͬ²½»úÖÆÀ´Í¬²½²»Í¬´¦ÀíÆ÷ÉϵÄÖ´Ðе¥Ôª¶Ô¹²ÏíµÄÊý¾ÝµÄ·ÃÎÊ¡£

ÔÚÖ÷Á÷µÄLinuxÄÚºËÖаüº¬Á˼¸ºõËùÓÐÏÖ´úµÄ²Ù×÷ϵͳ¾ßÓеÄͬ²½»úÖÆ£¬ÕâЩͬ²½»úÖÆ°üÀ¨£ºÔ­×Ó²Ù×÷¡¢ÐźÅÁ¿£¨semaphore£©¡¢¶ÁдÐźÅÁ¿£¨rw_semaphore£©¡¢spinlock¡¢BKL(Big Kernel Lock)¡¢rwlock¡¢brlock£¨Ö»°üº¬ÔÚ2.4ÄÚºËÖУ©¡¢RCU£¨Ö»°üº¬ÔÚ2.6ÄÚºËÖУ©ºÍseqlock£¨Ö»°üº¬ÔÚ2.6ÄÚºËÖУ©¡£

¶þ¡¢Ô­×Ó²Ù×÷

Ëùνԭ×Ó²Ù×÷£¬¾ÍÊǸòÙ×÷¾ø²»»áÔÚÖ´ÐÐÍê±ÏÇ°±»ÈκÎÆäËûÈÎÎñ»òʼþ´ò¶Ï£¬Ò²¾Í˵£¬ËüµÄ×îСµÄÖ´Ðе¥Î»£¬²»¿ÉÄÜÓбÈËü¸üСµÄÖ´Ðе¥Î»£¬Òò´ËÕâÀïµÄÔ­×Óʵ¼ÊÊÇʹÓÃÁËÎïÀíѧÀïµÄÎïÖÊ΢Á£µÄ¸ÅÄî¡£

Ô­×Ó²Ù×÷ÐèÒªÓ²¼þµÄÖ§³Ö£¬Òò´ËÊǼܹ¹Ïà¹ØµÄ£¬ÆäAPIºÍÔ­×ÓÀàÐ͵Ķ¨Ò嶼¶¨ÒåÔÚÄÚºËÔ´ÂëÊ÷µÄinclude/asm/atomic.hÎļþÖУ¬ËüÃǶ¼Ê¹Óûã±àÓïÑÔʵÏÖ£¬ÒòΪCÓïÑÔ²¢²»ÄÜʵÏÖÕâÑùµÄ²Ù×÷¡£

Ô­×Ó²Ù×÷Ö÷ÒªÓÃÓÚʵÏÖ×ÊÔ´¼ÆÊý£¬ºÜ¶àÒýÓüÆÊý(refcnt)¾ÍÊÇͨ¹ýÔ­×Ó²Ù×÷ʵÏֵġ£Ô­×ÓÀàÐͶ¨ÒåÈçÏ£º


typedef struct
{
volatile int counter;
}
atomic_t;


volatileÐÞÊÎ×ֶθæËßgcc²»Òª¶Ô¸ÃÀàÐ͵ÄÊý¾Ý×öÓÅ»¯´¦Àí£¬¶ÔËüµÄ·ÃÎʶ¼ÊǶÔÄÚ´æµÄ·ÃÎÊ£¬¶ø²»ÊǶԼĴæÆ÷µÄ·ÃÎÊ¡£

Ô­×Ó²Ù×÷API°üÀ¨£º


atomic_read(atomic_t * v);


¸Ãº¯Êý¶ÔÔ­×ÓÀàÐ͵ıäÁ¿½øÐÐÔ­×Ó¶Á²Ù×÷£¬Ëü·µ»ØÔ­×ÓÀàÐ͵ıäÁ¿vµÄÖµ¡£


atomic_set(atomic_t * v, int i);


¸Ãº¯ÊýÉèÖÃÔ­×ÓÀàÐ͵ıäÁ¿vµÄֵΪi¡£


void atomic_add(int i, atomic_t *v);


¸Ãº¯Êý¸øÔ­×ÓÀàÐ͵ıäÁ¿vÔö¼ÓÖµi¡£


atomic_sub(int i, atomic_t *v);


¸Ãº¯Êý´ÓÔ­×ÓÀàÐ͵ıäÁ¿vÖмõÈ¥i¡£


int atomic_sub_and_test(int i, atomic_t *v);


¸Ãº¯Êý´ÓÔ­×ÓÀàÐ͵ıäÁ¿vÖмõÈ¥i£¬²¢ÅжϽá¹ûÊÇ·ñΪ0£¬Èç¹ûΪ0£¬·µ»ØÕ棬·ñÔò·µ»Ø¼Ù¡£


void atomic_inc(atomic_t *v);


¸Ãº¯Êý¶ÔÔ­×ÓÀàÐͱäÁ¿vÔ­×ÓµØÔö¼Ó1¡£


void atomic_dec(atomic_t *v);


¸Ãº¯Êý¶ÔÔ­×ÓÀàÐ͵ıäÁ¿vÔ­×ӵؼõ1¡£


int atomic_dec_and_test(atomic_t *v);


¸Ãº¯Êý¶ÔÔ­×ÓÀàÐ͵ıäÁ¿vÔ­×ӵؼõ1£¬²¢ÅжϽá¹ûÊÇ·ñΪ0£¬Èç¹ûΪ0£¬·µ»ØÕ棬·ñÔò·µ»Ø¼Ù¡£


int atomic_inc_and_test(atomic_t *v);


¸Ãº¯Êý¶ÔÔ­×ÓÀàÐ͵ıäÁ¿vÔ­×ÓµØÔö¼Ó1£¬²¢ÅжϽá¹ûÊÇ·ñΪ0£¬Èç¹ûΪ0£¬·µ»ØÕ棬·ñÔò·µ»Ø¼Ù¡£


int atomic_add_negative(int i, atomic_t *v);


¸Ãº¯Êý¶ÔÔ­×ÓÀàÐ͵ıäÁ¿vÔ­×ÓµØÔö¼ÓI£¬²¢ÅжϽá¹ûÊÇ·ñΪ¸ºÊý£¬Èç¹ûÊÇ£¬·µ»ØÕ棬·ñÔò·µ»Ø¼Ù¡£


int atomic_add_return(int i, atomic_t *v);


¸Ãº¯Êý¶ÔÔ­×ÓÀàÐ͵ıäÁ¿vÔ­×ÓµØÔö¼Ói£¬²¢ÇÒ·µ»ØÖ¸ÏòvµÄÖ¸Õë¡£


int atomic_sub_return(int i, atomic_t *v);


¸Ãº¯Êý´ÓÔ­×ÓÀàÐ͵ıäÁ¿vÖмõÈ¥i£¬²¢ÇÒ·µ»ØÖ¸ÏòvµÄÖ¸Õë¡£


int atomic_inc_return(atomic_t * v);


¸Ãº¯Êý¶ÔÔ­×ÓÀàÐ͵ıäÁ¿vÔ­×ÓµØÔö¼Ó1²¢ÇÒ·µ»ØÖ¸ÏòvµÄÖ¸Õë¡£


int atomic_dec_return(atomic_t * v);

¸Ãº¯Êý¶ÔÔ­×ÓÀàÐ͵ıäÁ¿vÔ­×ӵؼõ1²¢ÇÒ·µ»ØÖ¸ÏòvµÄÖ¸Õë¡£

Ô­×Ó²Ù×÷ͨ³£ÓÃÓÚʵÏÖ×ÊÔ´µÄÒýÓüÆÊý£¬ÔÚTCP/IPЭÒéÕ»µÄIPËéƬ´¦ÀíÖУ¬¾ÍʹÓÃÁËÒýÓüÆÊý£¬ËéƬ¶ÓÁнṹstruct ipqÃèÊöÁËÒ»¸öIPËéƬ£¬×Ö¶Îrefcnt¾ÍÊÇÒýÓüÆÊýÆ÷£¬ËüµÄÀàÐÍΪatomic_t£¬µ±´´½¨IPËéƬʱ£¨ÔÚº¯Êýip_frag_createÖУ©£¬Ê¹ÓÃatomic_setº¯Êý°ÑËüÉèÖÃΪ1£¬µ±ÒýÓøÃIPËéƬʱ£¬¾ÍʹÓú¯Êýatomic_inc°ÑÒýÓüÆÊý¼Ó1¡£

µ±²»ÐèÒªÒýÓøÃIPËéƬʱ£¬¾ÍʹÓú¯Êýipq_putÀ´ÊͷŸÃIPËéƬ£¬ipq_putʹÓú¯Êýatomic_dec_and_test°ÑÒýÓüÆÊý¼õ1²¢ÅжÏÒýÓüÆÊýÊÇ·ñΪ0£¬Èç¹ûÊǾÍÊÍ·ÅIPËéƬ¡£º¯Êýipq_kill°ÑIPËéƬ´Óipq¶ÓÁÐÖÐɾ³ý£¬²¢°Ñ¸Ãɾ³ýµÄIPËéƬµÄÒýÓüÆÊý¼õ1£¨Í¨¹ýʹÓú¯Êýatomic_decʵÏÖ£©¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 9 ÌõÆÀÂÛ

  1. dzjxj1 ÓÚ 2006-07-22 12:01:21·¢±í:

    ºÃ¼«ÁË£¡¶¥¶¥

  2. redapp ÓÚ 2005-10-27 00:20:01·¢±í:

    Ö§³Ö

  3. root ÓÚ 2005-09-18 00:09:03·¢±í:

    ѧϰÁË

  4. °®µÄ±ßÔµ ÓÚ 2005-09-17 00:25:27·¢±í:

    ÊܽÌ

  5. Çç ÓÚ 2005-09-15 00:32:40·¢±í:

    ²»´í£¬Ö§³Ö

  6. uu ÓÚ 2005-09-14 15:13:42·¢±í:

    Òò´ËÈç¹ûÄܹ»Á¢¼´»ñµÃËø£¬ËüµÈͬÓÚspin_lock_irqsave£¬Èç¹û²»ÄÜ»ñµÃËø£¬ËüµÈͬÓÚspin_trylock¡£Èç¹û¸Ãºê»ñµÃ×ÔÐýËølock£¬ÄÇÐèҪʹÓÃspin_unlock_irqrestoreÀ´ÊÍ·Å¡£


    spin_trylock_irq(lock)




    ¸ÃºêÀàËÆÓÚspin_trylock_irqsave£¬Ö»ÊǸú겻±£´æ±êÖ¾¼Ä´æÆ÷¡£Èç¹û¸Ãºê»ñµÃ×ÔÐýËølock£¬ÐèҪʹÓÃspin_unlock_irqÀ´ÊÍ·Å¡£


    spin_trylock_bh(lock)




    ¸ÃºêÈç¹û»ñµÃÁË×ÔÐýËø£¬ËüÒ²½«Ê§Ð§±¾µØÈíÖжϡ£Èç¹ûµÃ²»µ½Ëø£¬ËüʲôҲ²»×ö¡£Òò´Ë£¬Èç¹ûµÃµ½ÁËËø£¬ËüµÈͬÓÚspin_lock_bh£¬Èç¹ûµÃ²»µ½Ëø£¬ËüµÈͬÓÚspin_trylock¡£Èç¹û¸ÃºêµÃµ½ÁË×ÔÐýËø£¬ÐèҪʹÓÃspin_unlock_bhÀ´ÊÍ·Å¡£


    spin_can_lock(lock)




    ¸ÃºêÓÃÓÚÅжÏ×ÔÐýËølockÊÇ·ñÄܹ»±»Ëø£¬Ëüʵ¼ÊÊÇspin_is_lockedÈ¡·´¡£Èç¹ûlockûÓб»Ëø£¬Ëü·µ»ØÕ棬·ñÔò£¬·µ»Ø¼Ù¡£¸ÃºêÔÚ2.6.11ÖеÚÒ»´Î±»¶¨Ò壬ÔÚÏÈÇ°µÄÄÚºËÖв¢Ã»Óиúꡣ

    »ñµÃ×ÔÐýËøºÍÊÍ·Å×ÔÐýËøÓкü¸¸ö°æ±¾£¬Òò´ËÈöÁÕßÖªµÀÔÚʲôÑùµÄÇé¿öÏÂʹÓÃʲô°æ±¾µÄ»ñµÃºÍÊÍ·ÅËøµÄºêÊǷdz£±ØÒªµÄ¡£

    Èç¹û±»±£»¤µÄ¹²Ïí×ÊÔ´Ö»ÔÚ½ø³ÌÉÏÏÂÎÄ·ÃÎʺÍÈíÖжÏÉÏÏÂÎÄ·ÃÎÊ£¬ÄÇôµ±ÔÚ½ø³ÌÉÏÏÂÎÄ·ÃÎʹ²Ïí×ÊԴʱ£¬¿ÉÄܱ»ÈíÖжϴò¶Ï£¬´Ó¶ø¿ÉÄܽøÈëÈíÖжÏÉÏÏÂÎÄÀ´¶Ô±»±£»¤µÄ¹²Ïí×ÊÔ´·ÃÎÊ£¬Òò´Ë¶ÔÓÚÕâÖÖÇé¿ö£¬¶Ô¹²Ïí×ÊÔ´µÄ·ÃÎʱØÐëʹÓÃspin_lock_bhºÍspin_unlock_bhÀ´±£»¤¡£

    µ±È»Ê¹ÓÃspin_lock_irqºÍspin_unlock_irqÒÔ¼°spin_lock_irqsaveºÍspin_unlock_irqrestoreÒ²¿ÉÒÔ£¬ËüÃÇʧЧÁ˱¾µØÓ²Öжϣ¬Ê§Ð§Ó²ÖжÏÒþʽµØҲʧЧÁËÈíÖжϡ£µ«ÊÇʹÓÃspin_lock_bhºÍspin_unlock_bhÊÇ×îÇ¡µ±µÄ£¬Ëü±ÈÆäËûÁ½¸ö¿ì¡£

    Èç¹û±»±£»¤µÄ¹²Ïí×ÊÔ´Ö»ÔÚ½ø³ÌÉÏÏÂÎĺÍtasklet»òtimerÉÏÏÂÎÄ·ÃÎÊ£¬ÄÇôӦ¸ÃʹÓÃÓëÉÏÃæÇé¿öÏàͬµÄ»ñµÃºÍÊÍ·ÅËøµÄºê£¬ÒòΪtaskletºÍtimerÊÇÓÃÈíÖжÏʵÏֵġ£

    Èç¹û±»±£»¤µÄ¹²Ïí×ÊÔ´Ö»ÔÚÒ»¸ötasklet»òtimerÉÏÏÂÎÄ·ÃÎÊ£¬ÄÇô²»ÐèÒªÈκÎ×ÔÐýËø±£»¤£¬ÒòΪͬһ¸ötasklet»òtimerÖ»ÄÜÔÚÒ»¸öCPUÉÏÔËÐУ¬¼´Ê¹ÊÇÔÚSMP»·¾³ÏÂÒ²ÊÇÈç´Ë¡£Êµ¼ÊÉÏtaskletÔÚµ÷ÓÃtasklet_schedule±ê¼ÇÆäÐèÒª±»µ÷¶ÈʱÒѾ­°Ñ¸Ãtasklet°ó¶¨µ½µ±Ç°CPU£¬Òò´Ëͬһ¸ötasklet¾ö²»¿ÉÄÜͬʱÔÚÆäËûCPUÉÏÔËÐС£

    timerÒ²ÊÇÔÚÆ䱻ʹÓÃadd_timerÌí¼Óµ½timer¶ÓÁÐÖÐʱÒѾ­±»°ï¶¨µ½µ±Ç°CPU£¬ËùÒÔͬһ¸ötimer¾ø²»¿ÉÄÜÔËÐÐÔÚÆäËûCPUÉÏ¡£µ±È»Í¬Ò»¸ötaskletÓÐÁ½¸öʵÀýͬʱÔËÐÐÔÚͬһ¸öCPU¾Í¸ü²»¿ÉÄÜÁË¡£

    Èç¹û±»±£»¤µÄ¹²Ïí×ÊÔ´Ö»ÔÚÁ½¸ö»ò¶à¸ötasklet»òtimerÉÏÏÂÎÄ·ÃÎÊ£¬ÄÇô¶Ô¹²Ïí×ÊÔ´µÄ·ÃÎʽöÐèÒªÓÃspin_lockºÍspin_unlockÀ´±£»¤£¬²»±ØʹÓÃ_bh°æ±¾£¬ÒòΪµ±tasklet»òtimerÔËÐÐʱ£¬²»¿ÉÄÜÓÐÆäËûtasklet»òtimerÔÚµ±Ç°CPUÉÏÔËÐС£

    Èç¹û±»±£»¤µÄ¹²Ïí×ÊÔ´Ö»ÔÚÒ»¸öÈíÖжϣ¨taskletºÍtimer³ýÍ⣩ÉÏÏÂÎÄ·ÃÎÊ£¬ÄÇôÕâ¸ö¹²Ïí×ÊÔ´ÐèÒªÓÃspin_lockºÍspin_unlockÀ´±£»¤£¬ÒòΪͬÑùµÄÈíÖжϿÉÒÔͬʱÔÚ²»Í¬µÄCPUÉÏÔËÐС£

    Èç¹û±»±£»¤µÄ¹²Ïí×ÊÔ´ÔÚÁ½¸ö»ò¶à¸öÈíÖжÏÉÏÏÂÎÄ·ÃÎÊ£¬ÄÇôÕâ¸ö¹²Ïí×ÊÔ´µ±È»¸üÐèÒªÓÃspin_lockºÍspin_unlockÀ´±£»¤£¬²»Í¬µÄÈíÖжÏÄܹ»Í¬Ê±ÔÚ²»Í¬µÄCPUÉÏÔËÐС£

    Èç¹û±»±£»¤µÄ¹²Ïí×ÊÔ´ÔÚÈíÖжϣ¨°üÀ¨taskletºÍtimer£©»ò½ø³ÌÉÏÏÂÎĺÍÓ²ÖжÏÉÏÏÂÎÄ·ÃÎÊ£¬ÄÇôÔÚÈíÖжϻò½ø³ÌÉÏÏÂÎÄ·ÃÎÊÆڼ䣬¿ÉÄܱ»Ó²Öжϴò¶Ï£¬´Ó¶ø½øÈëÓ²ÖжÏÉÏÏÂÎĶԹ²Ïí×ÊÔ´½øÐзÃÎÊ£¬Òò´Ë£¬ÔÚ½ø³Ì»òÈíÖжÏÉÏÏÂÎÄÐèҪʹÓÃspin_lock_irqºÍspin_unlock_irqÀ´±£»¤¶Ô¹²Ïí×ÊÔ´µÄ·ÃÎÊ¡£

    ¶øÔÚÖжϴ¦Àí¾ä±úÖÐʹÓÃʲô°æ±¾£¬ÐèÒÀÇé¿ö¶ø¶¨£¬Èç¹ûÖ»ÓÐÒ»¸öÖжϴ¦Àí¾ä±ú·ÃÎʸù²Ïí×ÊÔ´£¬ÄÇôÔÚÖжϴ¦Àí¾ä±úÖнöÐèÒªspin_lockºÍspin_unlockÀ´±£»¤¶Ô¹²Ïí×ÊÔ´µÄ·ÃÎʾͿÉÒÔÁË¡£

    ÒòΪÔÚÖ´ÐÐÖжϴ¦Àí¾ä±úÆڼ䣬²»¿ÉÄܱ»Í¬Ò»CPUÉϵÄÈíÖжϻò½ø³Ì´ò¶Ï¡£µ«ÊÇÈç¹ûÓв»Í¬µÄÖжϴ¦Àí¾ä±ú·ÃÎʸù²Ïí×ÊÔ´£¬ÄÇôÐèÒªÔÚÖжϴ¦Àí¾ä±úÖÐʹÓÃspin_lock_irqºÍspin_unlock_irqÀ´±£»¤¶Ô¹²Ïí×ÊÔ´µÄ·ÃÎÊ¡£

    ÔÚʹÓÃspin_lock_irqºÍspin_unlock_irqµÄÇé¿öÏ£¬ÍêÈ«¿ÉÒÔÓÃspin_lock_irqsaveºÍspin_unlock_irqrestoreÈ¡´ú£¬ÄǾßÌåÓ¦¸ÃʹÓÃÄÄÒ»¸öÒ²ÐèÒªÒÀÇé¿ö¶ø¶¨£¬Èç¹û¿ÉÒÔÈ·ÐÅÔÚ¶Ô¹²Ïí×ÊÔ´·ÃÎÊÇ°ÖжÏÊÇʹÄܵģ¬ÄÇôʹÓÃspin_lock_irq¸üºÃһЩ¡£

    ÒòΪËü±Èspin_lock_irqsaveÒª¿ìһЩ£¬µ«ÊÇÈç¹ûÄã²»ÄÜÈ·¶¨ÊÇ·ñÖжÏʹÄÜ£¬ÄÇôʹÓÃspin_lock_irqsaveºÍspin_unlock_irqrestore¸üºÃ£¬ÒòΪËü½«»Ö¸´·ÃÎʹ²Ïí×ÊÔ´Ç°µÄÖжϱêÖ¾¶ø²»ÊÇÖ±½ÓʹÄÜÖжϡ£

    µ±È»£¬ÓÐЩÇé¿öÏÂÐèÒªÔÚ·ÃÎʹ²Ïí×ÊԴʱ±ØÐëÖжÏʧЧ£¬¶ø·ÃÎÊÍêºó±ØÐëÖжÏʹÄÜ£¬ÕâÑùµÄÇéÐÎʹÓÃspin_lock_irqºÍspin_unlock_irq×îºÃ¡£

    ÐèÒªÌرðÌáÐѶÁÕߣ¬spin_lockÓÃÓÚ×èÖ¹ÔÚ²»Í¬CPUÉϵÄÖ´Ðе¥Ôª¶Ô¹²Ïí×ÊÔ´µÄͬʱ·ÃÎÊÒÔ¼°²»Í¬½ø³ÌÉÏÏÂÎÄ»¥ÏàÇÀÕ¼µ¼ÖµĶԹ²Ïí×ÊÔ´µÄ·Çͬ²½·ÃÎÊ£¬¶øÖжÏʧЧºÍÈíÖжÏʧЧȴÊÇΪÁË×èÖ¹ÔÚͬһCPUÉÏÈíÖжϻòÖж϶Թ²Ïí×ÊÔ´µÄ·Çͬ²½·ÃÎÊ¡£

    ²Î¿¼×ÊÁÏ

    Kernel Locking Techniques,http://www.linuxjournal.com/article/5833

    Redhat 9.0 kernel source tree

    kernel.org 2.6.12 source tree

    Linux 2.6ÄÚºËÖÐеÄËø»úÖÆ--RCU(Read-Copy Update),

    http://www.ibm.com/developerworks/cn/linux/l-rcu/

    Unreliable Guide To Locking.

  7. uu ÓÚ 2005-09-14 15:13:22·¢±í:

    Îå¡¢×ÔÐýËø£¨spinlock£©

    ×ÔÐýËøÓ뻥³âËøÓеãÀàËÆ£¬Ö»ÊÇ×ÔÐýËø²»»áÒýÆðµ÷ÓÃÕß˯Ãߣ¬Èç¹û×ÔÐýËøÒѾ­±»±ðµÄÖ´Ðе¥Ôª±£³Ö£¬µ÷ÓÃÕß¾Íһֱѭ»·ÔÚÄÇÀï¿´ÊÇ·ñ¸Ã×ÔÐýËøµÄ±£³ÖÕßÒѾ­ÊÍ·ÅÁËËø£¬"×ÔÐý"Ò»´Ê¾ÍÊÇÒò´Ë¶øµÃÃû¡£

    ÓÉÓÚ×ÔÐýËøʹÓÃÕßÒ»°ã±£³ÖËøʱ¼ä·Ç³£¶Ì£¬Òò´ËÑ¡Ôñ×ÔÐý¶ø²»ÊÇ˯ÃßÊǷdz£±ØÒªµÄ£¬×ÔÐýËøµÄЧÂÊÔ¶¸ßÓÚ»¥³âËø¡£

    ÐźÅÁ¿ºÍ¶ÁдÐźÅÁ¿ÊʺÏÓÚ±£³Öʱ¼ä½Ï³¤µÄÇé¿ö£¬ËüÃǻᵼÖµ÷ÓÃÕß˯Ãߣ¬Òò´ËÖ»ÄÜÔÚ½ø³ÌÉÏÏÂÎÄʹÓã¨_trylockµÄ±äÖÖÄܹ»ÔÚÖжÏÉÏÏÂÎÄʹÓã©£¬¶ø×ÔÐýËøÊʺÏÓÚ±£³Öʱ¼ä·Ç³£¶ÌµÄÇé¿ö£¬Ëü¿ÉÒÔÔÚÈκÎÉÏÏÂÎÄʹÓá£

    Èç¹û±»±£»¤µÄ¹²Ïí×ÊÔ´Ö»ÔÚ½ø³ÌÉÏÏÂÎÄ·ÃÎÊ£¬Ê¹ÓÃÐźÅÁ¿±£»¤¸Ã¹²Ïí×ÊÔ´·Ç³£ºÏÊÊ£¬Èç¹û¶Ô¹²Ïï×ÊÔ´µÄ·ÃÎÊʱ¼ä·Ç³£¶Ì£¬×ÔÐýËøÒ²¿ÉÒÔ¡£µ«ÊÇÈç¹û±»±£»¤µÄ¹²Ïí×ÊÔ´ÐèÒªÔÚÖжÏÉÏÏÂÎÄ·ÃÎÊ£¨°üÀ¨µ×°ë²¿¼´Öжϴ¦Àí¾ä±úºÍ¶¥°ë²¿¼´ÈíÖжϣ©£¬¾Í±ØÐëʹÓÃ×ÔÐýËø¡£

    ×ÔÐýËø±£³ÖÆÚ¼äÊÇÇÀռʧЧµÄ£¬¶øÐźÅÁ¿ºÍ¶ÁдÐźÅÁ¿±£³ÖÆÚ¼äÊÇ¿ÉÒÔ±»ÇÀÕ¼µÄ¡£×ÔÐýËøÖ»ÓÐÔÚÄں˿ÉÇÀÕ¼»òSMPµÄÇé¿öϲÅÕæÕýÐèÒª£¬ÔÚµ¥CPUÇÒ²»¿ÉÇÀÕ¼µÄÄÚºËÏ£¬×ÔÐýËøµÄËùÓвÙ×÷¶¼ÊÇ¿Õ²Ù×÷¡£

    ¸ú»¥³âËøÒ»Ñù£¬Ò»¸öÖ´Ðе¥ÔªÒªÏë·ÃÎʱ»×ÔÐýËø±£»¤µÄ¹²Ïí×ÊÔ´£¬±ØÐëÏȵõ½Ëø£¬ÔÚ·ÃÎÊÍê¹²Ïí×ÊÔ´ºó£¬±ØÐëÊÍ·ÅËø¡£Èç¹ûÔÚ»ñÈ¡×ÔÐýËøʱ£¬Ã»ÓÐÈκÎÖ´Ðе¥Ôª±£³Ö¸ÃËø£¬ÄÇô½«Á¢¼´µÃµ½Ëø£»Èç¹ûÔÚ»ñÈ¡×ÔÐýËøʱËøÒѾ­Óб£³ÖÕߣ¬ÄÇô»ñÈ¡Ëø²Ù×÷½«×ÔÐýÔÚÄÇÀֱµ½¸Ã×ÔÐýËøµÄ±£³ÖÕßÊÍ·ÅÁËËø¡£

    ÎÞÂÛÊÇ»¥³âËø£¬»¹ÊÇ×ÔÐýËø£¬ÔÚÈκÎʱ¿Ì£¬×î¶àÖ»ÄÜÓÐÒ»¸ö±£³ÖÕߣ¬Ò²¾Í˵£¬ÔÚÈκÎʱ¿Ì×î¶àÖ»ÄÜÓÐÒ»¸öÖ´Ðе¥Ôª»ñµÃËø¡£

    ×ÔÐýËøµÄAPIÓУº


    spin_lock_init(x)




    ¸ÃºêÓÃÓÚ³õʼ»¯×ÔÐýËøx¡£×ÔÐýËøÔÚÕæÕýʹÓÃÇ°±ØÐëÏȳõʼ»¯¡£¸ÃºêÓÃÓÚ¶¯Ì¬³õʼ»¯¡£


    DEFINE_SPINLOCK(x)




    ¸ÃºêÉùÃ÷Ò»¸ö×ÔÐýËøx²¢³õʼ»¯Ëü¡£¸ÃºêÔÚ2.6.11ÖеÚÒ»´Î±»¶¨Ò壬ÔÚÏÈÇ°µÄÄÚºËÖв¢Ã»Óиúꡣ


    SPIN_LOCK_UNLOCKED




    ¸ÃºêÓÃÓÚ¾²Ì¬³õʼ»¯Ò»¸ö×ÔÐýËø¡£


    DEFINE_SPINLOCK(x)µÈͬÓÚspinlock_t x = SPIN_LOCK_UNLOCKED
    spin_is_locked(x)




    ¸ÃºêÓÃÓÚÅжÏ×ÔÐýËøxÊÇ·ñÒѾ­±»Ä³Ö´Ðе¥Ôª±£³Ö£¨¼´±»Ëø£©£¬Èç¹ûÊÇ£¬·µ»ØÕ棬·ñÔò·µ»Ø¼Ù¡£


    spin_unlock_wait(x)




    ¸ÃºêÓÃÓڵȴý×ÔÐýËøx±äµÃûÓб»ÈκÎÖ´Ðе¥Ôª±£³Ö£¬Èç¹ûûÓÐÈκÎÖ´Ðе¥Ôª±£³Ö¸Ã×ÔÐýËø£¬¸ÃºêÁ¢¼´·µ»Ø£¬·ñÔò½«Ñ­»·ÔÚÄÇÀֱµ½¸Ã×ÔÐýËø±»±£³ÖÕßÊÍ·Å¡£


    spin_trylock(lock)




    ¸Ãºê¾¡Á¦»ñµÃ×ÔÐýËølock£¬Èç¹ûÄÜÁ¢¼´»ñµÃËø£¬Ëü»ñµÃËø²¢·µ»ØÕ棬·ñÔò²»ÄÜÁ¢¼´»ñµÃËø£¬Á¢¼´·µ»Ø¼Ù¡£Ëü²»»á×ÔÐýµÈ´ýlock±»ÊÍ·Å¡£


    spin_lock(lock)




    ¸ÃºêÓÃÓÚ»ñµÃ×ÔÐýËølock£¬Èç¹ûÄܹ»Á¢¼´»ñµÃËø£¬Ëü¾ÍÂíÉÏ·µ»Ø£¬·ñÔò£¬Ëü½«×ÔÐýÔÚÄÇÀֱµ½¸Ã×ÔÐýËøµÄ±£³ÖÕßÊÍ·Å£¬Õâʱ£¬Ëü»ñµÃËø²¢·µ»Ø¡£×ÜÖ®£¬Ö»ÓÐËü»ñµÃËø²Å·µ»Ø¡£


    spin_lock_irqsave(lock, flags)




    ¸Ãºê»ñµÃ×ÔÐýËøµÄͬʱ°Ñ±êÖ¾¼Ä´æÆ÷µÄÖµ±£´æµ½±äÁ¿flagsÖв¢Ê§Ð§±¾µØÖжϡ£


    spin_lock_irq(lock)




    ¸ÃºêÀàËÆÓÚspin_lock_irqsave£¬Ö»ÊǸú겻±£´æ±êÖ¾¼Ä´æÆ÷µÄÖµ¡£


    spin_lock_bh(lock)




    ¸ÃºêÔڵõ½×ÔÐýËøµÄͬʱʧЧ±¾µØÈíÖжϡ£


    spin_unlock(lock)




    ¸ÃºêÊÍ·Å×ÔÐýËølock£¬ËüÓëspin_trylock»òspin_lockÅä¶ÔʹÓá£Èç¹ûspin_trylock·µ»Ø¼Ù£¬±íÃ÷ûÓлñµÃ×ÔÐýËø£¬Òò´Ë²»±ØʹÓÃspin_unlockÊÍ·Å¡£


    spin_unlock_irqrestore(lock, flags)




    ¸ÃºêÊÍ·Å×ÔÐýËølockµÄͬʱ£¬Ò²»Ö¸´±êÖ¾¼Ä´æÆ÷µÄֵΪ±äÁ¿flags±£´æµÄÖµ¡£ËüÓëspin_lock_irqsaveÅä¶ÔʹÓá£


    spin_unlock_irq(lock)




    ¸ÃºêÊÍ·Å×ÔÐýËølockµÄͬʱ£¬Ò²Ê¹Äܱ¾µØÖжϡ£ËüÓëspin_lock_irqÅä¶ÔÓ¦Óá£


    spin_unlock_bh(lock)




    ¸ÃºêÊÍ·Å×ÔÐýËølockµÄͬʱ£¬Ò²Ê¹Äܱ¾µØµÄÈíÖжϡ£ËüÓëspin_lock_bhÅä¶ÔʹÓá£


    spin_trylock_irqsave(lock, flags)


    ¸ÃºêÈç¹û»ñµÃ×ÔÐýËølock£¬ËüÒ²½«±£´æ±êÖ¾¼Ä´æÆ÷µÄÖµµ½±äÁ¿flagsÖУ¬²¢ÇÒʧЧ±¾µØÖжϣ¬Èç¹ûûÓлñµÃËø£¬ËüʲôҲ²»×ö¡£

  8. uu ÓÚ 2005-09-14 15:12:47·¢±í:

    ËÄ¡¢¶ÁдÐźÅÁ¿£¨rw_semaphore£©

    ¶ÁдÐźÅÁ¿¶Ô·ÃÎÊÕß½øÐÐÁËϸ·Ö£¬»òÕßΪ¶ÁÕߣ¬»òÕßΪдÕߣ¬¶ÁÕßÔÚ±£³Ö¶ÁдÐźÅÁ¿ÆÚ¼äÖ»ÄܶԸöÁдÐźÅÁ¿±£»¤µÄ¹²Ïí×ÊÔ´½øÐжÁ·ÃÎÊ£¬Èç¹ûÒ»¸öÈÎÎñ³ýÁËÐèÒª¶Á£¬¿ÉÄÜ»¹ÐèҪд£¬ÄÇôËü±ØÐë±»¹éÀàΪдÕߣ¬ËüÔÚ¶Ô¹²Ïí×ÊÔ´·ÃÎÊ֮ǰ±ØÐëÏÈ»ñµÃдÕßÉí·Ý£¬Ð´ÕßÔÚ·¢ÏÖ×Ô¼º²»ÐèҪд·ÃÎʵÄÇé¿öÏ¿ÉÒÔ½µ¼¶Îª¶ÁÕß¡£¶ÁдÐźÅÁ¿Í¬Ê±ÓµÓеĶÁÕßÊý²»ÊÜÏÞÖÆ£¬Ò²¾Í˵¿ÉÒÔÓÐÈÎÒâ¶à¸ö¶ÁÕßͬʱӵÓÐÒ»¸ö¶ÁдÐźÅÁ¿¡£

    Èç¹ûÒ»¸ö¶ÁдÐźÅÁ¿µ±Ç°Ã»Óб»Ð´ÕßÓµÓв¢ÇÒҲûÓÐдÕߵȴý¶ÁÕßÊÍ·ÅÐźÅÁ¿£¬ÄÇôÈκζÁÕ߶¼¿ÉÒԳɹ¦»ñµÃ¸Ã¶ÁдÐźÅÁ¿£»·ñÔò£¬¶ÁÕß±ØÐë±»¹ÒÆðÖ±µ½Ð´ÕßÊͷŸÃÐźÅÁ¿¡£Èç¹ûÒ»¸ö¶ÁдÐźÅÁ¿µ±Ç°Ã»ÓỶÁÕß»òдÕßÓµÓв¢ÇÒҲûÓÐдÕߵȴý¸ÃÐźÅÁ¿£¬ÄÇôһ¸öдÕß¿ÉÒԳɹ¦»ñµÃ¸Ã¶ÁдÐźÅÁ¿£¬·ñÔòдÕß½«±»¹ÒÆð£¬Ö±µ½Ã»ÓÐÈκηÃÎÊÕß¡£Òò´Ë£¬Ð´ÕßÊÇÅÅËûÐԵģ¬¶ÀÕ¼ÐԵġ£

    ¶ÁдÐźÅÁ¿ÓÐÁ½ÖÖʵÏÖ£¬Ò»ÖÖÊÇͨÓõģ¬²»ÒÀÀµÓÚÓ²¼þ¼Ü¹¹£¬Òò´Ë£¬Ôö¼Óеļܹ¹²»ÐèÒªÖØÐÂʵÏÖËü£¬µ«È±µãÊÇÐÔÄܵͣ¬»ñµÃºÍÊͷŶÁдÐźÅÁ¿µÄ¿ªÏú´ó£»ÁíÒ»ÖÖÊǼܹ¹Ïà¹ØµÄ£¬Òò´ËÐÔÄܸߣ¬»ñÈ¡ºÍÊͷŶÁдÐźÅÁ¿µÄ¿ªÏúС£¬µ«Ôö¼Óеļܹ¹ÐèÒªÖØÐÂʵÏÖ¡£ÔÚÄÚºËÅäÖÃʱ£¬¿ÉÒÔͨ¹ýÑ¡ÏîÈ¥¿ØÖÆʹÓÃÄÄÒ»ÖÖʵÏÖ¡£

    ¶ÁдÐźÅÁ¿µÄÏà¹ØAPIÓУº


    DECLARE_RWSEM(name)




    ¸ÃºêÉùÃ÷Ò»¸ö¶ÁдÐźÅÁ¿name²¢¶ÔÆä½øÐгõʼ»¯¡£


    void init_rwsem(struct rw_semaphore *sem);




    ¸Ãº¯Êý¶Ô¶ÁдÐźÅÁ¿sem½øÐгõʼ»¯¡£


    void down_read(struct rw_semaphore *sem);




    ¶ÁÕßµ÷Óøú¯ÊýÀ´µÃµ½¶ÁдÐźÅÁ¿sem¡£¸Ãº¯Êý»áµ¼Öµ÷ÓÃÕß˯Ãߣ¬Òò´ËÖ»ÄÜÔÚ½ø³ÌÉÏÏÂÎÄʹÓá£


    int down_read_trylock(struct rw_semaphore *sem);




    ¸Ãº¯ÊýÀàËÆÓÚdown_read£¬Ö»ÊÇËü²»»áµ¼Öµ÷ÓÃÕß˯Ãß¡£Ëü¾¡Á¦µÃµ½¶ÁдÐźÅÁ¿sem£¬Èç¹ûÄܹ»Á¢¼´µÃµ½£¬Ëü¾ÍµÃµ½¸Ã¶ÁдÐźÅÁ¿£¬²¢ÇÒ·µ»Ø1£¬·ñÔò±íʾ²»ÄÜÁ¢¿ÌµÃµ½¸ÃÐźÅÁ¿£¬·µ»Ø0¡£Òò´Ë£¬ËüÒ²¿ÉÒÔÔÚÖжÏÉÏÏÂÎÄʹÓá£


    void down_write(struct rw_semaphore *sem);




    дÕßʹÓøú¯ÊýÀ´µÃµ½¶ÁдÐźÅÁ¿sem£¬ËüÒ²»áµ¼Öµ÷ÓÃÕß˯Ãߣ¬Òò´ËÖ»ÄÜÔÚ½ø³ÌÉÏÏÂÎÄʹÓá£


    int down_write_trylock(struct rw_semaphore *sem);




    ¸Ãº¯ÊýÀàËÆÓÚdown_write£¬Ö»ÊÇËü²»»áµ¼Öµ÷ÓÃÕß˯Ãß¡£¸Ãº¯Êý¾¡Á¦µÃµ½¶ÁдÐźÅÁ¿£¬Èç¹ûÄܹ»Á¢¿Ì»ñµÃ£¬¾Í»ñµÃ¸Ã¶ÁдÐźÅÁ¿²¢ÇÒ·µ»Ø1£¬·ñÔò±íʾÎÞ·¨Á¢¿Ì»ñµÃ£¬·µ»Ø0¡£Ëü¿ÉÒÔÔÚÖжÏÉÏÏÂÎÄʹÓá£


    void up_read(struct rw_semaphore *sem);




    ¶ÁÕßʹÓøú¯ÊýÊͷŶÁдÐźÅÁ¿sem¡£ËüÓëdown_read»òdown_read_trylockÅä¶ÔʹÓá£Èç¹ûdown_read_trylock·µ»Ø0£¬²»ÐèÒªµ÷ÓÃup_readÀ´ÊͷŶÁдÐźÅÁ¿£¬ÒòΪ¸ù±¾¾ÍûÓлñµÃÐźÅÁ¿¡£


    void up_write(struct rw_semaphore *sem);




    дÕßµ÷Óøú¯ÊýÊÍ·ÅÐźÅÁ¿sem¡£ËüÓëdown_write»òdown_write_trylockÅä¶ÔʹÓá£Èç¹ûdown_write_trylock·µ»Ø0£¬²»ÐèÒªµ÷ÓÃup_write£¬ÒòΪ·µ»Ø0±íʾûÓлñµÃ¸Ã¶ÁдÐźÅÁ¿¡£


    void downgrade_write(struct rw_semaphore *sem);




    ¸Ãº¯ÊýÓÃÓÚ°ÑдÕß½µ¼¶Îª¶ÁÕߣ¬ÕâÓÐʱÊDZØÒªµÄ¡£ÒòΪдÕßÊÇÅÅËûÐԵģ¬Òò´ËÔÚдÕß±£³Ö¶ÁдÐźÅÁ¿Æڼ䣬ÈκζÁÕß»òдÕ߶¼½«ÎÞ·¨·ÃÎʸöÁдÐźÅÁ¿±£»¤µÄ¹²Ïí×ÊÔ´£¬¶ÔÓÚÄÇЩµ±Ç°Ìõ¼þϲ»ÐèҪд·ÃÎʵÄдÕߣ¬½µ¼¶Îª¶ÁÕß½«£¬Ê¹µÃµÈ´ý·ÃÎʵĶÁÕßÄܹ»Á¢¿Ì·ÃÎÊ£¬´Ó¶øÔö¼ÓÁ˲¢·¢ÐÔ£¬Ìá¸ßÁËЧÂÊ¡£

    ¶ÁдÐźÅÁ¿ÊÊÓÚÔÚ¶Á¶àдÉÙµÄÇé¿öÏÂʹÓã¬ÔÚlinuxÄÚºËÖжԽø³ÌµÄÄÚ´æÓ³ÏñÃèÊö½á¹¹µÄ·ÃÎʾÍʹÓÃÁ˶ÁдÐźÅÁ¿½øÐб£»¤¡£

    ÔÚLinuxÖУ¬Ã¿Ò»¸ö½ø³Ì¶¼ÓÃÒ»¸öÀàÐÍΪtask_t»òstruct task_structµÄ½á¹¹À´ÃèÊö£¬¸Ã½á¹¹µÄÀàÐÍΪstruct mm_structµÄ×Ö¶ÎmmÃèÊöÁ˽ø³ÌµÄÄÚ´æÓ³Ïñ£¬ÌرðÊÇmm_struct½á¹¹µÄmmap×Ö¶Îά»¤ÁËÕû¸ö½ø³ÌµÄÄÚ´æ¿éÁÐ±í£¬¸ÃÁÐ±í½«ÔÚ½ø³ÌÉú´æÆڼ䱻´óÁ¿µØ±éÀû»òÐ޸ġ£

    Òò´Ëmm_struct½á¹¹¾ÍÓÐÒ»¸ö×Ö¶Îmmap_semÀ´¶ÔmmapµÄ·ÃÎʽøÐб£»¤£¬mmap_sem¾ÍÊÇÒ»¸ö¶ÁдÐźÅÁ¿£¬ÔÚprocÎļþϵͳÀïÓкܶà½ø³ÌÄÚ´æʹÓÃÇé¿öµÄ½Ó¿Ú£¬Í¨¹ýËüÃÇÄܹ»²é¿´Ä³Ò»½ø³ÌµÄÄÚ´æʹÓÃÇé¿ö£¬ÃüÁîfree¡¢psºÍtop¶¼ÊÇͨ¹ýprocÀ´µÃµ½ÄÚ´æʹÓÃÐÅÏ¢µÄ£¬proc½Ó¿Ú¾ÍʹÓÃdown_readºÍup_readÀ´¶ÁÈ¡½ø³ÌµÄmmapÐÅÏ¢¡£

    µ±½ø³Ì¶¯Ì¬µØ·ÖÅä»òÊÍ·ÅÄÚ´æʱ£¬ÐèÒªÐÞ¸ÄmmapÀ´·´Ó³·ÖÅä»òÊͷźóµÄÄÚ´æÓ³Ïñ£¬Òò´Ë¶¯Ì¬ÄÚ´æ·ÖÅä»òÊͷŲÙ×÷ÐèÒªÒÔдÕßÉí·Ý»ñµÃ¶ÁдÐźÅÁ¿mmap_semÀ´¶Ômmap½øÐиüС£ÏµÍ³µ÷ÓÃbrkºÍmunmap¾ÍʹÓÃÁËdown_writeºÍup_writeÀ´±£»¤¶ÔmmapµÄ·ÃÎÊ¡£

  9. uu ÓÚ 2005-09-14 15:12:14·¢±í:

    Èý¡¢ÐźÅÁ¿£¨semaphore£©

    LinuxÄں˵ÄÐźÅÁ¿ÔÚ¸ÅÄîºÍÔ­ÀíÉÏÓëÓû§Ì¬µÄSystem VµÄIPC»úÖÆÐźÅÁ¿ÊÇÒ»ÑùµÄ£¬µ«ÊÇËü¾ø²»¿ÉÄÜÔÚÄÚºËÖ®ÍâʹÓã¬Òò´ËËüÓëSystem VµÄIPC»úÖÆÐźÅÁ¿ºÁ²»Ïà¸É¡£

    ÐźÅÁ¿ÔÚ´´½¨Ê±ÐèÒªÉèÖÃÒ»¸ö³õʼֵ£¬±íʾͬʱ¿ÉÒÔÓм¸¸öÈÎÎñ¿ÉÒÔ·ÃÎʸÃÐźÅÁ¿±£»¤µÄ¹²Ïí×ÊÔ´£¬³õʼֵΪ1¾Í±ä³É»¥³âËø£¨Mutex£©£¬¼´Í¬Ê±Ö»ÄÜÓÐÒ»¸öÈÎÎñ¿ÉÒÔ·ÃÎÊÐźÅÁ¿±£»¤µÄ¹²Ïí×ÊÔ´¡£

    Ò»¸öÈÎÎñÒªÏë·ÃÎʹ²Ïí×ÊÔ´£¬Ê×ÏȱØÐëµÃµ½ÐźÅÁ¿£¬»ñÈ¡ÐźÅÁ¿µÄ²Ù×÷½«°ÑÐźÅÁ¿µÄÖµ¼õ1£¬Èôµ±Ç°ÐźÅÁ¿µÄֵΪ¸ºÊý£¬±íÃ÷ÎÞ·¨»ñµÃÐźÅÁ¿£¬¸ÃÈÎÎñ±ØÐë¹ÒÆðÔÚ¸ÃÐźÅÁ¿µÄµÈ´ý¶ÓÁеȴý¸ÃÐźÅÁ¿¿ÉÓã»Èôµ±Ç°ÐźÅÁ¿µÄֵΪ·Ç¸ºÊý£¬±íʾ¿ÉÒÔ»ñµÃÐźÅÁ¿£¬Òò¶ø¿ÉÒÔÁ¢¿Ì·ÃÎʱ»¸ÃÐźÅÁ¿±£»¤µÄ¹²Ïí×ÊÔ´¡£

    µ±ÈÎÎñ·ÃÎÊÍê±»ÐźÅÁ¿±£»¤µÄ¹²Ïí×ÊÔ´ºó£¬±ØÐëÊÍ·ÅÐźÅÁ¿£¬ÊÍ·ÅÐźÅÁ¿Í¨¹ý°ÑÐźÅÁ¿µÄÖµ¼Ó1ʵÏÖ£¬Èç¹ûÐźÅÁ¿µÄֵΪ·ÇÕýÊý£¬±íÃ÷ÓÐÈÎÎñµÈ´ýµ±Ç°ÐźÅÁ¿£¬Òò´ËËüÒ²»½ÐÑËùÓеȴý¸ÃÐźÅÁ¿µÄÈÎÎñ¡£

    ÐźÅÁ¿µÄAPIÓУº


    DECLARE_MUTEX(name)




    ¸ÃºêÉùÃ÷Ò»¸öÐźÅÁ¿name²¢³õʼ»¯ËüµÄֵΪ0£¬¼´ÉùÃ÷Ò»¸ö»¥³âËø¡£


    DECLARE_MUTEX_LOCKED(name)




    ¸ÃºêÉùÃ÷Ò»¸ö»¥³âËøname£¬µ«°ÑËüµÄ³õʼֵÉèÖÃΪ0£¬¼´ËøÔÚ´´½¨Ê±¾Í´¦ÔÚÒÑËø״̬¡£Òò´Ë¶ÔÓÚÕâÖÖËø£¬Ò»°ãÊÇÏÈÊͷźó»ñµÃ¡£


    void sema_init (struct semaphore *sem, int val);




    ¸Ãº¯ÓÃÓÚÊý³õʼ»¯ÉèÖÃÐźÅÁ¿µÄ³õÖµ£¬ËüÉèÖÃÐźÅÁ¿semµÄֵΪval¡£


    void init_MUTEX (struct semaphore *sem);




    ¸Ãº¯ÊýÓÃÓÚ³õʼ»¯Ò»¸ö»¥³âËø£¬¼´Ëü°ÑÐźÅÁ¿semµÄÖµÉèÖÃΪ1¡£


    void init_MUTEX_LOCKED (struct semaphore *sem);




    ¸Ãº¯ÊýÒ²ÓÃÓÚ³õʼ»¯Ò»¸ö»¥³âËø£¬µ«Ëü°ÑÐźÅÁ¿semµÄÖµÉèÖÃΪ0£¬¼´Ò»¿ªÊ¼¾Í´¦ÔÚÒÑËø״̬¡£


    void down(struct semaphore * sem);




    ¸Ãº¯ÊýÓÃÓÚ»ñµÃÐźÅÁ¿sem£¬Ëü»áµ¼ÖÂ˯Ãߣ¬Òò´Ë²»ÄÜÔÚÖжÏÉÏÏÂÎÄ£¨°üÀ¨IRQÉÏÏÂÎĺÍsoftirqÉÏÏÂÎÄ£©Ê¹Óøú¯Êý¡£¸Ãº¯Êý½«°ÑsemµÄÖµ¼õ1£¬Èç¹ûÐźÅÁ¿semµÄÖµ·Ç¸º£¬¾ÍÖ±½Ó·µ»Ø£¬·ñÔòµ÷ÓÃÕß½«±»¹ÒÆð£¬Ö±µ½±ðµÄÈÎÎñÊͷŸÃÐźÅÁ¿²ÅÄܼÌÐøÔËÐС£


    int down_interruptible(struct semaphore * sem);




    ¸Ãº¯Êý¹¦ÄÜÓëdownÀàËÆ£¬²»Í¬Ö®´¦Îª£¬down²»»á±»Ðźţ¨signal£©´ò¶Ï£¬µ«down_interruptibleÄܱ»ÐźŴò¶Ï£¬Òò´Ë¸Ãº¯ÊýÓзµ»ØÖµÀ´Çø·ÖÊÇÕý³£·µ»Ø»¹ÊDZ»ÐźÅÖжϣ¬Èç¹û·µ»Ø0£¬±íʾ»ñµÃÐźÅÁ¿Õý³£·µ»Ø£¬Èç¹û±»ÐźŴò¶Ï£¬·µ»Ø-EINTR¡£


    int down_trylock(struct semaphore * sem);




    ¸Ãº¯ÊýÊÔ×Å»ñµÃÐźÅÁ¿sem£¬Èç¹ûÄܹ»Á¢¿Ì»ñµÃ£¬Ëü¾Í»ñµÃ¸ÃÐźÅÁ¿²¢·µ»Ø0£¬·ñÔò£¬±íʾ²»ÄÜ»ñµÃÐźÅÁ¿sem£¬·µ»ØֵΪ·Ç0Öµ¡£Òò´Ë£¬Ëü²»»áµ¼Öµ÷ÓÃÕß˯Ãߣ¬¿ÉÒÔÔÚÖжÏÉÏÏÂÎÄʹÓá£


    void up(struct semaphore * sem);




    ¸Ãº¯ÊýÊÍ·ÅÐźÅÁ¿sem£¬¼´°ÑsemµÄÖµ¼Ó1£¬Èç¹ûsemµÄֵΪ·ÇÕýÊý£¬±íÃ÷ÓÐÈÎÎñµÈ´ý¸ÃÐźÅÁ¿£¬Òò´Ë»½ÐÑÕâЩµÈ´ýÕß¡£

    ÐźÅÁ¿ÔÚ¾ø´ó²¿·ÖÇé¿öÏÂ×÷Ϊ»¥³âËøʹÓã¬ÏÂÃæÒÔconsoleÇý¶¯ÏµÍ³ÎªÀý˵Ã÷ÐźÅÁ¿µÄʹÓá£

    ÔÚÄÚºËÔ´ÂëÊ÷µÄkernel/printk.cÖУ¬Ê¹ÓúêDECLARE_MUTEXÉùÃ÷ÁËÒ»¸ö»¥³âËøconsole_sem£¬ËüÓÃÓÚ±£»¤consoleÇý¶¯Áбíconsole_driversÒÔ¼°Í¬²½¶ÔÕû¸öconsoleÇý¶¯ÏµÍ³µÄ·ÃÎÊ¡£

    ÆäÖж¨ÒåÁ˺¯Êýacquire_console_semÀ´»ñµÃ»¥³âËøconsole_sem£¬¶¨ÒåÁËrelease_console_semÀ´ÊÍ·Å»¥³âËøconsole_sem£¬¶¨ÒåÁ˺¯Êýtry_acquire_console_semÀ´¾¡Á¦µÃµ½»¥³âËøconsole_sem¡£ÕâÈý¸öº¯Êýʵ¼ÊÉÏÊÇ·Ö±ð¶Ôº¯Êýdown£¬upºÍdown_trylockµÄ¼òµ¥°ü×°¡£

    ÐèÒª·ÃÎÊconsole_driversÇý¶¯Áбíʱ¾ÍÐèҪʹÓÃacquire_console_semÀ´±£»¤console_driversÁÐ±í£¬µ±·ÃÎÊÍê¸ÃÁбíºó£¬¾Íµ÷ÓÃrelease_console_semÊÍ·ÅÐźÅÁ¿console_sem¡£

    º¯Êýconsole_unblank£¬console_device£¬console_stop£¬console_start£¬register_consoleºÍunregister_console¶¼ÐèÒª·ÃÎÊconsole_drivers£¬Òò´ËËüÃǶ¼Ê¹Óú¯Êý¶Ôacquire_console_semºÍrelease_console_semÀ´¶Ôconsole_drivers½øÐб£»¤¡£