Part Number: LAUNCHXL-F28069M
Other Parts Discussed in Thread: SYSBIOS
Tool/software: TI-RTOS
Can you please provide an example project that uses GateMutexPri to demonstrate a solution to concurrent-access model?
Thanks!
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Part Number: LAUNCHXL-F28069M
Other Parts Discussed in Thread: SYSBIOS
Tool/software: TI-RTOS
Can you please provide an example project that uses GateMutexPri to demonstrate a solution to concurrent-access model?
Thanks!
Hi Louis,
I took the mutex example from SYS/BIOS and tweaked it to use a GateMutexPri instance instead of a Semaphore. The lower priority spins for longer than the higher priority sleeps. When the higher priority tries to enter the gate, the lower priority task has it's priority raised. I also had to add the following into the .cfg of the example
var GateMutexPri = xdc.useModule('ti.sysbios.gates.GateMutexPri');
/*
* Copyright (c) 2015-2019, Texas Instruments Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* ======== mutex.c ========
*/
/* XDC module Headers */
#include <xdc/std.h>
#include <xdc/runtime/Error.h>
#include <xdc/runtime/System.h>
/* BIOS module Headers */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Clock.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Semaphore.h>
#include <ti/sysbios/gates/GateMutexPri.h>
#define TASKSTACKSIZE 512
Void task1Fxn(UArg arg0, UArg arg1);
Void task2Fxn(UArg arg0, UArg arg1);
Int resource = 0;
Int finishCount = 0;
UInt32 sleepTickCount;
Task_Struct task1Struct, task2Struct;
Char task1Stack[TASKSTACKSIZE], task2Stack[TASKSTACKSIZE];
Semaphore_Struct semStruct;
Semaphore_Handle semHandle;
GateMutexPri_Handle gateHandle;
/*
* ======== main ========
*/
int main()
{
/* Construct BIOS objects */
Task_Params taskParams;
Semaphore_Params semParams;
Error_Block eb;
Error_init(&eb);
gateHandle = GateMutexPri_create(NULL, &eb);
/* Construct writer/reader Task threads */
Task_Params_init(&taskParams);
taskParams.stackSize = TASKSTACKSIZE;
taskParams.stack = &task1Stack;
taskParams.priority = 1;
Task_construct(&task1Struct, (Task_FuncPtr)task1Fxn, &taskParams, NULL);
taskParams.stack = &task2Stack;
taskParams.priority = 2;
Task_construct(&task2Struct, (Task_FuncPtr)task2Fxn, &taskParams, NULL);
/* Construct a Semaphore object to be use as a resource lock, inital count 1 */
Semaphore_Params_init(&semParams);
Semaphore_construct(&semStruct, 1, &semParams);
/* Obtain instance handle */
semHandle = Semaphore_handle(&semStruct);
/* We want to sleep for 10000 microseconds */
sleepTickCount = 10000 / Clock_tickPeriod;
BIOS_start(); /* Does not return */
return(0);
}
/*
* ======== task1Fxn ========
*/
Void task1Fxn(UArg arg0, UArg arg1)
{
UInt32 time;
IArg key;
for (;;) {
System_printf("Running task1 function\n");
// if (Semaphore_getCount(semHandle) == 0) {
// System_printf("Sem blocked in task1\n");
// }
/* Get access to resource */
// Semaphore_pend(semHandle, BIOS_WAIT_FOREVER);
key = GateMutexPri_enter(gateHandle);
/* Do work by waiting for 2 system ticks to pass */
time = Clock_getTicks();
while (Clock_getTicks() <= (time + sleepTickCount)) {
;
}
/* Do work on locked resource */
resource += 1;
/* Unlock resource */
// Semaphore_post(semHandle);
GateMutexPri_leave(gateHandle, key);
Task_sleep(sleepTickCount);
}
}
/*
* ======== task2Fxn ========
*/
Void task2Fxn(UArg arg0, UArg arg1)
{
IArg key;
for (;;) {
System_printf("Running task2 function\n");
// if (Semaphore_getCount(semHandle) == 0) {
// System_printf("Sem blocked in task2\n");
// }
/* Get access to resource */
// Semaphore_pend(semHandle, BIOS_WAIT_FOREVER);
key = GateMutexPri_enter(gateHandle);
/* Do work on locked resource */
resource += 1;
/* Unlock resource */
// Semaphore_post(semHandle);
GateMutexPri_leave(gateHandle, key);
Task_sleep(sleepTickCount);
finishCount++;
if (finishCount == 5) {
System_printf("Calling BIOS_exit from task2\n");
BIOS_exit(0);
}
}
}
Todd