]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ipc/ipcdev.git/blob - qnx/src/api/Ipc.c
Linux/Qnx: MessageQ msg cannot be freed if put fails
[ipc/ipcdev.git] / qnx / src / api / Ipc.c
1 /*
2  * Copyright (c) 2013, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*!
33  *  @file       Ipc.c
34  *
35  *  @brief      Starts and stops user side Ipc
36  *              All setup/destroy APIs on user side will be call from this
37  *              module.
38  *
39  *  @ver        0002
40  *
41  */
43 /* Standard headers */
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <signal.h>
47 #include <ti/ipc/Std.h>
49 /* Common IPC headers: */
50 #include <ti/ipc/Ipc.h>
51 #include <ti/ipc/NameServer.h>
53 /* User side headers */
54 #include <ti/syslink/inc/usr/Qnx/IpcDrv.h>
56 /* IPC startup/shutdown stuff: */
57 #include <ti/ipc/MultiProc.h>
58 #include <ti/ipc/MessageQ.h>
59 #include <ti/ipc/NameServer.h>
60 #include <_MessageQ.h>
61 #include <_NameServer.h>
62 #include <_GateMP.h>
63 #include <_GateMP_usr.h>
64 #include <ti/syslink/inc/GateHWSpinlock.h>
65 #include <ti/syslink/inc/_MultiProc.h>
67 MultiProc_Config _MultiProc_cfg;
69 static void cleanup(int arg);
71 /** ============================================================================
72  *  Functions
73  *  ============================================================================
74  */
75 /* Function to start Ipc */
76 Int Ipc_start (Void)
77 {
78     MessageQ_Config   msgqCfg;
79     MultiProc_Config  mpCfg;
80     Int32             status = Ipc_S_SUCCESS;
81     UInt16            rprocId;
83     /* Catch ctrl-C, and cleanup: */
84     (void) signal(SIGINT, cleanup);
86     status = IpcDrv_open();
87     if (status < 0) {
88         printf("Ipc_start: IpcDrv_open() failed: %d\n", status);
89         status = Ipc_E_FAIL;
90         goto exit;
91     }
93     status = NameServer_setup();
94     if (status >= 0) {
95         MessageQ_getConfig(&msgqCfg);
96         MessageQ_setup(&msgqCfg);
98         /* Setup and get MultiProc configuration from resource manager */
99         MultiProc_getConfig(&mpCfg);
100         _MultiProc_cfg = mpCfg;
102         /* Now attach to all remote processors, assuming they are up. */
103         for (rprocId = 0;
104              (rprocId < MultiProc_getNumProcessors()) && (status >= 0);
105              rprocId++) {
106            if (0 == rprocId) {
107                /* Skip host, which should always be 0th entry. */
108                continue;
109            }
110            status = MessageQ_attach (rprocId, NULL);
111            if (status < 0) {
112               printf("Ipc_start: MessageQ_attach(%d) failed: %d\n",
113                      rprocId, status);
114               status = Ipc_E_FAIL;
115               goto messageqattach_fail;
116            }
117         }
118     }
119     else {
120         printf("Ipc_start: NameServer_setup() failed: %d\n", status);
121         status = Ipc_E_FAIL;
122         goto nameserversetup_fail;
123     }
125     /* Start GateMP only if it is setup in the resource manager */
126 #if defined(GATEMP_SUPPORT)
127     if (GateMP_isSetup()) {
128         status = GateHWSpinlock_start();
129         if (status < 0) {
130             printf("Ipc_start: GateHWSpinlock_start failed: %d\n",
131                 status);
132             status = Ipc_E_FAIL;
133             goto gatehwspinlockstart_fail;
134         }
135         else {
136             status = GateMP_start();
137             if (status < 0) {
138                 printf("Ipc_start: GateMP_start failed: %d\n",
139                 status);
140                 status = Ipc_E_FAIL;
141                 goto gatempstart_fail;
142             }
143         }
144     }
145 #endif
146     /* Success */
147     goto exit;
148 #if defined(GATEMP_SUPPORT)
149 gatempstart_fail:
150     GateHWSpinlock_stop();
151 gatehwspinlockstart_fail:
152     for (rprocId = rprocId - 1; (rprocId > 0) && (status >= 0); rprocId--) {
153        MessageQ_detach(rprocId);
154     }
155 #endif
156 messageqattach_fail:
157     MessageQ_destroy();
158     NameServer_destroy();
159 nameserversetup_fail:
160     IpcDrv_close();
162 exit:
163     return (status);
167 /* Function to stop Ipc */
168 Int Ipc_stop (Void)
170     Int32             status = Ipc_S_SUCCESS;
171     UInt16            rprocId;
172 #if defined(GATEMP_SUPPORT)
173     if (GateMP_isSetup()) {
174         /* Stop GateMP */
175         status = GateMP_stop();
176         if (status < 0) {
177             printf("Ipc_stop: GateMP_stop() failed: %d\n", status);
178             status = Ipc_E_FAIL;
179             goto exit;
180         }
182         /* Finalize GateHWSpinlock */
183         status = GateHWSpinlock_stop();
184         if (status < 0) {
185             printf("Ipc_stop: GateHWSpinlock_stop() failed: %d\n", status);
186             status = Ipc_E_FAIL;
187             goto exit;
188         }
189     }
190 #endif
191     /* Now detach from all remote processors, assuming they are up. */
192     for (rprocId = 0;
193          (rprocId < MultiProc_getNumProcessors()) && (status >= 0);
194          rprocId++) {
195        if (0 == rprocId) {
196           /* Skip host, which should always be 0th entry. */
197           continue;
198        }
199        status = MessageQ_detach(rprocId);
200        if (status < 0) {
201             printf("Ipc_stop: MessageQ_detach(%d) failed: %d\n",
202                  rprocId, status);
203             status = Ipc_E_FAIL;
204             goto exit;
205        }
206     }
208     status = MessageQ_destroy();
209     if (status < 0) {
210         printf("Ipc_stop: MessageQ_destroy() failed: %d\n", status);
211         status = Ipc_E_FAIL;
212         goto exit;
213     }
215     status = NameServer_destroy();
216     if (status < 0) {
217         printf("Ipc_stop: NameServer_destroy() failed: %d\n", status);
218         status = Ipc_E_FAIL;
219         goto exit;
220     }
222     IpcDrv_close();
224 exit:
225     return (status);
228 static void cleanup(int arg)
230     printf("Ipc: Caught SIGINT, calling Ipc_stop...\n");
231     Ipc_stop();
232     exit(0);