| 1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 2 |
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"> |
| 3 |
<head> |
| 4 |
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> |
| 5 |
<meta http-equiv="X-UA-Compatible" content="IE=11"/> |
| 6 |
<meta name="generator" content="Doxygen 1.15.0"/> |
| 7 |
<meta name="viewport" content="width=device-width, initial-scale=1"/> |
| 8 |
<title>Hardware Locality (hwloc): Thread Safety</title> |
| 9 |
<link href="tabs.css" rel="stylesheet" type="text/css"/> |
| 10 |
<script type="text/javascript" src="jquery.js"></script> |
| 11 |
<script type="text/javascript" src="dynsections.js"></script> |
| 12 |
<script type="text/javascript" src="clipboard.js"></script> |
| 13 |
<link href="doxygen.css" rel="stylesheet" type="text/css" /> |
| 14 |
</head> |
| 15 |
<body> |
| 16 |
<div id="top"><!-- do not remove this div, it is closed by doxygen! --> |
| 17 |
<div id="titlearea"> |
| 18 |
<table cellspacing="0" cellpadding="0"> |
| 19 |
<tbody> |
| 20 |
<tr id="projectrow"> |
| 21 |
<td id="projectalign"> |
| 22 |
<div id="projectname">Hardware Locality (hwloc)<span id="projectnumber"> 2.14.0</span> |
| 23 |
</div> |
| 24 |
</td> |
| 25 |
</tr> |
| 26 |
</tbody> |
| 27 |
</table> |
| 28 |
</div> |
| 29 |
<!-- end header part --> |
| 30 |
<!-- Generated by Doxygen 1.15.0 --> |
| 31 |
<script type="text/javascript"> |
| 32 |
$(function() { codefold.init(); }); |
| 33 |
</script> |
| 34 |
<script type="text/javascript" src="menudata.js"></script> |
| 35 |
<script type="text/javascript" src="menu.js"></script> |
| 36 |
<script type="text/javascript"> |
| 37 |
$(function() { |
| 38 |
initMenu('',false,false,'search.php','Search',false); |
| 39 |
}); |
| 40 |
</script> |
| 41 |
<div id="main-nav"></div> |
| 42 |
</div><!-- top --> |
| 43 |
<div id="doc-content"> |
| 44 |
<div><div class="header"> |
| 45 |
<div class="headertitle"><div class="title">Thread Safety </div></div> |
| 46 |
</div><!--header--> |
| 47 |
<div class="contents"> |
| 48 |
<div class="textblock"><p>Like most libraries that mainly fill data structures, hwloc is not thread safe but rather reentrant: all state is held in a <a class="el" href="a00169.html#ga9d1e76ee15a7dee158b786c30b6a6e38" title="Topology context.">hwloc_topology_t</a> instance without mutex protection. That means, for example, that two threads can safely operate on and modify two different <a class="el" href="a00169.html#ga9d1e76ee15a7dee158b786c30b6a6e38" title="Topology context.">hwloc_topology_t</a> instances, but they should not simultaneously invoke functions that modify the <em>same</em> instance. Similarly, one thread should not modify a <a class="el" href="a00169.html#ga9d1e76ee15a7dee158b786c30b6a6e38" title="Topology context.">hwloc_topology_t</a> instance while another thread is reading or traversing it. However, two threads can safely read or traverse the same <a class="el" href="a00169.html#ga9d1e76ee15a7dee158b786c30b6a6e38" title="Topology context.">hwloc_topology_t</a> instance concurrently.</p> |
| 49 |
<p>When running in multiprocessor environments, be aware that proper thread synchronization and/or memory coherency protection is needed to pass hwloc data (such as <a class="el" href="a00169.html#ga9d1e76ee15a7dee158b786c30b6a6e38" title="Topology context.">hwloc_topology_t</a> pointers) from one processor to another (e.g., a mutex, semaphore, or a memory barrier). Note that this is not a hwloc-specific requirement, but it is worth mentioning.</p> |
| 50 |
<p>For reference, <a class="el" href="a00169.html#ga9d1e76ee15a7dee158b786c30b6a6e38" title="Topology context.">hwloc_topology_t</a> modification operations include (but may not be limited to):</p> |
| 51 |
<dl> |
| 52 |
<dt>Creation and destruction </dt> |
| 53 |
<dd><p class="startdd"><span class="tt"><a class="el" href="a00169.html#ga03fd4a16d8b9ee1ffc32b25fd2f6bdfa" title="Allocate a topology context.">hwloc_topology_init()</a>, <a class="el" href="a00169.html#gabdf58d87ad77f6615fccdfe0535ff826" title="Build the actual topology.">hwloc_topology_load()</a>, |
| 54 |
<a class="el" href="a00169.html#ga9f34a640b6fd28d23699d4d084667b15" title="Terminate and free a topology context.">hwloc_topology_destroy()</a></span> (see <a class="el" href="a00169.html">Topology Creation and Destruction</a>) imply major modifications of the structure, including freeing some objects. No other thread cannot access the topology or any of its objects at the same time.</p> |
| 55 |
<p class="interdd">Also references to objects inside the topology are not valid anymore after these functions return. </p> |
| 56 |
<p class="enddd"></p> |
| 57 |
</dd> |
| 58 |
<dt>Runtime topology modifications </dt> |
| 59 |
<dd><p class="startdd"><span class="tt"><a class="el" href="a00177.html#gad980782ade737900c5cf208946768c30" title="Add a MISC object as a leaf of the topology.">hwloc_topology_insert_misc_object()</a></span>, <span class="tt"><a class="el" href="a00177.html#ga4cea4741165faf5323931a9ed8786ef7" title="Allocate a Group object to insert later with hwloc_topology_insert_group_object().">hwloc_topology_alloc_group_object()</a></span>, and <span class="tt"><a class="el" href="a00177.html#ga1fc6012b3e1c249b83f48cb7bcacaa5b" title="Add more structure to the topology by adding an intermediate Group.">hwloc_topology_insert_group_object()</a></span> (see <a class="el" href="a00177.html">Modifying a loaded Topology</a>) may modify the topology significantly by adding objects inside the tree, changing the topology depth, etc.</p> |
| 60 |
<p class="interdd"><span class="tt"><a class="el" href="a00193.html#ga1f5a29760ebb8f9af831e645315f553c" title="Commit a new distances structure.">hwloc_distances_add_commit()</a></span> and <span class="tt"><a class="el" href="a00194.html#gac188d9b64d9560255ce5f6d0a20f9c0a" title="Remove all distance matrices from a topology.">hwloc_distances_remove()</a></span> (see <a class="el" href="a00193.html">Add distances between objects</a>) modify the list of distance structures in the topology, and the former may even insert new Group objects.</p> |
| 61 |
<p class="interdd"><span class="tt"><a class="el" href="a00196.html#ga770657d1e44b09e93e09f623936c1e5f" title="Register a new memory attribute.">hwloc_memattr_register()</a></span> and <span class="tt"><a class="el" href="a00196.html#ga960529c08b25cf15825e0f72ecceb504" title="Set an attribute value for a specific target NUMA node.">hwloc_memattr_set_value()</a></span> (see <a class="el" href="a00196.html">Managing memory attributes</a>) modify the memory attributes of the topology.</p> |
| 62 |
<p class="interdd"><span class="tt"><a class="el" href="a00177.html#ga6db81ed13ac0a9d70cc80372ab537815" title="Restrict the topology to the given CPU set or nodeset.">hwloc_topology_restrict()</a></span> modifies the topology even more dramatically by removing some objects.</p> |
| 63 |
<p class="interdd"><span class="tt"><a class="el" href="a00177.html#ga698ecd640d2b76742bba3829a145cd9a" title="Refresh internal structures after topology modification.">hwloc_topology_refresh()</a></span> updates some internal cached structures. (see below).</p> |
| 64 |
<p class="interdd">Although references to former objects <em>may</em> still be valid after insertion or restriction, it is strongly advised to not rely on any such guarantee and always re-consult the topology to reacquire new instances of objects. </p> |
| 65 |
<p class="enddd"></p> |
| 66 |
</dd> |
| 67 |
<dt>Consulting distances </dt> |
| 68 |
<dd><p class="startdd"><span class="tt"><a class="el" href="a00191.html#ga613e6b2a5d0f06626ee8d0c12fa46691" title="Retrieve distance matrices.">hwloc_distances_get()</a></span> and its variants are thread-safe except if the topology was recently modified (because distances may involve objects that were removed).</p> |
| 69 |
<p class="interdd">Whenever the topology is modified (see above), <span class="tt"><a class="el" href="a00177.html#ga698ecd640d2b76742bba3829a145cd9a" title="Refresh internal structures after topology modification.">hwloc_topology_refresh()</a></span> should be called in the same thread-safe context to force the refresh of internal distances structures. A call to <span class="tt"><a class="el" href="a00191.html#ga613e6b2a5d0f06626ee8d0c12fa46691" title="Retrieve distance matrices.">hwloc_distances_get()</a></span> may also refresh distances-related structures.</p> |
| 70 |
<p class="interdd">Once this refresh has been performed, multiple <span class="tt"><a class="el" href="a00191.html#ga613e6b2a5d0f06626ee8d0c12fa46691" title="Retrieve distance matrices.">hwloc_distances_get()</a></span> may then be performed concurrently by multiple threads. </p> |
| 71 |
<p class="enddd"></p> |
| 72 |
</dd> |
| 73 |
<dt>Consulting memory attributes </dt> |
| 74 |
<dd><p class="startdd">Functions consulting memory attributes in <a class="el" href="a00110_source.html">hwloc/memattrs.h</a> are thread-safe except if the topology was recently modified (because memory attributes may involve objects that were removed).</p> |
| 75 |
<p class="interdd">Whenever the topology is modified (see above), <span class="tt"><a class="el" href="a00177.html#ga698ecd640d2b76742bba3829a145cd9a" title="Refresh internal structures after topology modification.">hwloc_topology_refresh()</a></span> should be called in the same thread-safe context to force the refresh of internal memory attribute structures. A call to <span class="tt"><a class="el" href="a00195.html#ga297e4a9adc2272446a4c7449dacef0df" title="Return an attribute value for a specific target NUMA node.">hwloc_memattr_get_value()</a></span> or <span class="tt"><a class="el" href="a00195.html#gac8c06c1c89ab352c5db8e04a75033513" title="Return the target NUMA nodes that have some values for a given attribute.">hwloc_memattr_get_targets()</a></span> may also refresh internal structures for a given memory attribute.</p> |
| 76 |
<p class="interdd">Once this refresh has been performed, multiple functions consulting memory attributes may then be performed concurrently by multiple threads. </p> |
| 77 |
<p class="enddd"></p> |
| 78 |
</dd> |
| 79 |
<dt>Locating topologies </dt> |
| 80 |
<dd><p class="startdd"><span class="tt">hwloc_topology_set_*</span> (see <a class="el" href="a00176.html">Topology Detection Configuration and Query</a>) do not modify the topology directly, but they do modify internal structures describing the behavior of the upcoming invocation of <span class="tt"><a class="el" href="a00169.html#gabdf58d87ad77f6615fccdfe0535ff826" title="Build the actual topology.">hwloc_topology_load()</a></span>. Hence, all of these functions should not be used concurrently. </p> |
| 81 |
<p class="enddd"></p> |
| 82 |
</dd> |
| 83 |
</dl> |
| 84 |
</div></div><!-- contents --> |
| 85 |
</div><!-- PageDoc --> |
| 86 |
<!-- start footer part --> |
| 87 |
<hr class="footer"/><address class="footer"><small> |
| 88 |
Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.15.0 |
| 89 |
</small></address> |
| 90 |
</div><!-- doc-content --> |
| 91 |
</body> |
| 92 |
</html> |