<p><a href="https://github.com/charlesrchance" class="user-mention">@charlesrchance</a> - a solution is to use a shared memory variable to say if something needs to be consumed, like:</p>

<pre><code>// mod init
int *dmq_usrloc_tasks = shm_malloc(sizeof(int));
*dmq_usrloc_tasks = 0;
gen_lock_t *dmq_usrloc_tasks_lock = ... // usual lock initialization

// runtime - producer
lock_get(dmq_usrloc_tasks_lock);
*dmq_usrloc_tasks++
lock_release(dmq_usrloc_tasks_lock);

// runtime - consumer
while (1) {
  lock_get(dmq_usrloc_tasks_lock);
  while(*dmq_usrloc_tasks>0) *dmq_usrloc_tasks++;
  lock_release(dmq_usrloc_tasks_lock);
  sleep_us(dmq_usrloc_tasks_sleep);
}
</code></pre>

<p>The incrementing/decrementing needs to be accompanied by producing/consuming the tasks.</p>

<p>The key here is the sleep_us(), where dmq_usrloc_tasks_sleep can be a new mod param to specify the miliseconds (or microseconds) to sleep. It proved that triggering a sleep takes the cpu from process and cpu load is kept low. One can tune its value to suit better its environment.</p>

<p>IIRC, this is used by maybe in async module for async_route().</p>

<p>An alternative is using in-memory sockets to pass tasks from producers to consumers. The consumer should be blocked in a read() and that should not consume cpu, although on some systems I got read() returning fast with error, ending in a loop of fast reads and high cpu again. This should be used by evapi to pass events between sip workers and evapi worker.</p>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/kamailio/kamailio/issues/822#issuecomment-258382642">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AF36ZU2ez6_U4GcKHK_vbgHD5dAkmYXvks5q6vwqgaJpZM4KWtZY">mute the thread</a>.<img alt="" height="1" src="https://github.com/notifications/beacon/AF36Zay6ZmAbSbFqeTOcrsHMcVRjqAWhks5q6vwqgaJpZM4KWtZY.gif" width="1" /></p>
<div itemscope itemtype="http://schema.org/EmailMessage">
<div itemprop="action" itemscope itemtype="http://schema.org/ViewAction">
  <link itemprop="url" href="https://github.com/kamailio/kamailio/issues/822#issuecomment-258382642"></link>
  <meta itemprop="name" content="View Issue"></meta>
</div>
<meta itemprop="description" content="View this Issue on GitHub"></meta>
</div>

<script type="application/json" data-scope="inboxmarkup">{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/kamailio/kamailio","title":"kamailio/kamailio","subtitle":"GitHub repository","main_image_url":"https://cloud.githubusercontent.com/assets/143418/17495839/a5054eac-5d88-11e6-95fc-7290892c7bb5.png","avatar_image_url":"https://cloud.githubusercontent.com/assets/143418/15842166/7c72db34-2c0b-11e6-9aed-b52498112777.png","action":{"name":"Open in GitHub","url":"https://github.com/kamailio/kamailio"}},"updates":{"snippets":[{"icon":"PERSON","message":"@miconda in #822: @charlesrchance - a solution is to use a shared memory variable to say if something needs to be consumed, like:\r\n\r\n```\r\n// mod init\r\nint *dmq_usrloc_tasks = shm_malloc(sizeof(int));\r\n*dmq_usrloc_tasks = 0;\r\ngen_lock_t *dmq_usrloc_tasks_lock = ... // usual lock initialization\r\n\r\n// runtime - producer\r\nlock_get(dmq_usrloc_tasks_lock);\r\n*dmq_usrloc_tasks++\r\nlock_release(dmq_usrloc_tasks_lock);\r\n\r\n// runtime - consumer\r\nwhile (1) {\r\n  lock_get(dmq_usrloc_tasks_lock);\r\n  while(*dmq_usrloc_tasks\u003e0) *dmq_usrloc_tasks++;\r\n  lock_release(dmq_usrloc_tasks_lock);\r\n  sleep_us(dmq_usrloc_tasks_sleep);\r\n}\r\n```\r\n\r\nThe incrementing/decrementing needs to be accompanied by producing/consuming the tasks.\r\n\r\nThe key here is the sleep_us(), where dmq_usrloc_tasks_sleep can be a new mod param to specify the miliseconds (or microseconds) to sleep. It proved that triggering a sleep takes the cpu from process and cpu load is kept low. One can tune its value to suit better its environment.\r\n\r\nIIRC, this is used by maybe in async module for async_route().\r\n\r\nAn alternative is using in-memory sockets to pass tasks from producers to consumers. The consumer should be blocked in a read() and that should not consume cpu, although on some systems I got read() returning fast with error, ending in a loop of fast reads and high cpu again. This should be used by evapi to pass events between sip workers and evapi worker."}],"action":{"name":"View Issue","url":"https://github.com/kamailio/kamailio/issues/822#issuecomment-258382642"}}}</script>