Updated to use external spawn.

Fork of simplelink_V2 by David Fletcher

Committer:
dflet
Date:
Sat Jun 06 13:48:29 2015 +0000
Revision:
1:9b68e650b3f6
Parent:
0:1a07906111ec
Oppps

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dflet 0:1a07906111ec 1 /*
dflet 0:1a07906111ec 2 * spawn.c - CC31xx/CC32xx Host Driver Implementation
dflet 0:1a07906111ec 3 *
dflet 0:1a07906111ec 4 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
dflet 0:1a07906111ec 5 *
dflet 0:1a07906111ec 6 *
dflet 0:1a07906111ec 7 * Redistribution and use in source and binary forms, with or without
dflet 0:1a07906111ec 8 * modification, are permitted provided that the following conditions
dflet 0:1a07906111ec 9 * are met:
dflet 0:1a07906111ec 10 *
dflet 0:1a07906111ec 11 * Redistributions of source code must retain the above copyright
dflet 0:1a07906111ec 12 * notice, this list of conditions and the following disclaimer.
dflet 0:1a07906111ec 13 *
dflet 0:1a07906111ec 14 * Redistributions in binary form must reproduce the above copyright
dflet 0:1a07906111ec 15 * notice, this list of conditions and the following disclaimer in the
dflet 0:1a07906111ec 16 * documentation and/or other materials provided with the
dflet 0:1a07906111ec 17 * distribution.
dflet 0:1a07906111ec 18 *
dflet 0:1a07906111ec 19 * Neither the name of Texas Instruments Incorporated nor the names of
dflet 0:1a07906111ec 20 * its contributors may be used to endorse or promote products derived
dflet 0:1a07906111ec 21 * from this software without specific prior written permission.
dflet 0:1a07906111ec 22 *
dflet 0:1a07906111ec 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
dflet 0:1a07906111ec 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
dflet 0:1a07906111ec 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
dflet 0:1a07906111ec 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
dflet 0:1a07906111ec 27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
dflet 0:1a07906111ec 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
dflet 0:1a07906111ec 29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
dflet 0:1a07906111ec 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
dflet 0:1a07906111ec 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
dflet 0:1a07906111ec 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
dflet 0:1a07906111ec 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
dflet 0:1a07906111ec 34 *
dflet 0:1a07906111ec 35 */
dflet 0:1a07906111ec 36
dflet 0:1a07906111ec 37
dflet 0:1a07906111ec 38
dflet 0:1a07906111ec 39 /*****************************************************************************/
dflet 0:1a07906111ec 40 /* Include files */
dflet 0:1a07906111ec 41 /*****************************************************************************/
dflet 0:1a07906111ec 42 #include "cc3100_simplelink.h"
dflet 0:1a07906111ec 43
dflet 0:1a07906111ec 44 #include "cc3100_spawn.h"
dflet 0:1a07906111ec 45
dflet 0:1a07906111ec 46 #if (defined (SL_PLATFORM_MULTI_THREADED)) && (!defined (SL_PLATFORM_EXTERNAL_SPAWN))
dflet 0:1a07906111ec 47
dflet 0:1a07906111ec 48 namespace mbed_cc3100 {
dflet 0:1a07906111ec 49
dflet 0:1a07906111ec 50 #define _SL_MAX_INTERNAL_SPAWN_ENTRIES 10
dflet 0:1a07906111ec 51
dflet 0:1a07906111ec 52
dflet 0:1a07906111ec 53 typedef struct _SlInternalSpawnEntry_t {
dflet 0:1a07906111ec 54 _SlSpawnEntryFunc_t pEntry;
dflet 0:1a07906111ec 55 void* pValue;
dflet 0:1a07906111ec 56 struct _SlInternalSpawnEntry_t* pNext;
dflet 0:1a07906111ec 57 } _SlInternalSpawnEntry_t;
dflet 0:1a07906111ec 58
dflet 0:1a07906111ec 59 typedef struct {
dflet 0:1a07906111ec 60 _SlInternalSpawnEntry_t SpawnEntries[_SL_MAX_INTERNAL_SPAWN_ENTRIES];
dflet 0:1a07906111ec 61 _SlInternalSpawnEntry_t* pFree;
dflet 0:1a07906111ec 62 _SlInternalSpawnEntry_t* pWaitForExe;
dflet 0:1a07906111ec 63 _SlInternalSpawnEntry_t* pLastInWaitList;
dflet 0:1a07906111ec 64 _SlSyncObj_t SyncObj;
dflet 0:1a07906111ec 65 _SlLockObj_t LockObj;
dflet 0:1a07906111ec 66 } _SlInternalSpawnCB_t;
dflet 0:1a07906111ec 67
dflet 0:1a07906111ec 68 _SlInternalSpawnCB_t g_SlInternalSpawnCB;
dflet 0:1a07906111ec 69
dflet 0:1a07906111ec 70 cc3100_spawn::cc3100_spawn()
dflet 0:1a07906111ec 71 {
dflet 0:1a07906111ec 72
dflet 0:1a07906111ec 73 }
dflet 0:1a07906111ec 74
dflet 0:1a07906111ec 75 cc3100_spawn::~cc3100_spawn()
dflet 0:1a07906111ec 76 {
dflet 0:1a07906111ec 77
dflet 0:1a07906111ec 78 }
dflet 0:1a07906111ec 79
dflet 0:1a07906111ec 80
dflet 0:1a07906111ec 81 void cc3100_spawn::_SlInternalSpawnTaskEntry()
dflet 0:1a07906111ec 82 {
dflet 0:1a07906111ec 83 int16_t i;
dflet 0:1a07906111ec 84 _SlInternalSpawnEntry_t* pEntry;
dflet 0:1a07906111ec 85 uint8_t LastEntry;
dflet 0:1a07906111ec 86
dflet 0:1a07906111ec 87 /* create and lock the locking object. lock in order to avoid race condition
dflet 0:1a07906111ec 88 on the first creation */
dflet 0:1a07906111ec 89 sl_LockObjCreate(&g_SlInternalSpawnCB.LockObj,"SlSpawnProtect");
dflet 0:1a07906111ec 90 sl_LockObjLock(&g_SlInternalSpawnCB.LockObj,SL_OS_NO_WAIT);
dflet 0:1a07906111ec 91
dflet 0:1a07906111ec 92 /* create and clear the sync object */
dflet 0:1a07906111ec 93 sl_SyncObjCreate(&g_SlInternalSpawnCB.SyncObj,"SlSpawnSync");
dflet 0:1a07906111ec 94 sl_SyncObjWait(&g_SlInternalSpawnCB.SyncObj,SL_OS_NO_WAIT);
dflet 0:1a07906111ec 95
dflet 0:1a07906111ec 96 g_SlInternalSpawnCB.pFree = &g_SlInternalSpawnCB.SpawnEntries[0];
dflet 0:1a07906111ec 97 g_SlInternalSpawnCB.pWaitForExe = NULL;
dflet 0:1a07906111ec 98 g_SlInternalSpawnCB.pLastInWaitList = NULL;
dflet 0:1a07906111ec 99
dflet 0:1a07906111ec 100 /* create the link list between the entries */
dflet 0:1a07906111ec 101 for (i=0 ; i<_SL_MAX_INTERNAL_SPAWN_ENTRIES - 1 ; i++) {
dflet 0:1a07906111ec 102 g_SlInternalSpawnCB.SpawnEntries[i].pNext = &g_SlInternalSpawnCB.SpawnEntries[i+1];
dflet 0:1a07906111ec 103 g_SlInternalSpawnCB.SpawnEntries[i].pEntry = NULL;
dflet 0:1a07906111ec 104 }
dflet 0:1a07906111ec 105 g_SlInternalSpawnCB.SpawnEntries[i].pNext = NULL;
dflet 0:1a07906111ec 106
dflet 0:1a07906111ec 107 _SlDrvObjUnLock(&g_SlInternalSpawnCB.LockObj);
dflet 0:1a07906111ec 108
dflet 0:1a07906111ec 109
dflet 0:1a07906111ec 110 /* here we ready to execute entries */
dflet 0:1a07906111ec 111
dflet 0:1a07906111ec 112 while (TRUE) {
dflet 0:1a07906111ec 113 sl_SyncObjWait(&g_SlInternalSpawnCB.SyncObj,SL_OS_WAIT_FOREVER);
dflet 0:1a07906111ec 114 /* go over all entries that already waiting for execution */
dflet 0:1a07906111ec 115 LastEntry = FALSE;
dflet 0:1a07906111ec 116 do {
dflet 0:1a07906111ec 117 /* get entry to execute */
dflet 0:1a07906111ec 118 _SlDrvObjLockWaitForever(&g_SlInternalSpawnCB.LockObj);
dflet 0:1a07906111ec 119
dflet 0:1a07906111ec 120 pEntry = g_SlInternalSpawnCB.pWaitForExe;
dflet 0:1a07906111ec 121 if ( NULL == pEntry ) {
dflet 0:1a07906111ec 122 _SlDrvObjUnLock(&g_SlInternalSpawnCB.LockObj);
dflet 0:1a07906111ec 123 break;
dflet 0:1a07906111ec 124 }
dflet 0:1a07906111ec 125 g_SlInternalSpawnCB.pWaitForExe = pEntry->pNext;
dflet 0:1a07906111ec 126 if (pEntry == g_SlInternalSpawnCB.pLastInWaitList) {
dflet 0:1a07906111ec 127 g_SlInternalSpawnCB.pLastInWaitList = NULL;
dflet 0:1a07906111ec 128 LastEntry = TRUE;
dflet 0:1a07906111ec 129 }
dflet 0:1a07906111ec 130
dflet 0:1a07906111ec 131 _SlDrvObjUnLock(&g_SlInternalSpawnCB.LockObj);
dflet 0:1a07906111ec 132
dflet 0:1a07906111ec 133
dflet 0:1a07906111ec 134 /* pEntry could be null in case that the sync was already set by some
dflet 0:1a07906111ec 135 of the entries during execution of earlier entry */
dflet 0:1a07906111ec 136 if (NULL != pEntry) {
dflet 0:1a07906111ec 137 pEntry->pEntry(pEntry->pValue);
dflet 0:1a07906111ec 138 /* free the entry */
dflet 0:1a07906111ec 139 _SlDrvObjLockWaitForever(&g_SlInternalSpawnCB.LockObj);
dflet 0:1a07906111ec 140
dflet 0:1a07906111ec 141 pEntry->pNext = g_SlInternalSpawnCB.pFree;
dflet 0:1a07906111ec 142 g_SlInternalSpawnCB.pFree = pEntry;
dflet 0:1a07906111ec 143
dflet 0:1a07906111ec 144
dflet 0:1a07906111ec 145 if (NULL != g_SlInternalSpawnCB.pWaitForExe) {
dflet 0:1a07906111ec 146 /* new entry received meanwhile */
dflet 0:1a07906111ec 147 LastEntry = FALSE;
dflet 0:1a07906111ec 148 }
dflet 0:1a07906111ec 149
dflet 0:1a07906111ec 150 _SlDrvObjUnLock(&g_SlInternalSpawnCB.LockObj);
dflet 0:1a07906111ec 151
dflet 0:1a07906111ec 152 }
dflet 0:1a07906111ec 153
dflet 0:1a07906111ec 154 } while (!LastEntry);
dflet 0:1a07906111ec 155 }
dflet 0:1a07906111ec 156 }
dflet 0:1a07906111ec 157
dflet 0:1a07906111ec 158
dflet 0:1a07906111ec 159 int16_t cc3100_spawn::_SlInternalSpawn(_SlSpawnEntryFunc_t pEntry , void* pValue , uint32_t flags)
dflet 0:1a07906111ec 160 {
dflet 0:1a07906111ec 161 int16_t Res = 0;
dflet 0:1a07906111ec 162 _SlInternalSpawnEntry_t* pSpawnEntry;
dflet 0:1a07906111ec 163
dflet 0:1a07906111ec 164 if (NULL == pEntry) {
dflet 0:1a07906111ec 165 Res = -1;
dflet 0:1a07906111ec 166 } else {
dflet 0:1a07906111ec 167 _SlDrvObjLockWaitForever(&g_SlInternalSpawnCB.LockObj);
dflet 0:1a07906111ec 168
dflet 0:1a07906111ec 169 pSpawnEntry = g_SlInternalSpawnCB.pFree;
dflet 0:1a07906111ec 170 g_SlInternalSpawnCB.pFree = pSpawnEntry->pNext;
dflet 0:1a07906111ec 171
dflet 0:1a07906111ec 172 pSpawnEntry->pEntry = pEntry;
dflet 0:1a07906111ec 173 pSpawnEntry->pValue = pValue;
dflet 0:1a07906111ec 174 pSpawnEntry->pNext = NULL;
dflet 0:1a07906111ec 175
dflet 0:1a07906111ec 176 if (NULL == g_SlInternalSpawnCB.pWaitForExe) {
dflet 0:1a07906111ec 177 g_SlInternalSpawnCB.pWaitForExe = pSpawnEntry;
dflet 0:1a07906111ec 178 g_SlInternalSpawnCB.pLastInWaitList = pSpawnEntry;
dflet 0:1a07906111ec 179 } else {
dflet 0:1a07906111ec 180 g_SlInternalSpawnCB.pLastInWaitList->pNext = pSpawnEntry;
dflet 0:1a07906111ec 181 g_SlInternalSpawnCB.pLastInWaitList = pSpawnEntry;
dflet 0:1a07906111ec 182 }
dflet 0:1a07906111ec 183
dflet 0:1a07906111ec 184 _SlDrvObjUnLock(&g_SlInternalSpawnCB.LockObj);
dflet 0:1a07906111ec 185
dflet 0:1a07906111ec 186 /* this sync is called after releasing the lock object to avoid unnecessary context switches */
dflet 0:1a07906111ec 187 _SlDrvSyncObjSignal(&g_SlInternalSpawnCB.SyncObj);
dflet 0:1a07906111ec 188 }
dflet 0:1a07906111ec 189
dflet 0:1a07906111ec 190 return Res;
dflet 0:1a07906111ec 191 }
dflet 0:1a07906111ec 192
dflet 0:1a07906111ec 193 }//namespace mbed_cc3100
dflet 0:1a07906111ec 194
dflet 0:1a07906111ec 195 #endif//SL_PLATFORM_MULTI_THREADED
dflet 0:1a07906111ec 196