[討論] pthread library中的mutex lock

看板C_and_CPP作者 (小星)時間12年前 (2012/07/15 17:06), 編輯推噓1(108)
留言9則, 5人參與, 最新討論串1/1
HI各位版友 小弟最近在trace pthread library的code 因為要研究出mutex lock這個動作內部實作方式&花費時間 我目前研究了 int Pthread_mutex_lock int Pthread_mutex_unlock 以及在lock內部會用到的static void activate_next_thread 會來版上PO文是因為 雖然我一行一行的看 也google了一些相關資訊 但還是怕自己對於一些細部的運作方式並未通盤理解 所以想上來問問版友們 也許剛好會有對mutex lock程式碼相當專精的版友! 希望可以讓我問一些問題 :D 如果需要P幣我可以全數奉上 雖然只有一兩千塊XDDD 若是單純樂意討論的 更是非常感謝 http://ppt.cc/E1ZB pthread.c檔 http://ppt.cc/U532 pthread.h檔 如果懶惰也可以看以下 --------------------------------------- int Pthread_mutex_lock (Pthread_mutex_t * mutex){ Pthread_t * new; if (mutex == NULL) return (errno = EINVAL); block_scheduler (); if (mutex->locked) { new = realloc(mutex->threads, sizeof (Pthread_t)*(mutex->nb_threads_waiting + 1)); if (new == NULL) { unblock_scheduler(); return (EAGAIN); } mutex->threads=new; mutex->threads[mutex->nb_threads_waiting]= current_thread; mutex->nb_threads_waiting ++; /* mutex_lock is not a cancel point */ threads[current_thread].state = STATE_WAITING; if (sigsetjmp (threads[current_thread].context, 1) == 0) activate_next_thread (); } mutex->locked = 1; unblock_scheduler (); return (errno = 0); } int Pthread_mutex_unlock (Pthread_mutex_t * mutex){ if ((mutex == NULL) || (! mutex->locked)) return (errno = EINVAL); block_scheduler (); if (mutex->nb_threads_waiting) { threads [mutex->threads[mutex->nb_threads_waiting - 1]].state = STATE_RUNNING; mutex -> nb_threads_waiting --; } if (! mutex->nb_threads_waiting) mutex->locked = 0; unblock_scheduler (); return (0); } 有興趣討論的版友 請務必站內信我!!或直接推文 萬分感謝 祝大家寫code順利 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.169.80.169

07/15 18:16, , 1F
P幣是能幹嘛,要討論問題何不直接po版就好?
07/15 18:16, 1F
r大對不起 因為我不是像一般那樣有個BUG 或者有某個概念不清楚可以問 而是對於整個CODE的流程沒有完全的理解 比較需要"討論" 可能會需要你一句我一句的進行 所以無法順利的以文章的方式呈現 至於P幣只是隨著ptt的趨勢 算是心意而已 並不代表什麼 請息怒QQ ※ 編輯: didayo 來自: 118.169.80.169 (07/15 23:14)

07/15 23:34, , 2F
你就直接po就好,大家都很熱心的
07/15 23:34, 2F
ok 那我就先從最大的點開始好了 CODE中有很多current_thread 我想應該如字面上是代表執行中的thread 可是看完整個.c檔跟.h檔 current_thread都沒有變動的地方 例如current_thread=???之類的 所以好像變成每個mutex->threads[mutex->nb_threads_waiting] 裡面的值都一樣 另外ctrl_find整個.c檔 也沒看到mutex->threads[mutex->nb_threads_waiting]有什麼用處 總結是我不太懂current_thread是幹嘛用的@@? ※ 編輯: didayo 來自: 118.169.80.169 (07/15 23:51)

07/16 04:19, , 3F
有種東西叫 TLS, 可以先去 google 一下
07/16 04:19, 3F
Transport Layer Security 請問這跟我的問題有什麼關聯呢? ※ 編輯: didayo 來自: 118.169.80.169 (07/16 16:05)

07/16 16:15, , 4F
tls thread local storage
07/16 16:15, 4F
謝謝樓上,所以你們想表達的是current_thread是被thread各自存取嗎? 那這樣,我還是不知道current_thread主要的用處是什麼@@ ※ 編輯: didayo 來自: 118.169.80.169 (07/16 16:45)

07/17 01:47, , 5F
倒數第三行不是有嘛,記錄現在context是第幾個thread用的
07/17 01:47, 5F

07/17 01:48, , 6F
另外這份看起來有夠陽春,想要從這邊知道lock的時間有點不
07/17 01:48, 6F

07/17 01:49, , 7F
切實際,可以去看看glibc光是一個lock就多長。
07/17 01:49, 7F

07/17 01:55, , 8F
建議你先去找llnl的pthread programming來看,這份code基本
07/17 01:55, 8F

07/17 01:55, , 9F
上就是把該記錄的資料記下來然後根據POSIX標準動作
07/17 01:55, 9F
謝謝c大 我會再去研究 ※ 編輯: didayo 來自: 118.169.89.48 (07/17 14:38)
文章代碼(AID): #1G0eVvNc (C_and_CPP)