I am attempting to use spinlockIsr_t
in vxWorks 6.7 but if I try to use it with multiple tasks the system freezes. Are you allowed to use multiple tasks that attempt to acquire the same spin lock? If not then whats the point of it in the first place? The following code works with 1 or 2 tasks but once the number of tasks is increased to 3 system freezes before being able to finish printing first line on console.
spinlockIsr_t mySpinLock;
int sharedResource;
void vx_test_mtx_spin_lock(void)
{
const int kNumTask = 3;
const int kTaskPriority = 50;
const int kTaskStackSize = 10000;
int i = 0;
sharedResource = 0;
char procName[40];
printf("Number of tasks : %d\n", kNumTask);
printf("SPIN_LOCK_ISR_INIT() [main task]\n");
SPIN_LOCK_ISR_INIT (&mySpinLock, 0); printf("SPIN_LOCK_ISR_TAKE() [main task] \n");
SPIN_LOCK_ISR_TAKE (&mySpinLock);
for (i = 0; i < kNumTask; i++){
sprintf(procName, "%s%d", "myTask", i); printf("taskSpawn() [main task]\n");
taskSpawn((char*)procName,
kTaskPriority,
0,
kTaskStackSize,
(FUNCPTR)vx_spin_lock_unlock,
0,0,0,0,0,0,0,0,0,0
);
} printf("SPIN_LOCK_ISR_GIVE() [main task]");
SPIN_LOCK_ISR_GIVE (&mySpinLock);
}
void vx_spin_lock_unlock(void)
{
printf("SPIN_LOCK_ISR_TAKE() [spawned task] \n");
SPIN_LOCK_ISR_TAKE (&mySpinLock);
/* ... Access the share resource here */ printf("SPIN_LOCK_ISR_GIVE() [spawned task] \n");
SPIN_LOCK_ISR_GIVE (&mySpinLock);
}
I suspect you have a higher-priority task spinning on the spinlock while a lower-priority task already holds the lock. The lower-priority task will never be able to run, and therefore the lock is never unlocked, and deadlock occurs. Spinlocks should basically never be used in realtime systems. If they are used, you must ensure that all threads that could spin on the lock have the same priority.