<div dir="ltr">But if the state is anything other than TASK_RUNNING, schedule() is attempts to remove the task off run queue.  <div>Please see below (implementation of __schedule, called by schedule()) - </div><div><pre style="font-family:Monaco,&#39;Courier New&#39;,Courier,monospace;font-size:14px;padding:1em;color:rgb(120,120,120);overflow:auto;font-weight:bold">
<a name="L2671" href="http://lxr.free-electrons.com/source/kernel/sched/core.c?v=3.14#L2671" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">2671</a>         <b><i>/*</i></b>
<a name="L2672" href="http://lxr.free-electrons.com/source/kernel/sched/core.c?v=3.14#L2672" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">2672</a> <b><i>         * Make sure that signal_pending_state()-&gt;signal_pending() below</i></b>
<a name="L2673" href="http://lxr.free-electrons.com/source/kernel/sched/core.c?v=3.14#L2673" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">2673</a> <b><i>         * can&#39;t be reordered with __set_current_state(TASK_INTERRUPTIBLE)</i></b>
<a name="L2674" href="http://lxr.free-electrons.com/source/kernel/sched/core.c?v=3.14#L2674" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">2674</a> <b><i>         * done by the caller to avoid the race with signal_wake_up().</i></b>
<a name="L2675" href="http://lxr.free-electrons.com/source/kernel/sched/core.c?v=3.14#L2675" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">2675</a> <b><i>         */</i></b>
<a name="L2676" href="http://lxr.free-electrons.com/source/kernel/sched/core.c?v=3.14#L2676" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">2676</a>         <a href="http://lxr.free-electrons.com/ident?v=3.14;i=smp_mb__before_spinlock" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">smp_mb__before_spinlock</a>();
<a name="L2677" href="http://lxr.free-electrons.com/source/kernel/sched/core.c?v=3.14#L2677" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">2677</a>         <a href="http://lxr.free-electrons.com/ident?v=3.14;i=raw_spin_lock_irq" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">raw_spin_lock_irq</a>(&amp;<a href="http://lxr.free-electrons.com/ident?v=3.14;i=rq" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black;background-image:none;background-repeat:initial">rq</a>-&gt;<a href="http://lxr.free-electrons.com/ident?v=3.14;i=lock" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">lock</a>);
<a name="L2678" href="http://lxr.free-electrons.com/source/kernel/sched/core.c?v=3.14#L2678" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">2678</a> 
<a name="L2679" href="http://lxr.free-electrons.com/source/kernel/sched/core.c?v=3.14#L2679" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">2679</a>         switch_count = &amp;prev-&gt;nivcsw;
<a name="L2680" href="http://lxr.free-electrons.com/source/kernel/sched/core.c?v=3.14#L2680" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">2680</a>         if (prev-&gt;<a href="http://lxr.free-electrons.com/ident?v=3.14;i=state" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">state</a> &amp;&amp; !(<a href="http://lxr.free-electrons.com/ident?v=3.14;i=preempt_count" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">preempt_count</a>() &amp; <a href="http://lxr.free-electrons.com/ident?v=3.14;i=PREEMPT_ACTIVE" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">PREEMPT_ACTIVE</a>)) {
<a name="L2681" href="http://lxr.free-electrons.com/source/kernel/sched/core.c?v=3.14#L2681" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">2681</a>                 if (<a href="http://lxr.free-electrons.com/ident?v=3.14;i=unlikely" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">unlikely</a>(<a href="http://lxr.free-electrons.com/ident?v=3.14;i=signal_pending_state" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">signal_pending_state</a>(prev-&gt;<a href="http://lxr.free-electrons.com/ident?v=3.14;i=state" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">state</a>, prev))) {
<a name="L2682" href="http://lxr.free-electrons.com/source/kernel/sched/core.c?v=3.14#L2682" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">2682</a>                         prev-&gt;<a href="http://lxr.free-electrons.com/ident?v=3.14;i=state" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">state</a> = <a href="http://lxr.free-electrons.com/ident?v=3.14;i=TASK_RUNNING" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">TASK_RUNNING</a>;
<a name="L2683" href="http://lxr.free-electrons.com/source/kernel/sched/core.c?v=3.14#L2683" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">2683</a>                 } else {
<a name="L2684" href="http://lxr.free-electrons.com/source/kernel/sched/core.c?v=3.14#L2684" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">2684</a>                         <a href="http://lxr.free-electrons.com/ident?v=3.14;i=deactivate_task" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">deactivate_task</a>(<a href="http://lxr.free-electrons.com/ident?v=3.14;i=rq" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">rq</a>, prev, <a href="http://lxr.free-electrons.com/ident?v=3.14;i=DEQUEUE_SLEEP" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">DEQUEUE_SLEEP</a>);
<a name="L2685" href="http://lxr.free-electrons.com/source/kernel/sched/core.c?v=3.14#L2685" style="text-decoration:none;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:rgb(153,153,153);color:black">2685</a>                         prev-&gt;on_rq = 0;</pre>
<pre style="padding:1em;overflow:auto"><span style="color:rgb(34,34,34);font-family:arial;font-size:small;font-weight:normal;white-space:normal">At line 2684 task is removed. So it will be suspended.  However,as per line 2681, there  seems to be a case when it may not put a task to sleep. But I am not sure when that can happen.  Also comment at line 2672 talks about preventing a race-condition in the caller. I did not understand that. In the code-snippet(in my first post) I did not do anything special in caller to avoid any race condition!</span></pre>
</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Jun 12, 2014 at 12:15 AM, Jeff Haran <span dir="ltr">&lt;<a href="mailto:Jeff.Haran@citrix.com" target="_blank">Jeff.Haran@citrix.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">





