<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hello Matthieu,</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Thanks for your question!</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
> Is there a way of using an Argobots condition variable with an rwlock instead of a mutex?<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
No. The user cannot combine ABT_rwlock with ABT_cond.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
> my use-case is a structure that receives many read requests and a few writes, clearly justifying using a rwlock instead of a mutex, but I may want some of the reads to block until a write has happened, which means I need a condition variable
<div><br>
</div>
<div>First, if readers encounter an rwlock locked by a writer, they will suspend until the writer releases the rwlock (if --disable-simple-mutex is set, which is not set by default).</div>
<div><br>
</div>
<div>For this specific purpose(especially if "some", not "all" of the readers should block), what I came up with in my mind first is the following.  It looks fine except for a seemingly complex structure that uses multiple synchronization objects.</div>
<div>
<div><br>
</div>
<div>void reader() {</div>
<div>  while (1) {</div>
<div>    if (work_queue.is_empty() and I_AM_SOME_OF_READERS()) {</div>
<div>      // 1. Internally ABT_self_suspend()-like mechanism needs</div>
<div>      //    to take a lock (even if it is in the readers' lock)</div>
<div>      //    since multiple readers might access the same data</div>
<div>      //    structure (for example, a user-maintained</div>
<div>      //    suspended ULT list).</div>
<div>      // 2. Anyway this path is not performance sensitive</div>
<div>      ABT_mutex_lock(mutex);</div>
<div>      ABT_cond_wait(cond, mutex);</div>
<div>      ABT_mutex_unlock(mutex);</div>
<div>      // Now someone woke me up after pushing work.</div>
<div>    }</div>
<div>    ABT_rwlock_rdlock(rwlock);</div>
<div>    if (!work_queue.is_empty())</div>
<div>      ; // Do real work.</div>
<div>    ABT_rwlock_unlock(rwlock);</div>
<div>  }</div>
<div>}</div>
<div><br>
</div>
<div>void writer() {</div>
<div>  ABT_rwlock_wrlock(rwlock);</div>
<div>  work_queue.push_work(work);</div>
<div>  // You do not need to take a mutex to call ABT_cond_broadcast.</div>
<div>  // It is fine even if there's no waiter.</div>
<div>  ABT_cond_broadcast(cond);</div>
<div>  ABT_rwlock_unlock(rwlock);</div>
<span>}</span><br>
</div>
<div><br>
</div>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span>I would welcome any suggestions! (For example, does ABT_rwlock_trywrlock() help?)<br>
</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span><br>
</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span>Best,</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span>Shintaro</span></div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Dorier, Matthieu via discuss <discuss@lists.argobots.org><br>
<b>Sent:</b> Friday, July 23, 2021 5:03 AM<br>
<b>To:</b> discuss@argobots.org <discuss@argobots.org><br>
<b>Cc:</b> Dorier, Matthieu <mdorier@anl.gov><br>
<b>Subject:</b> [argobots-discuss] condition variable and rwlock</font>
<div> </div>
</div>
<style>
<!--
@font-face
        {font-family:"Cambria Math"}
@font-face
        {font-family:Calibri}
p.x_MsoNormal, li.x_MsoNormal, div.x_MsoNormal
        {margin:0cm;
        font-size:12.0pt;
        font-family:"Calibri",sans-serif}
span.x_EmailStyle17
        {font-family:"Calibri",sans-serif;
        color:windowtext}
.x_MsoChpDefault
        {font-size:12.0pt;
        font-family:"Calibri",sans-serif}
@page WordSection1
        {margin:72.0pt 72.0pt 72.0pt 72.0pt}
div.x_WordSection1
        {}
-->
</style>
<div lang="EN-GB" link="#0563C1" vlink="#954F72" style="word-wrap:break-word">
<div class="x_WordSection1">
<p class="x_MsoNormal"><span lang="EN-US" style="font-size:11.0pt">Hi,</span></p>
<p class="x_MsoNormal"><span lang="EN-US" style="font-size:11.0pt"> </span></p>
<p class="x_MsoNormal"><span lang="EN-US" style="font-size:11.0pt">I suspect the answer is no, but is there a way of using an Argobots condition variable with an rwlock instead of a mutex?</span></p>
<p class="x_MsoNormal"><span lang="EN-US" style="font-size:11.0pt">(my use-case is a structure that receives many read requests and a few writes, clearly justifying using a rwlock instead of a mutex, but I may want some of the reads to block until a write has
 happened, which means I need a condition variable).</span></p>
<p class="x_MsoNormal"><span lang="EN-US" style="font-size:11.0pt">Thanks,</span></p>
<p class="x_MsoNormal"><span lang="EN-US" style="font-size:11.0pt"> </span></p>
<p class="x_MsoNormal"><span lang="EN-US" style="font-size:11.0pt">Matthieu </span>
</p>
</div>
</div>
</body>
</html>