<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix">On 09/29/2012 03:07 PM, Benjamin Wang
(gendwang) wrote:<br>
</div>
<blockquote
cite="mid:2DA6F4A3691599408358374D182280F911D128@xmb-rcd-x04.cisco.com"
type="cite">
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<meta name="Generator" content="Microsoft Word 14 (filtered
medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:SimSun;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:SimSun;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:SimSun;
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri","sans-serif";
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri","sans-serif";}
/* Page Definitions */
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US">Hi,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> Currently
virInitialize() method defined in libvirt.c has the
following code:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">int<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">virInitialize(void)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">{<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> if (initialized)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> return 0;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> initialized = 1;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> if
(virThreadInitialize() < 0 ||<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">
virErrorInitialize() < 0 ||<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">
virRandomInitialize(time(NULL) ^ getpid()) ||<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">
virNodeSuspendInit() < 0)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> return -1;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">……<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">}<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">When two threads access
virInitialize method, there is no lock for the “initialized”
parameter. If the first thread enters this method and set
“initialized” to 1,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">the second thread could
see that “initialized” is 1(Because initialized is not
volatiled, I say could). In some situation, before the first
thread finishes all the initialization,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">the second thread could
use some resources which should be initialized in Initialize
method.
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">If you have any
comments, please let me know. Thanks!<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">B.R.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Benjamin Wang<o:p></o:p></span></p>
</div>
<br>
</blockquote>
<br>
As the comments above the function said,<br>
"It's better to call this routine at startup in multithreaded
applications to avoid potential race when initializing the library."<br>
<br>
<br>
Guannan<br>
<br>
<br>
</body>
</html>