test-und-sett

computer sainslo, test-und-sett instruction anedi memory sthaanaaniki (sett) 1 vraayadaaniki mariyu daani paatha viluvanu oke anugaa (i.ii., antaraayam kaliginchani) operation. coll dwara rashtram marchabadindo ledho chuudataaniki coll chesevaaru phalitaanni "pareekshinchavacchu". bahula prakriyalu oke memory sthaanaanni access cheyagaligithe, mariyu oka prakriya prastutam test-und-setnu nirvahistunnatlayite, modati prakriya yokka test-und-sett poortayye varaku yea itara prakriya maroka test-und-setnu praarambhinchadu. oka central prosessing unit (cpu) dual-port raam vanti maroka electronic bhaagam andinche test-und-sett suuchanalanu upayoginchavacchu; cpu kooda test-und-sett suuchanalanu andinchavacchu.
anhu pareeksha mariyu sett suuchanalanu upayoginchi eee krindi vidhamgaa oka taalaanni nirminchavachuhu
eee code modati test-und-setku mundu edho oka samayamlo memory sthaanaanni tirigi praarambhinchinatlu bhaavistundi. coll prakriya paatha viluva 0 ayithe laaknu pondutundi, lekapothe vail-loop lock pondadaaniki vechi untundi. dheenini spinlock antaaru. yea samayamloonainaa, lock holder maroka vyakti dwara samuparjana kosam laaknu vidudala cheyadaaniki memory sthaanaanni tirigi tirigi sett cheyavachu-dheeniki pratyeka nirvahana avasaram ledhu endukante holder eee memory sthaanaanni "kaligi unnadu". "test und test-und-sett" maroka udaaharana.
pareeksha-mariyu-samiti (1-bitt poolika) parimita ekaabhipraaya sankhyanu kaligi undani mariyu garishtamgaa rendu ummadi prakriyala kosam vechi-laeni ekaabhipraaya samasyanu parishkarinchagaladani mauries herley (1991) niroopinchindi. dheeniki viruddhamgaa, polchi-marchandi (32-bitt poolika) eee samasyaku marinta saadhaarana parishkaranni andistundi, mariyu konni amalulalo polchi-rendu-marpidi (64-bitt poolika) kooda podiginchina viniyogaaniki andubaatulo undhi.
pareeksha mariyu sett yokka hardware amalu.
deprom test-und-sett suuchanalu aneka vidhaluga pani cheyagalavu. ikkada rendu vaividhyaalu unnaayi, rendoo kooda oka depromnu vivaristaayi, idhi sarigga 2 portlanu andistundi, idhi depromloni prati memory sthaanaaniki 2 ververu electronic bhaagaalanu (2 sipus vantivi) access cheyadaaniki anumatistundi.
vaividhyam 1.
cpu 1 oka pareeksha-mariyu-sett suuchananu jaarii chesinappudu, deprom modata memory sthaanam yokka chirunamanu pratyeka pradaesamloe nilva cheyadam dwara dheeni yokka "antargata gamanika" chestundi. eee samayamlo, adhe memory sthaanaaniki pareeksha-mariyu-sett suuchananu jaarii cheyadaaniki CPU 2 jarigithe, dpram modata daani "antargata gamanika" nuu tanikhi chestundi, paristhitini gurtistundi mariyu raddiigaa unna antaraayam kaligistundi, idhi CPU 2 kii vechi undi tirigi prayatninchaalani chebutundi. idhi antaraayam kaliginche yantraamgaanni upayoginchi bijeegaa vechi unna ledhaa spinlock amalu. idantaa hardware vegamtho jarugutundi kabatti, spin-lock nundi bayatapadataaniki cpu 2 yokka vechi undatam chaalaa takkuva.
CPU2 memory sthaanaanni access cheyadaaniki prayatnistundaa ledhaa, CPU1 icchina pareekshanu DPram nirvahistundi. pareeksha vijayavantamaite, CPU1 icchina viluvaku memory sthaanaanni dPram sett chestundi. appudu CPU1 akkada vraastunna daani "antargata note" nuu dPram tudichivestundi. eee samayamlo, cpu 2 oka test-und-setnu jaarii cheyagaladu, idhi vijayavantamavutundi.
vaividhyam 2.
memory location a" kuu vraayadaaniki CPU 1 oka test-und-sett suuchananu jaarii chestundi. deprom ventane memory location a loe viluvanu nilva cheyadu, conny badulugaa ekakaalamlo prastuta viluvanu pratyeka registerku taralistundi, adhe samayamlo memory location yokka kantentlanu pratyeka "flag viluva" kii sett chestundi. eee samayamlo, CPU 2 memory location a kii test-und-setnu jaarii chesthe, dpram pratyeka flag viluvanu gurtistundi, mariyu vaividhyam 1 loe, oka bijeegaa unna antaraayam kaligistundi.
CPU2 memory sthaanaanni access cheyadaaniki prayatnistundaa ledhaa, dpram ippudu CPU1 yokka pareekshanu nirvahistundi. pareeksha vijayavantamaite, deprom memory sthaanaanni cpu 1 perkonna viluvaku sett chestundi. pareeksha viphalamaithe, deprom viluvanu pratyeka register nundi memory sthaanaaniki tirigi coppy chestundi. yea operation ayina pratyeka genda viluvanu tudichivestundi. cpu 2 ippudu test-und-setnu jaarii chesthe, adhi vijayavantamavutundi.
test-und-sett yokka software amalu.
konni instruction setlalo anhu pareeksha mariyu sett yantra bhasha bodhana untundi. udaaharanalalo x86 mariyu ibm vyavastha/360 mariyu daani vaarasulu (z/nirmaanamtho sahaa) unnaayi.
chadavandi-savarinchandi-vraayandi ledhaa polchandi-mariyu-marpidi suuchanalanu upayoginchi anhu pareeksha-mariyu-setnu ippatikee amalu cheyaleni vaaru.
pareeksha mariyu nirdesam sett, booliyan viluvalatho upayoginchinappudu, eee krindi functionlo chuupina vidhamgaa tarkaanni upayogistundi, function tappanisarigaa paramaanugaa amalu cheyali. antey, yea itara prakriya kooda function madhya-amaluni antaraayam kaliginchalekapovali, tadwara function amalu cheyabadinappudu matrame unna sthithini chudavachu. dheeniki hardware maddatu avasaram; chuupina vidhamgaa dheenini amalu cheyalemu. ayinappatikee, chuupina code pareeksha-mariyu-sett yokka pravartananu vivarinchadaaniki sahaayapadutundi. gamanikah eee udaaharanalo, 'lock' anedi suuchana dwara (ledhaa paeru dwara) aamodinchabadindani bhaavinchabadutundi, conny 'prarambha' kuu ketayimpu kotta viluvanu srushtistundi (kevalam suuchananu coppy cheyadam kaadu).
 function testandset (booliyan _ reflock) {
 booliyan prarambha = lock;
 lock = nijamainadi;
 prarambha tirigi;
code paramaanu kaadu anie choopinchadame kakunda, pareeksha-mariyu-sett suuchanala ardhamlo, idhi paina perkonna dpram hardware pareeksha-mariyu-sett vivaranalaku kooda bhinnamgaa untundi. ikkada, sett cheyabadutunna viluva mariyu pareeksha sthiramgaa mariyu asthiramgaa untaayi, mariyu pareeksha phalitamtho sambandham lekunda viluva naveekarinchabadutundi, ayithe deprom test-und-sett kosam, pareeksha vijayavantam ayinappudu matrame memory sett cheyabadutundi, mariyu sett cheyadaaniki viluva mariyu pareeksha paristhiti cpu dwara perkonabadutundi. ikkada, sett cheyavalasina viluva 1 matrame kaavacchu, conny memory sthaanaaniki 0 mariyu 1 matrame chellubaatu ayye viluvalugaa pariganinchabadithe, mariyu "viluva sunna kaanidi" matrame anumatinchabadina pareeksha ayithe, idhi dpram hardware kosam vivarinchina kesuku samanam (ledhaa, marinta pratyaekamgaa, dpram kesu eee parimitula kinda dheeniki taggutundi). aaa drukkonam nundi, dheenini sarigga, aaa padam yokka puurti, sampradaaya ardhamlo "test-und-sett" anie pilavavacchu. gamaninchavalasina mukhyamaina vishayam emitante, test-und-sett yokka saadhaarana uddesam mariyu suutramh oka viluvanu pareekshinchi, oka anhu operationlo sett chestaaru, tadwara yea itara programme thread ledhaa prakriya lakshya ghnaapakasakti sthaanaanni pareekshinchina tarvaata maarchadu, conny adhi sett cheyabadataaniki mundu. (endukante, aaa sthaanam prastutam oka nirdishta viluvanu kaligi unte matrame sett cheyali, kontakaalam mundu aaa viluvanu kaligi unte kaadu.)
sii prograaming bhaashalo, amalu ilaa untundih
intu test _ und _ sett (intu * lockpter) {
 paatha viluva;
 //--anhu vibhagam prarambham --
 //dheenini udaaharana prayojanaala kosam matrame sudokodga ardham chesukovali.
 //eee code yokka saampradaaya sankalanam anutvaaniki haamii ivvadu,
 //shared memory upayogam (i.ii., naan-kaash viluvalu), compiler nundi rakshana
 //optimisations, ledhaa itara avasaramaina lakshanaalu.
 paatha viluva = * lockpter;
 *lockpter = lock cheyabadindi;
 //--anhu vibhagam mugimpu --
 paatha viluvanu tirigi ivvandi;
code nijamgaa rendu operations unnayani kooda chuupistumdihi oka anhu patanam-savarinchadam-vrayadam mariyu oka pareeksha. chadavandi-savarinchandi-vrayadam matrame anugaa undaali. (idhi nijam endukante viluvanu polchadamlo entha samayam ayina alasyam cheyadam valla pareekshaku viluva pondina tarvaata pareeksha phalitaanni maarchadu. code prarambha viluvanu vraasina tarvaata, pareeksha yokka phalitam inca lekkinchabadakapoyina, sthaapinchabadindi-e.zii., = = operator dwara.)
test-und-sett upayoginchi paraspara minahaayimpu.
paraspara minahaayimpunu amalu cheyadaaniki oka maargam eee krindi vidhamgaa test-und-sett aadhaarita laaknu upayoginchadam
spin lock yokka suudoe-sii amalu.
asthira intluck = 0;
shoonya vimarsanaatmaka () {
 //spin lock manaku lock vachhe varaku eppatikee loop cheyandi; lock undani maaku telusu
 //eee vail loop nundi nishkraminchina tarvaata vijayavantamgaa pondabadindi endukante 
 //test _ and _ set () function laaknu lock chesi munupati laaknu tirigi istundi 
 //viluva. munupati lock viluva 1 ayithe, lock appatike * * gaa undhi**
 //maroka thread ledhaa prakriya dwara lock cheyabadindi. munupati lock viluva okasari
 //0 ayithe, appudu adhi lock * * lock cheyabadaledani suuchistumdi
 //lock chesaru, conny ippudu adhi lock cheyabadindi endukante memu daanini lock chesaamu, suuchistumdi
 //maaku taalam undhi.
 ayithe (test _ und _ sett (& lock) = = 1); 
 klishtamaina vibhagam// oka samayamlo eee vibhaagamlo oke oka prakriya undavacchu.
 klishtamaina vibhaagamto puurti chesinappudu lock = 0;// vidudala lock
lock variable anedi shared variable i.ii. dheenini anni processorlu/threadlu dwara access cheyavachu. asthira" keelaka padaanni gamaninchandi. asthirata lenappudu, compiler mariyu/ledhaa cpu (luu) lock cheyadaaniki mariyu/ledhaa kaash chesina viluvalanu upayoginchadaaniki accessnu optimize cheyavachu, tadwara paina perkonna kodnu tappugaa andistundi. dheeniki viruddhamgaa, mariyu duradrushtavasaattu, "asthira" uniki anedi chadavadam mariyu vrayadam ghnaapakasaktiki kattubadi unnayani haamii ivvadu. konni compilerlu memareeki kattubadi unnayani nirdhaarinchadaaniki memory addamkulanu jaarii chestaayi, conny sii/sii + + loe "asthira" yokka ardhashaastram chaalaa aspashtamgaa unnanduna, anni compilerlu alaa cheyavu. adhi jarugutundo ledho telusukovadaaniki mea compiler yokka documentation sampradinchandi.
eee spin lock functionnu bahula prakriyala dwara pilavavacchu, conny oka samayamlo klishtamaina vibhaagamlo oke prakriya matrame untundani haamii ivvabadindi. migilina prakriyalu lock vachhe varaku tirugutuu untaayi. oka prakriyaku eppuduu lock manjuru cheyakapovachhu. atuvanti sandarbhamlo idhi antuleni loop avutundi. idhi spin lock amalulo oka lopam, endukante idhi nyayanni nirdhaarinchadu. eee samasyalu paniteeru vibhaagamlo marinta vivarinchabaddaayi.
assembly amalu.
enter region; oka "jump tuu" tagg; function entry point.
 tsl regg, genda; pareekshinchi, taalam pettandi; genda
 shared variable; idhi coppy cheyabadindi
 register regg mariyu jendaaloki
 ; appudu anuvutho anuvutho amarchandi.
 cmp regg, #0; pravesha _ praantamloo genda sunna?
 jnz enter region; unte enter region kuu jump cheyandi
 ; regg anedi sunna kaanidi; i.ii.,
 pravaesaaniki genda sunna kaanidi.
 rett; nishkramana; i.ii.genda sunnaga undhi
 pravesam. memu ikkadaku vasthe, tsl
 ; daanini sunna kaanidigaa sett chestundi; eee vidhamgaa,
 ; memu vanarunu claim chesaamu
 jendaatho anubandham.
vadilivese praantamha
 jendaanu taralinchandi, #0; gendaalo gendaalo nilva cheyandi
 tirigi coll chesevaariki tirigi vellandi
ikkada coque _ 1 anedi anhu suuchana mariyu coque _ 2 anedi lock variable. lock pondakapothe prakriya tirigi raadu.
test-und-sett taalaala paniteeru moolyaankanam.
saadhaaranamgaa taalaala kosam naalugu pradhana muulyaankana kolamanalu analochita lock-samuparjana jaapyam, bassu traaphik, fairness mariyu nilva.
pareeksha mariyu sett skorlu vaatilo rendintipai takkuvagaa untaayi, avi adhika bassu traaphik mariyu anyayam.
processor p1 lock pondinappudu mariyu processor p2 kooda lock kosam vechi unnappudu, p2 laaknu pondadaaniki chese prayatnaalalo bassu laavaadaeveelanu konasaagistundi. oka processor lock pondinappudu, adhe lock pondaalanukune anni itara processorlu kooda laaknu pattukune varaku padepade bassu laavaadaeveelanu praarambhinchadam dwara lock pondadaaniki prayatnistuunae untaayi. idhi test-und-sett bassu traaphik avasaranni gananeeyamgaa penchutundi. idhi kaash mariyu coherence miss nundi itara traafiknu taggistundi. lock samuparjana prayatnaalalo viphalamainanduna traaphik santruptamai unnanduna idhi mottam vibhaganni mandagistundi. lock samuparjana abhyardhanalanu nirantaram praarambhinchananduna test-und-test-und-sett anedi tsl kante merugudala.
manam nyayanni parigananaloki teesukunnappudu, oka processor laaknu uchitamgaa sett chesinappudu daanni pondadaaniki sarasamaina avakaasam labhistunda anie memu pariseelistaamu. teevramaina paristhitiloo praasesarku aakalitho undavacchu.ii. aaa samayamlo adhi uchitam ayinappatikee, adhi ekkuva kaalam paatu taalam pondalekapovacchu.
oka taalam matrame avasaram kabatti tsl kosam nilva ovar head aemee ledhu. oka anhu bodhana mariyu saakha matrame avasaram kabatti analochita jaapyam kooda takkuvagaa untundi.

