Advertisement
Guest User

SPINLOCK.C

a guest
Nov 9th, 2015
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.93 KB | None | 0 0
  1. //this is Coto's spinlock , requires spinlock_sched() to be glued to some kind of irq trigger so it runs regardless the main program core.
  2.  
  3. #include "spinlock.h"
  4.  
  5. u32cback_ptr cb_handler; //global callbacku32handler
  6. int counter = 0; //counter on hblank
  7. volatile spinlock_table spinlock_handler[spinlock_elements];
  8.  
  9. //spinlock create process | spinlock_createproc(u8 process_id,u8 status,u32cback_ptr new_callback)
  10. //returns 0 if created | returns 1 if already exists (failure)
  11. u32 spinlock_createproc(u8 process_id,u8 status,u32cback_ptr new_callback){
  12.         if(!spinlock_handler[process_id].spinlock_id){
  13.                 spinlock_handler[process_id].spinlock_id = process_id;
  14.                 spinlock_handler[process_id].status = status;
  15.                 spinlock_handler[process_id].func_ptr = (u32)new_callback;
  16.         }
  17.         else
  18.                 return 1;
  19. return 0;
  20. }
  21.  
  22. //Modifies an existing spinlock process
  23. //returns 0 if created | returns 1 if doesnot exists (failure)
  24. u32 spinlock_modifyproc(u8 process_id,u8 status,u32cback_ptr new_callback){
  25.         if(spinlock_handler[process_id].spinlock_id){
  26.                 //spinlock_handler[process_id].spinlock_id = process_id; //Modify keeps the actual index/ID
  27.                 spinlock_handler[process_id].status = status;
  28.                 spinlock_handler[process_id].func_ptr = (u32)new_callback;
  29.         }
  30.         else
  31.                 return 1;
  32. return 0;
  33. }
  34.  
  35. //Deletes an existing spinlock process
  36. //returns 0 if created | returns 1 if doesnot exists (failure)
  37. u32 spinlock_deleteproc(u8 process_id){
  38.         if(spinlock_handler[process_id].spinlock_id){
  39.                 spinlock_handler[process_id].spinlock_id =      (u32)NULL_VAR;
  40.                 spinlock_handler[process_id].status =           (u32)NULL_VAR;
  41.                 spinlock_handler[process_id].func_ptr =         (u32)NULL_VAR;
  42.         }
  43.         else
  44.                 return 1;
  45. return 0;
  46. }
  47.  
  48.  
  49. //spinlock scheduler //executes all threads set to unlock
  50. u32 __attribute__ ((hot)) spinlock_sched(){
  51.        
  52.         switch(spinlock_handler[counter].status){
  53.                 case(0):
  54.                         cb_handler = (u32cback_ptr) spinlock_handler[counter].func_ptr;
  55.                         //spinlock_handler[counter].status=1;
  56.                         cb_handler();
  57.                 break;
  58.                 default:
  59.                         counter++; //if this thread isn't runnable index++
  60.                 break;
  61.         }
  62.        
  63.         if (counter>((int)spinlock_linearelements))
  64.                 counter=0;
  65.        
  66. return 0;
  67. }
  68.  
  69. //spinlock permission enable/disable
  70. //returns 0 if set permission | returns 1 if process don't exists (failure)
  71. u32 spinlock_perm(u8 process_id,u8 status){
  72.         if(spinlock_handler[process_id].spinlock_id){
  73.                 spinlock_handler[process_id].status=status;
  74.                 return 0;
  75.         }
  76.         else
  77.                 return 1;
  78.  
  79. return 1;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement