Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/msgqcreate_8c_source.html
<!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/opencmessagequeuelibraryex/engine/src/msgqcreate.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/opencmessagequeuelibraryex/engine/src/msgqcreate.c</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001
<a name="l00010"></a>00010 <span class="preprocessor">#include <errno.h></span>
<a name="l00011"></a>00011 <span class="preprocessor">#include <sys/msg.h></span>
<a name="l00012"></a>00012 <span class="preprocessor">#include <sys/sem.h></span>
<a name="l00013"></a>00013 <span class="preprocessor">#include "<a class="code" href="msgqinternal_8h.html">MsgQInternal.h</a>"</span>
<a name="l00014"></a>00014
<a name="l00015"></a>00015
<a name="l00016"></a>00016 <span class="comment">/********************************************************************************</span>
<a name="l00017"></a>00017 <span class="comment">* MsgQCreate</span>
<a name="l00018"></a>00018 <span class="comment">* Description: Creates a message queue</span>
<a name="l00019"></a>00019 <span class="comment">* Inputs:</span>
<a name="l00020"></a>00020 <span class="comment">* qName : ULONG that represents the queue name</span>
<a name="l00021"></a>00021 <span class="comment">* maxMsgs : ULONG that represents maximum size of messages </span>
<a name="l00022"></a>00022 <span class="comment">*********************************************************************************/</span>
<a name="l00023"></a><a class="code" href="msgqlib_8h.html#acc58bf2bd8d3b904069d8d604ccea5b1">00023</a> EXPORT_C <span class="keywordtype">int</span> <a class="code" href="msgqlib_8h.html#acc58bf2bd8d3b904069d8d604ccea5b1">MsgQCreate</a>(ULONG qName, ULONG maxMsgs, ULONG qOptions, <span class="keywordtype">int</span>* err) {
<a name="l00024"></a>00024 <span class="keywordtype">int</span> qId;
<a name="l00025"></a>00025 <span class="keywordtype">int</span> semId;
<a name="l00026"></a>00026 <span class="keywordtype">int</span> semName ;
<a name="l00027"></a>00027 <span class="keyword">struct </span>msqid_ds qStatus ;
<a name="l00028"></a>00028 <span class="keyword">struct </span>semid_ds sStatus ;
<a name="l00029"></a>00029
<a name="l00030"></a>00030 <span class="keyword">union </span>semun {
<a name="l00031"></a>00031 <span class="keywordtype">int</span> val;
<a name="l00032"></a>00032 <span class="keyword">struct </span>semid_ds* buf;
<a name="l00033"></a>00033 ushort_t* array;
<a name="l00034"></a>00034 } arg ;
<a name="l00035"></a>00035
<a name="l00036"></a>00036
<a name="l00037"></a>00037 <span class="keywordtype">int</span> hashInstErr;
<a name="l00038"></a>00038
<a name="l00039"></a>00039 <span class="keywordflow">if</span> (MsgQTableLookup(qName) != NULL) {
<a name="l00040"></a>00040 *err = OK;
<a name="l00041"></a>00041 <span class="keywordflow">return</span> (OK);
<a name="l00042"></a>00042 }
<a name="l00043"></a>00043 <span class="keywordflow">else</span> {
<a name="l00044"></a>00044 <span class="keywordflow">if</span>((qOptions == MSG_Q_FIFO) || (qOptions == MSG_Q_PRIORITY)) {
<a name="l00045"></a>00045 <span class="comment">// Set msg queue options to FIFO and create the message queue</span>
<a name="l00046"></a>00046 qOptions= MSG_Q_FIFO ;
<a name="l00047"></a>00047 <span class="keywordflow">if</span>((qId = msgget((key_t) qName ,IPC_CREAT | 0666 | IPC_EXCL )) >=0 ) {
<a name="l00048"></a>00048 <span class="comment">// set msg queue parameter max # bytes in queue</span>
<a name="l00049"></a>00049 <span class="keywordflow">if</span>( msgctl(qId,IPC_STAT,&qStatus) == 0 )
<a name="l00050"></a>00050 <span class="keywordflow">if</span>( qStatus.msg_qbytes > (maxMsgs * MAX_MSG_LEN) ) {
<a name="l00051"></a>00051 qStatus.msg_qbytes = maxMsgs * MAX_MSG_LEN ;
<a name="l00052"></a>00052 <span class="keywordflow">if</span>( msgctl(qId,IPC_SET,&qStatus) < 0) {
<a name="l00053"></a>00053 <span class="comment">// delete message queue on error </span>
<a name="l00054"></a>00054 msgctl(qId,IPC_RMID,0);
<a name="l00055"></a>00055 *err = errno;
<a name="l00056"></a>00056 <span class="keywordflow">return</span>(ERROR);
<a name="l00057"></a>00057 }
<a name="l00058"></a>00058 }
<a name="l00059"></a>00059 <span class="comment">// create semaphore</span>
<a name="l00060"></a>00060 semName= (key_t) qName;
<a name="l00061"></a>00061 <span class="keywordflow">if</span>((semId = semget(semName, 1, IPC_CREAT | 0666 |IPC_EXCL )) >= 0 ) {
<a name="l00062"></a>00062 <span class="comment">// set the semaphore value</span>
<a name="l00063"></a>00063 arg.buf = &sStatus;
<a name="l00064"></a>00064 arg.val = 1;
<a name="l00065"></a>00065 semctl(semId , 0, SETVAL, arg) ;
<a name="l00066"></a>00066
<a name="l00067"></a>00067 <span class="comment">//install queue data in hash table</span>
<a name="l00068"></a>00068 <span class="keywordflow">if</span>(InstallMsqQTable(qName, qId, semId, &hashInstErr) == OK) {
<a name="l00069"></a>00069 AddToMsgQTable(qName);
<a name="l00070"></a>00070 *err = OK;
<a name="l00071"></a>00071 <span class="keywordflow">return</span> (OK);
<a name="l00072"></a>00072 }
<a name="l00073"></a>00073 <span class="keywordflow">else</span> {
<a name="l00074"></a>00074 <span class="comment">//delete semaphore on error</span>
<a name="l00075"></a>00075 semctl(semId,0,IPC_RMID,0) ;
<a name="l00076"></a>00076 *err = hashInstErr;
<a name="l00077"></a>00077 }
<a name="l00078"></a>00078 }
<a name="l00079"></a>00079 <span class="keywordflow">else</span> {
<a name="l00080"></a>00080 <span class="comment">// delete message queue on error</span>
<a name="l00081"></a>00081 msgctl(qId,IPC_RMID,0);
<a name="l00082"></a>00082 *err = errno;
<a name="l00083"></a>00083 }
<a name="l00084"></a>00084 }
<a name="l00085"></a>00085 <span class="keywordflow">else</span> {
<a name="l00086"></a>00086 *err = errno;
<a name="l00087"></a>00087 }
<a name="l00088"></a>00088 }
<a name="l00089"></a>00089 <span class="keywordflow">else</span>
<a name="l00090"></a>00090 *err = KMsgQLibParamErr;
<a name="l00091"></a>00091
<a name="l00092"></a>00092 <span class="keywordflow">return</span>(ERROR);
<a name="l00093"></a>00093 }
<a name="l00094"></a>00094 }
<a name="l00095"></a>00095
</pre></div></div>
<hr size="1"/><address style="text-align: right;"><small>Generated by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2 </small></address>
</body>
</html>