<div lang="EN-US" link="blue" vlink="purple">
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d"><u></u> <u></u></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #b5c4df 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">From:</span></b><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;"> <a href="mailto:kernelnewbies-bounces@kernelnewbies.org" target="_blank">kernelnewbies-bounces@kernelnewbies.org</a> [mailto:<a href="mailto:kernelnewbies-bounces@kernelnewbies.org" target="_blank">kernelnewbies-bounces@kernelnewbies.org</a>]
<b>On Behalf Of </b>Joshi<br>
<b>Sent:</b> Wednesday, June 11, 2014 11:33 AM<br>
<b>To:</b> <a href="mailto:Valdis.Kletnieks@vt.edu" target="_blank">Valdis.Kletnieks@vt.edu</a><br>
<b>Cc:</b> <a href="mailto:kernelnewbies@kernelnewbies.org" target="_blank">kernelnewbies@kernelnewbies.org</a><br>
<b>Subject:</b> Re: Putting a thread to sleep using schedule() is foolproof?<u></u><u></u></span></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div><div class="">
<p class="MsoNormal">Is not changing the state to TASK_INTERRUPTIBLE before calling schedule() renders it not-schedulable? I thought task would be removed off run-queue by schedule() because it is in TASK_INTERRUPTIBLE state. <u></u><u></u></p>

<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I was making a sort of task queue. Threads puts themselves into this queue when a resource(say memory-space) is not available. And other thread, post releasing the memory space, will wake the first thread in queue.<u></u><u></u></p>

</div>
<div>
<p class="MsoNormal">There are other means to achieve that, but I wondered why the above mentioned method did not succeed in making thread sleep.<u></u><u></u></p>
</div>
</div><div>
<p class="MsoNormal"><span style="color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d">I think TASK_INTERRUPTIBLE has to do with whether or not a task can be “interrupted” by a signal, as in wait_event_interruptible().<span class="HOEnZb"><font color="#888888"><u></u><u></u></font></span></span></p>
<span class="HOEnZb"><font color="#888888">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d">Jeff Haran<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d"><u></u> <u></u></span></p>
</font></span></div>
</div>
</div>
</div>
</div>

</blockquote></div><br><br clear="all"><div><br></div>-- <br>Joshi<br>
</div>