PageSourceSearch

http://barngoggles.com/category/programming/

html barngoggles.com collected 2026-10-04 01:50:27 UTC 16,823 bytes, 189 lines download raw bytes

1<!DOCTYPE html>
2<!--[if IE 7]>
3<html class="ie ie7" lang="en-US">
4<![endif]-->
5<!--[if IE 8]>
6<html class="ie ie8" lang="en-US">
7<![endif]-->
8<!--[if !(IE 7) | !(IE 8)  ]><!-->
9<html lang="en-US">
10<!--<![endif]-->
11<head>
12<meta charset="UTF-8" />
13<meta name="viewport" content="width=device-width" />
14<title>Programming | c . run</title>
15<link rel="profile" href="http://gmpg.org/xfn/11" />
16<link rel="pingback" href="http://barngoggles.com/xmlrpc.php" />
17<!--[if lt IE 9]>
18<script src="http://barngoggles.com/wp-content/themes/twentytwelve/js/html5.js" type="text/javascript"></script>
18
19<![endif]-->
20<link rel="alternate" type="application/rss+xml" title="c . run &raquo; Feed" href="http://barngoggles.com/feed/" />
21<link rel="alternate" type="application/rss+xml" title="c . run &raquo; Comments Feed" href="http://barngoggles.com/comments/feed/" />
22<link rel="alternate" type="application/rss+xml" title="c . run &raquo; Programming Category Feed" href="http://barngoggles.com/category/programming/feed/" />
23		
23<script type="text/javascript">
24			window._wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/72x72\/","ext":".png","source":{"concatemoji":"http:\/\/barngoggles.com\/wp-includes\/js\/wp-emoji-release.min.js?ver=4.4.34"}};
25			!function(e,n,t){var a;function i(e){var t=n.createElement("canvas"),a=t.getContext&&t.getContext("2d"),i=String.fromCharCode;return!(!a||!a.fillText)&&(a.textBaseline="top",a.font="600 32px Arial","flag"===e?(a.fillText(i(55356,56806,55356,56826),0,0),3e3<t.toDataURL().length):"diversity"===e?(a.fillText(i(55356,57221),0,0),t=a.getImageData(16,16,1,1).data,a.fillText(i(55356,57221,55356,57343),0,0),(t=a.getImageData(16,16,1,1).data)[0],t[1],t[2],t[3],!0):("simple"===e?a.fillText(i(55357,56835),0,0):a.fillText(i(55356,57135),0,0),0!==a.getImageData(16,16,1,1).data[0]))}function o(e){var t=n.createElement("script");t.src=e,t.type="text/javascript",n.getElementsByTagName("head")[0].appendChild(t)}t.supports={simple:i("simple"),flag:i("flag"),unicode8:i("unicode8"),diversity:i("diversity")},t.DOMReady=!1,t.readyCallback=function(){t.DOMReady=!0},t.supports.simple&&t.supports.flag&&t.supports.unicode8&&t.supports.diversity||(a=function(){t.readyCallback()},n.addEventListener?(n.addEventListener("DOMContentLoaded",a,!1),e.addEventListener("load",a,!1)):(e.attachEvent("onload",a),n.attachEvent("onreadystatechange",function(){"complete"===n.readyState&&t.readyCallback()})),(a=t.source||{}).concatemoji?o(a.concatemoji):a.wpemoji&&a.twemoji&&(o(a.twemoji),o(a.wpemoji)))}(window,document,window._wpemojiSettings);
26		</script>
26
27		<style type="text/css">
28img.wp-smiley,
29img.emoji {
30	display: inline !important;
31	border: none !important;
32	box-shadow: none !important;
33	height: 1em !important;
34	width: 1em !important;
35	margin: 0 .07em !important;
36	vertical-align: -0.1em !important;
37	background: none !important;
38	padding: 0 !important;
39}
40</style>
41<link rel='stylesheet' id='uaf_client_css-css'  href='http://barngoggles.com/wp-content/uploads/useanyfont/uaf.css?ver=1436821703' type='text/css' media='all' />
42<link rel='stylesheet' id='twentytwelve-fonts-css'  href='http://fonts.googleapis.com/css?family=Open+Sans:400italic,700italic,400,700&#038;subset=latin,latin-ext' type='text/css' media='all' />
43<link rel='stylesheet' id='twentytwelve-style-css'  href='http://barngoggles.com/wp-content/themes/twentytwelve/style.css?ver=4.4.34' type='text/css' media='all' />
44<!--[if lt IE 9]>
45<link rel='stylesheet' id='twentytwelve-ie-css'  href='http://barngoggles.com/wp-content/themes/twentytwelve/css/ie.css?ver=20121010' type='text/css' media='all' />
46<![endif]-->
47<link rel='stylesheet' id='enlighter-local-css'  href='http://barngoggles.com/wp-content/plugins/enlighter/resources/EnlighterJS.min.css?ver=4.4.34' type='text/css' media='all' />
48<script type='text/javascript' src='http://barngoggles.com/wp-includes/js/jquery/jquery.js?ver=1.11.3'></script>
vendor: 1 bytes, line 48
48
49<script type='text/javascript' src='http://barngoggles.com/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1'></script>
vendor: 1 bytes, line 49
49
50<script type='text/javascript' src='http://barngoggles.com/wp-content/plugins/dropdown-menu-widget/scripts/include.js?ver=4.4.34'></script>
vendor: 1 bytes, line 50
50
51<script type='text/javascript' src='http://barngoggles.com/wp-content/plugins/enlighter/resources/mootools-core-yc.js?ver=4.4.34'></script>
vendor: 1 bytes, line 51
51
52<script type='text/javascript' src='http://barngoggles.com/wp-content/plugins/enlighter/resources/EnlighterJS.min.js?ver=4.4.34'></script>
52
53<link rel='https://api.w.org/' href='http://barngoggles.com/wp-json/' />
54<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://barngoggles.com/xmlrpc.php?rsd" />
55<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://barngoggles.com/wp-includes/wlwmanifest.xml" /> 
56<meta name="generator" content="WordPress 4.4.34" />
57<script type="text/javascript">/* <![CDATA[ */window.addEvent('domready', function(){if (typeof EnlighterJS == "undefined"){return;};EnlighterJS.Util.Init('pre.EnlighterJSRAW', 'code.EnlighterJSRAW', {"language":"cpp","theme":"enlighter","indent":2,"hover":"hoverEnabled","showLinenumbers":true,"rawButton":true,"infoButton":true,"windowButton":true,"rawcodeDoubleclick":false,"grouping":true,"cryptex":{"enabled":false,"email":"[email protected]"}});});/* ]]> */</script>
57
58
59
60<!-- Dropdown Menu Widget Styles by shailan (http://shailan.com) v1.9.4 on wp4.4.34 -->
61<link rel="stylesheet" href="http://barngoggles.com/wp-content/plugins/dropdown-menu-widget/css/shailan-dropdown.min.css" type="text/css" />
62<style type="text/css" media="all">
63	ul.dropdown { white-space: nowrap; }	
64	/** Show submenus */
65	ul.dropdown li:hover > ul, ul.dropdown li.hover ul{ display: block; }
66	
67	/** Show current submenu */
68	ul.dropdown li.hover ul, ul.dropdown ul li.hover ul, ul.dropdown ul ul li.hover ul, ul.dropdown ul ul ul li.hover ul, ul.dropdown ul ul ul ul li.hover ul , ul.dropdown li:hover ul, ul.dropdown ul li:hover ul, ul.dropdown ul ul li:hover ul, ul.dropdown ul ul ul li:hover ul, ul.dropdown ul ul ul ul li:hover ul { display: block; } 
69				
70			
71ul.dropdown li.parent>a{
72	padding-right:25px;
73}
74ul.dropdown li.parent>a:after{ 
75	content:""; position:absolute; top: 45%; right:6px;width:0;height:0; 
76	border-top:4px solid rgba(0,0,0,0.5);border-right:4px solid transparent;border-left:4px solid transparent }
77ul.dropdown li.parent:hover>a:after{
78	content:"";position:absolute; top: 45%; right:6px; width:0; height:0;
79	border-top:4px solid rgba(0,0,0,0.5);border-right:4px solid transparent;border-left:4px solid transparent }
80ul.dropdown li li.parent>a:after{
81	content:"";position:absolute;top: 40%; right:5px;width:0;height:0;
82	border-left:4px solid rgba(0,0,0,0.5);border-top:4px solid transparent;border-bottom:4px solid transparent }
83ul.dropdown li li.parent:hover>a:after{
84	content:"";position:absolute;top: 40%; right:5px;width:0;height:0;
85	border-left:4px solid rgba(0,0,0,0.5);border-top:4px solid transparent;border-bottom:4px solid transparent }
86
87
88</style>
89<!-- /Dropdown Menu Widget Styles -->
90
91 	<style type="text/css" id="twentytwelve-header-css">
92			.site-title,
93		.site-description {
94			position: absolute;
95			clip: rect(1px 1px 1px 1px); /* IE7 */
96			clip: rect(1px, 1px, 1px, 1px);
97		}
98		</style>
99	<style type="text/css" id="custom-background-css">
100body.custom-background { background-color: #595959; background-image: url('http://barngoggles.com/wp-content/uploads/2015/05/IMG_1865_fixedBig_smaller.jpg'); background-repeat: repeat; background-position: top center; background-attachment: scroll; }
101</style>
102</head>
103
104<body class="archive category category-programming category-11 custom-background full-width custom-font-enabled single-author">
105<div id="page" class="hfeed site">
106	<header id="masthead" class="site-header" role="banner">
107		<hgroup>
108			<h1 class="site-title"><a href="http://barngoggles.com/" title="c . run" rel="home">c . run</a></h1>
109			<h2 class="site-description">Warm storage for thing I think are interesting</h2>
110		</hgroup>
111
112		<nav id="site-navigation" class="main-navigation" role="navigation">
113			<h3 class="menu-toggle">Menu</h3>
114			<a class="assistive-text" href="#content" title="Skip to content">Skip to content</a>
115			<div class="menu-basic-menu-container"><ul id="menu-basic-menu" class="nav-menu"><li id="menu-item-444" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-444"><a href="http://barngoggles.com">HOME</a></li>
116<li id="menu-item-428" class="menu-item menu-item-type-taxonomy menu-item-object-category current-menu-item menu-item-428"><a href="http://barngoggles.com/category/programming/">Programming</a></li>
117<li id="menu-item-437" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-437"><a href="http://barngoggles.com/about/">About</a></li>
118</ul></div>		</nav><!-- #site-navigation -->
119
120				<a href="http://barngoggles.com/"><img src="http://barngoggles.com/wp-content/uploads/2012/10/teaLights.png" class="header-image" width="1000" height="479" alt="" /></a>
121			</header><!-- #masthead -->
122
123	<div id="main" class="wrapper">
124	<section id="primary" class="site-content">
125		<div id="content" role="main">
126
127					<header class="archive-header">
128				<h1 class="archive-title">Category Archives: <span>Programming</span></h1>
129
130						</header><!-- .archive-header -->
131
132			
133	<article id="post-383" class="post-383 post type-post status-publish format-standard hentry category-c category-programming">
134				<header class="entry-header">
135									<h1 class="entry-title">
136				<a href="http://barngoggles.com/mutex-diversion-from-atomics/" rel="bookmark">Mutex Diversion from Atomics</a>
137			</h1>
138			<!--
139							<div class="comments-link">
140					<a href="http://barngoggles.com/mutex-diversion-from-atomics/#respond"><span class="leave-reply">Leave a reply</span></a>				</div><!-- .comments-link -> //THIS LINE NEEDS A TRAILING HYPHEN
141			-->
142		</header><!-- .entry-header -->
143
144				<div class="entry-content">
145			<p>I mentioned in the <a href="http://barngoggles.com/?p=341" target="_blank">memory re-ordering entry</a> that I did some disassembly of a mutex. I actually did two disassemblies, one with a lock guard and one without. Here&#8217;s what I found.</p>
146<p>I used this C++ code for the non-gaurded mutex, which successfully ensures that each thread has access to a shared resource without fear of another thread interfer
146ing</p>
147<pre class="EnlighterJSRAW" data-enlighter-language="null">...
148std::mutex mutex; //declared at global scope
149...
150mutex.lock(); //executed within a threaded function
151...
152mutex.unlock();
153...</pre>
154<p>The x86 for this is different in one major way from the x86 we saw in the primary post, and that&#8217;s because we are calling into the data segment here.</p>
155<p><a href="http://barngoggles.com/wp-content/uploads/2015/07/solveWithMutex_noLockGuard_disassembly1.png"><img class="alignnone size-full wp-image-399" src="http://barngoggles.com/wp-content/uploads/2015/07/solveWithMutex_noLockGuard_disassembly1.png" alt="solveWithMutex_noLockGuard_disassembly" width="715" height="394" srcset="http://barngoggles.com/wp-content/uploads/2015/07/solveWithMutex_noLockGuard_disassembly1-300x165.png 300w, http://barngoggles.com/wp-content/uploads/2015/07/solveWithMutex_noLockGuard_disassembly1-624x344.png 624w, http://barngoggles.com/wp-content/uploads/2015/07/solveWithMutex_noLockGuard_disassembly1.png 715w" sizes="(max-width: 715px) 100vw, 715px" /></a></p>
156<p>The code calls the _imp__Mtx_lock instruction then pops the last frame off the stack (that&#8217;s effectively what add esp, 4 does. There is actually a small <
156a href="http://stackoverflow.com/questions/5904018/pop-or-add-esp-4-what-is-the-difference" target="_blank">difference</a>.) and then checks if the return value is zero or not (error). If it&#8217;s zero then we jump down to the instructions we saw in the previous post (with a compiler reordering), otherwise we throw an exception. We see the same pattern of calling the function, checking the result and maybe throwing an exception in the __imp___Mtx_unlock section.</p>
157<p>If we look in the data segment we see the following for the mutex.</p>
158<p><a href="http://barngoggles.com/wp-content/uploads/2015/07/solveWithMutex_dataSegment_disassembly.png"><img class="alignnone size-full wp-image-396" src="http://barngoggles.com/wp-content/uploads/2015/07/solveWithMutex_dataSegment_disassembly.png" alt="solveWithMutex_dataSegment_disassembly" width="649" height="30" srcset="http://barngoggles.com/wp-content/uploads/2015/07/solveWithMutex_dataSegment_disassembly-300x14.png 300w, http://barngoggles.com/wp-content/uploads/2015/07/solveWithMutex_dataSegment_disassembly-624x29.png 624w, http://barngoggles.com/wp-content/uploads/2015/07/solveWithMutex_dataSegment_disassembly.png 649w" sizes="(max-width: 649px) 100vw, 649px" /></a></p>
159<p>It&#8217;s just a variable that&#8217;s cross referenced to our two threads, not terribly exciting. If we look at the extern segment we see both lock and unlock function calls. Just a refresher, extern is for declaring symbols which aren&#8217;t in the current assembly unit but are defined in some other unit that is going to be referenced by the current unit.</p>
160<p><a href="http://barngoggles.com/wp-content/uploads/2015/07/solveWithMutex_mutexExtern_disassembly.png"><img class="alignnone size-full wp-image-389" src="http://barngoggles.com/wp-content/uploads/2015/07/solveWithMutex_mutexExtern_disassembly.png" alt="solveWithMutex_mutexExtern_disassembly" width="591" height="48" srcset="http://barngoggles.com/wp-content/uploads/2015/07/solveWithMutex_mutexExtern_disassembly-300x24.png 300w, http://barngoggles.com/wp-content/uploads/2015/07/solveWithMutex_mutexExtern_disassembly.png 591w" sizes="(max-width: 591px) 100vw, 591px" /></a></p>
161<p>The mutex lock and unlock are kernel functions and this aside is meant to be a bit shorter than what it takes to do kernel disassembly. Let&#8217;s move on to the lock gaurded case. How do lock gaurds like this work.</p>
162<pre class="EnlighterJSRAW" data-enlighter-language="null">...
163std::mutex mutex;
164...
165std::lock_guard&lt;std::mutex&gt; lock(mutex);
166...</pre>
167<p>Notice that the above code doesn&#8217;t contain an unlock statement. This would be an error if we were just using the mutex&#8217;s lock method because the mutex would never unlock. Looking at the disassembly below we see that the mutex does unlock when the function finishes.</p>
168<p><a href="http://barngoggles.com/wp-content/uploads/2015/07/solveWithMutex_disassembly.png"><img class="alignnone size-full wp-image-388" src="http://barngoggles.com/wp-content/uploads/2015/07/solveWithMutex_disassembly.png" alt="solveWithMutex_disassembly" width="554" height="377" srcset="http://barngoggles.com/wp-content/uploads/2015/07/solveWithMutex_disassembly-300x204.png 300w, http://barngoggles.com/wp-content/uploads/2015/07/solveWithMutex_disassembly.png 554w" sizes="(max-width: 554px) 100vw, 554px" /></a></p>
169<p>I had half expected that the lock gaurd would do something fancy but it doesn&#8217;t seem be much more than insurance for unlocking the mutex. Yep, that&#8217;s the same code we saw above in the manual lock and unlock cases. If you&#8217;d like to explore the exciting world of kernel mutexs, take a look at <a href="https://github.com/torvalds/linux/blob/de182468d1bb726198abaab315820542425270b7/kernel/locking/mutex.c" target="_blank">this C code for the linux mutex</a>.</p>
170					</div><!-- .entry-content -->
171		
172		<footer class="entry-meta">
173			This entry was posted in <a href="http://barngoggles.com/category/programming/c/" rel="category tag">C++</a>, <a href="http://barngoggles.com/category/programming/" rel="category tag">Programming</a> on <a href="http://barngoggles.com/mutex-diversion-from-atomics/" title="8:02 am" rel="bookmark"><time class="entry-date" datetime="2015-07-17T08:02:09+00:00">17 July, 2015</time></a><span class="by-author"> by <span class="author vcard"><a class="url fn n" href="http://barngoggles.com/author/barngoggles/" title="View all posts by barngoggles" rel="author">barngoggles</a></span></span>.						
174<!--
175<div class="comments-link"><a href="http://barngoggles.com/mutex-diversion-from-atomics/#respond"><span class="leave-reply">Leave a Comment</span></a></div>-->
176
177		</footer><!-- .entry-meta -->
178	</article><!-- #post -->		<nav id="nav-below" class="navigation" role="navigation">
179			<h3 class="assistive-text">Post navigation</h3>
180			<div class="nav-previous"><a href="http://barngoggles.com/category/programming/page/2/" ><span class="meta-nav">&larr;</span> Older posts</a></div>
181			<div class="nav-next"></div>
182		</nav><!-- #nav-below .navigation -->
183	
184		
185		</div><!-- #content -->
186	</section><!-- #primary -->
187
188
189	

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.