OpenCores
URL https://opencores.org/ocsvn/plasma/plasma/trunk

Subversion Repositories plasma

[/] [plasma/] [trunk/] [kernel/] [rtos_ex.c] - Diff between revs 405 and 407

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 405 Rev 407
Line 13... Line 13...
#include "plasma.h"
#include "plasma.h"
#include "rtos.h"
#include "rtos.h"
 
 
/************** WIN32 Simulation Support *************/
/************** WIN32 Simulation Support *************/
#ifdef WIN32
#ifdef WIN32
#undef kbhit
#include <conio.h>
#undef getch
#define kbhit _kbhit
#undef putch
#define getch _getch
extern int kbhit(void);
#define putch _putch
extern int getch(void);
extern void __stdcall Sleep(unsigned long value);
extern int putch(int);
 
 
#if OS_CPU_COUNT > 1
unsigned int __stdcall GetCurrentThreadId(void);
unsigned int __stdcall GetCurrentThreadId(void);
typedef void (*LPTHREAD_START_ROUTINE)(void *lpThreadParameter);
typedef void (*LPTHREAD_START_ROUTINE)(void *lpThreadParameter);
void * __stdcall CreateThread(void *lpsa, unsigned int dwStackSize,
void * __stdcall CreateThread(void *lpsa, unsigned int dwStackSize,
   LPTHREAD_START_ROUTINE pfnThreadProc, void *pvParam,
   LPTHREAD_START_ROUTINE pfnThreadProc, void *pvParam,
   unsigned int dwCreationFlags, unsigned int *pdwThreadId);
   unsigned int dwCreationFlags, unsigned int *pdwThreadId);
 
 
#if OS_CPU_COUNT > 1
 
static unsigned int ThreadId[OS_CPU_COUNT];
static unsigned int ThreadId[OS_CPU_COUNT];
 
 
//PC simulation of multiple CPUs
//PC simulation of multiple CPUs
void OS_InitSimulation(void)
void OS_InitSimulation(void)
{
{
Line 37... Line 37...
   int i;
   int i;
   ThreadId[0] = GetCurrentThreadId();
   ThreadId[0] = GetCurrentThreadId();
   for(i = 1; i < OS_CPU_COUNT; ++i)
   for(i = 1; i < OS_CPU_COUNT; ++i)
      CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)OS_Start, NULL, 0, &ThreadId[i]);
      CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)OS_Start, NULL, 0, &ThreadId[i]);
}
}
#endif
#endif  //OS_CPU_COUNT > 1
 
 
static uint32 Memory[8];
static uint32 Memory[8];
 
 
//Simulates device register memory reads
//Simulates device register memory reads
uint32 MemoryRead(uint32 address)
uint32 MemoryRead(uint32 address)
Line 107... Line 107...
   {
   {
      if(threadId == ThreadId[i])
      if(threadId == ThreadId[i])
         return i;
         return i;
   }
   }
#endif
#endif
   return 0; //0 to OS_CPU_COUNT-1; Read a CPU specific GPIO value
   //return MemoryRead(GPIO_CPU_INDEX);
 
   return 0; //0 to OS_CPU_COUNT-1
}
}
 
 
 
 
/******************************************/
/******************************************/
//Symmetric Multiprocessing Spin Lock Mutex
//Symmetric Multiprocessing Spin Lock Mutex
Line 120... Line 121...
   uint32 state, cpuIndex, i, ok, delay;
   uint32 state, cpuIndex, i, ok, delay;
   volatile uint32 keepVar;
   volatile uint32 keepVar;
 
 
   cpuIndex = OS_CpuIndex();
   cpuIndex = OS_CpuIndex();
   state = OS_AsmInterruptEnable(0);    //disable interrupts
   state = OS_AsmInterruptEnable(0);    //disable interrupts
   if(++SpinLockArray[cpuIndex] > 1)    //check for nesting
   if(SpinLockArray[cpuIndex])
      return state;
      return (uint32)-1;                //already locked
   delay = (4 + cpuIndex) << 2;
   delay = (4 + cpuIndex) << 2;
 
 
   //Spin until only this CPU has the spin lock
   //Spin until only this CPU has the spin lock
   for(;;)
   for(;;)
   {
   {
      ok = 1;
      ok = 1;
 
      SpinLockArray[cpuIndex] = 1;
      for(i = 0; i < OS_CPU_COUNT; ++i)
      for(i = 0; i < OS_CPU_COUNT; ++i)
      {
      {
         if(i != cpuIndex && SpinLockArray[i])
         if(i != cpuIndex && SpinLockArray[i])
            ok = 0;   //Another CPU has the spin lock
            ok = 0;   //Another CPU has the spin lock
      }
      }
Line 143... Line 145...
         ++ok;
         ++ok;
      keepVar = ok;    //don't optimize away the delay loop
      keepVar = ok;    //don't optimize away the delay loop
      if(delay < 128)
      if(delay < 128)
         delay <<= 1;
         delay <<= 1;
      state = OS_AsmInterruptEnable(0); //disable interrupts
      state = OS_AsmInterruptEnable(0); //disable interrupts
      SpinLockArray[cpuIndex] = 1;
 
   }
   }
}
}
 
 
 
 
/******************************************/
/******************************************/
void OS_SpinUnlock(uint32 state)
void OS_SpinUnlock(uint32 state)
{
{
   uint32 cpuIndex;
   uint32 cpuIndex;
 
   if(state == (uint32)-1)
 
      return;                           //nested lock call
   cpuIndex = OS_CpuIndex();
   cpuIndex = OS_CpuIndex();
   if(--SpinLockArray[cpuIndex] == 0)
   SpinLockArray[cpuIndex] = 0;
      OS_AsmInterruptEnable(state);
 
 
 
   assert(SpinLockArray[cpuIndex] < 10);
 
}
}
#endif  //OS_CPU_COUNT > 1
#endif  //OS_CPU_COUNT > 1
 
 
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.