Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/consumer_8c_source.html
author Dominic Pinkman <dominic.pinkman@nokia.com>
Fri, 13 Aug 2010 16:47:46 +0100
changeset 14 578be2adaf3e
parent 6 43e37759235e
permissions -rw-r--r--
Week 32 contribution of PDK documentation content. See release notes for details. Fixes bug Bug 3582

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>TB9.2 Example Applications: examples/PIPS/opencproducerconsumerex/src/consumer.c Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.2 -->
<h1>examples/PIPS/opencproducerconsumerex/src/consumer.c</h1><a href="consumer_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00015"></a>00015 <span class="comment">/* INCLUDE FILES */</span>
<a name="l00016"></a>00016 <span class="preprocessor">#include &lt;sys/types.h&gt;</span>
<a name="l00017"></a>00017 <span class="preprocessor">#include &lt;sys/ipc.h&gt;</span>
<a name="l00018"></a>00018 <span class="preprocessor">#include &lt;sys/msg.h&gt;</span>
<a name="l00019"></a>00019 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<a name="l00020"></a>00020 
<a name="l00021"></a>00021 <span class="preprocessor">#include &quot;CommanHeader.h&quot;</span>
<a name="l00022"></a>00022 
<a name="l00023"></a>00023 <span class="comment">/*****************************************************************************</span>
<a name="l00024"></a>00024 <span class="comment">*  ConsumerThreadEntryPoint</span>
<a name="l00025"></a>00025 <span class="comment">*  Function: Consumer Thread that consumes as amny as needed items when </span>
<a name="l00026"></a>00026 <span class="comment">*  Producer has enough items produced</span>
<a name="l00027"></a>00027 <span class="comment">*  It also informs the Observer thread about item consumed</span>
<a name="l00028"></a>00028 <span class="comment">*******************************************************************************/</span>
<a name="l00029"></a>00029 
<a name="l00030"></a><a class="code" href="consumer_8c.html#aeb7daec86aaf17cff923e4e69e1ca964">00030</a> <span class="keywordtype">void</span>* <a class="code" href="consumer_8c.html#aeb7daec86aaf17cff923e4e69e1ca964">ConsumerThreadEntryPoint</a>( <span class="keywordtype">void</span>* aParam ) 
<a name="l00031"></a>00031 {
<a name="l00032"></a>00032         <a class="code" href="struct_thread_param.html">ThreadParam</a>* args = (<a class="code" href="struct_thread_param.html">ThreadParam</a>*) aParam;
<a name="l00033"></a>00033         <span class="keywordtype">int</span> yetToConsume = args-&gt;noOfItems;
<a name="l00034"></a>00034         <span class="keywordtype">int</span> clientId = args-&gt;noOfConsumers;
<a name="l00035"></a>00035         <a class="code" href="struct_produced_item.html">ProducedItem</a>* consumeItem;
<a name="l00036"></a>00036         <span class="keywordtype">int</span> ret = 0;
<a name="l00037"></a>00037         key_t msgQFd = -1;
<a name="l00038"></a>00038 
<a name="l00039"></a>00039         <span class="comment">/* Construct the message to be send thru msg q */</span>
<a name="l00040"></a>00040         <span class="keyword">struct </span>msgbuf* sendMsg = (<span class="keyword">struct </span>msgbuf*)malloc(KMAXSENDMSG);
<a name="l00041"></a>00041         sendMsg-&gt;mtype = 1;
<a name="l00042"></a>00042         
<a name="l00043"></a>00043         <span class="comment">/* Get the Handler to Observer Msg Q */</span>
<a name="l00044"></a>00044         msgQFd = msgget(KMSGQKEY, IPC_CREAT);
<a name="l00045"></a>00045 
<a name="l00046"></a>00046         <span class="keywordflow">while</span> (yetToConsume != 0) 
<a name="l00047"></a>00047         {
<a name="l00048"></a>00048                 <span class="comment">/* Acquire the Lock before checking for items available from Producer */</span>
<a name="l00049"></a>00049                 sem_wait(&amp;args-&gt;itemLock);
<a name="l00050"></a>00050                 <span class="comment">/* Pop item from Producved Item-Stack */</span>
<a name="l00051"></a>00051                 consumeItem = <a class="code" href="stack_8cpp.html#aaf42677487a6b410248a6c4fef8979fd">PopFromStack</a>();
<a name="l00052"></a>00052                 <span class="keywordflow">if</span> (consumeItem == NULL) 
<a name="l00053"></a>00053                 {
<a name="l00054"></a>00054                         <span class="comment">/* No items in the Stack */</span>
<a name="l00055"></a>00055                         sem_post(&amp;args-&gt;itemLock);
<a name="l00056"></a>00056                         <span class="keywordflow">continue</span>;
<a name="l00057"></a>00057                 }
<a name="l00058"></a>00058                 yetToConsume--;
<a name="l00059"></a>00059                 <span class="comment">/* Inform the Observer about Item Consumption */</span>
<a name="l00060"></a>00060                 sprintf(&amp;sendMsg-&gt;mtext, <span class="stringliteral">&quot; Consumer#%d Consuming Produced %s\0&quot;</span>, clientId, consumeItem-&gt;itemName);
<a name="l00061"></a>00061                 ret = msgsnd(msgQFd, sendMsg, strlen(sendMsg-&gt;mtext)+4, 0);
<a name="l00062"></a>00062 
<a name="l00063"></a>00063                 <span class="comment">/* Release the Lock once after getting an item */</span>
<a name="l00064"></a>00064                 sem_post(&amp;args-&gt;itemLock);
<a name="l00065"></a>00065                 <a class="code" href="stack_8cpp.html#ac0da5b0fd0c1fbbee12568b2cfbf5354">FreeItem</a>(consumeItem);
<a name="l00066"></a>00066         }
<a name="l00067"></a>00067 
<a name="l00068"></a>00068         free( sendMsg );
<a name="l00069"></a>00069         <span class="keywordflow">return</span> (<span class="keywordtype">int</span>*)0;
<a name="l00070"></a>00070 }
<a name="l00071"></a>00071 
<a name="l00072"></a>00072 
<a name="l00073"></a>00073 <span class="comment">/*  End of File */</span>
</pre></div></div>
<hr size="1"/><address style="text-align: right;"><small>Generated by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2 </small></address>
</body>
</html>