<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
I've tried lock-free queues way in the past, and the main problem is
there is no easy way to detect deadlocks. While that may be fine (and
avoidable) with a static design, the environment of OpenSim is not so
concrete. I suggest to limit the number of lock-free queues to static
usage.<br>
<br>
The implementation I found that worked well in dynamic environments is
to have one synchronous thread and several asynchronous worker threads.
The synchronous thread assigns tasks to the worker thread. Since the
synchronous thread has control of what tasks are being assigned, there
is no reason for it to lock while it processes data  between the the
worker thread and the main dataset. The only time the synchronous
thread waits is when there is either nothing to do or all the worker
threads are busy. With single core processors, this may seem like extra
overhead with the thread switches, but this implementation scales very
well with multiple processor cores.<br>
<br>
I've used fiber-like threads in the past, but these fiber threads don't
work well with C# due to the stack frame dependencies (i.e.
setjmp/longjmp is incompatible with C#).<br>
<br>
My two cents is that a lock-free queue can be used to assign tasks to a
main synchronous thread, which then controls the actual movement of the
data and the assignment of worker threads. The worker threads can
process data or wait for events. The contents of the lock-free queue
can be pointer to a function call or likewise. The lock-free queue in
the synchronous thread basically resembles a fiber-like task queue but
without the longjmp/setjmp contexts. This achieves the more static
usage of the lock-free queues and avoids the deadlock detection
overhead.<br>
<br>
Frisby, Adam wrote:
<blockquote
 cite="mid63FAD4F222230A4EA79DE9E8BE66473502D870DA@winxbeus19.exchange.xchg"
 type="cite">
  <meta http-equiv="Content-Type" content="text/html; ">
  <meta name="Generator" content="Microsoft Word 12 (filtered medium)">
  <style>
<!--
 /* Font Definitions */
 @font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        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;}
@page Section1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.Section1
        {page:Section1;}
-->
  </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="Section1">
  <p class="MsoNormal">I’ve recently discovered (and perhaps I should
have
known this earlier) it’s possible to create a reasonably high
performance
lock-free list/dictionary/stack/queue structure under C#.<o:p></o:p></p>
  <p class="MsoNormal"><o:p> </o:p></p>
  <p class="MsoNormal">Julian Bucknall has posted the sourcecode to a
bunch of
these here: <a href="http://www.boyet.com/articles/lockfreestack.html">http://www.boyet.com/articles/lockfreestack.html</a>
  <o:p></o:p></p>
  <p class="MsoNormal"><o:p> </o:p></p>
  <p class="MsoNormal">One of the big performance issues with OpenSim
is our
overreliance on locking data structures, which can cause issues when
those
locks don’t release in a timely manner. If we switched to lockless
structures – we could effectively eliminate those issues. (and probably
create new ones.)<o:p></o:p></p>
  <p class="MsoNormal"><o:p> </o:p></p>
  <p class="MsoNormal">Does anyone have any opposition if we try to
replace some of
the core structures with lock-free versions, and then removing the
locks on
them? I’m thinking Scene.Entities, Scene.Presences would be two good
targets
for the first lot, then maybe we can attack some deeper bits later.<o:p></o:p></p>
  <p class="MsoNormal"><o:p> </o:p></p>
  <p class="MsoNormal">Adam<o:p></o:p></p>
  </div>
  <pre wrap="">
<hr size="4" width="90%">
_______________________________________________
Opensim-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Opensim-dev@lists.berlios.de">Opensim-dev@lists.berlios.de</a>
<a class="moz-txt-link-freetext" href="https://lists.berlios.de/mailman/listinfo/opensim-dev">https://lists.berlios.de/mailman/listinfo/opensim-dev</a>
  </pre>
</blockquote>
<br>
</body>
</html>