mips_lock.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. *
  3. * simple locking test program
  4. * (no paralles stuff)
  5. *
  6. * Compile with: gcc -D__CPU_i386 -O3 on x86 machines and
  7. * gcc -mips2 -O2 -D__CPU_mips on mips machines.
  8. * -- andrei
  9. *
  10. *
  11. */
  12. #include <stdio.h>
  13. typedef volatile int fl_lock_t;
  14. int tsl(fl_lock_t* lock)
  15. {
  16. long val;
  17. #ifdef __CPU_mips
  18. long tmp=0;
  19. asm volatile(
  20. ".set noreorder\n\t"
  21. "1: ll %1, %2 \n\t"
  22. " li %0, 1 \n\t"
  23. " sc %0, %2 \n\t"
  24. " beqz %0, 1b \n\t"
  25. " nop \n\t"
  26. ".set reorder\n\t"
  27. : "=&r" (tmp), "=&r" (val), "=m" (*lock)
  28. : "0" (tmp), "m" (*lock)
  29. : "cc"
  30. );
  31. #elif defined __CPU_i386
  32. val=1;
  33. asm volatile(
  34. " xchg %b1, %0" : "=q" (val), "=m" (*lock) : "0" (val)
  35. );
  36. #else
  37. #error "cpu type not defined, add -D__CPU_<type> when compiling"
  38. #endif
  39. return val;
  40. }
  41. void release_lock(fl_lock_t* lock)
  42. {
  43. #ifdef __CPU_mips
  44. int tmp;
  45. tmp=0;
  46. asm volatile(
  47. ".set noreorder \n\t"
  48. " sync \n\t"
  49. " sw $0, %0 \n\t"
  50. ".set reorder \n\t"
  51. : /*no output*/ : "m" (*lock) : "memory"
  52. );
  53. #elif defined __CPU_i386
  54. asm volatile(
  55. " movb $0, (%0)" : /*no output*/ : "r"(lock): "memory"
  56. );
  57. #else
  58. #error "cpu type not defined, add -D__CPU_<type> when compiling"
  59. #endif
  60. }
  61. int main(int argc, char** argv)
  62. {
  63. fl_lock_t lock;
  64. int r;
  65. lock=0;
  66. printf("starting locking basic tests...\n");
  67. r=tsl(&lock);
  68. printf(" tsl should return 0 ... %d\n", r);
  69. printf(" lock should be 1 now ... %d\n", lock);
  70. r=tsl(&lock);
  71. printf(" tsl should return 1 ... %d\n", r);
  72. printf(" lock should still be 1 now ... %d\n", lock);
  73. release_lock(&lock);
  74. printf(" release_lock: lock should be 0 now ... %d\n", lock);
  75. printf("trying tsl once more...\n");
  76. r=tsl(&lock);
  77. printf(" tsl should return 0 ... %d\n", r);
  78. printf(" lock should be 1 now ... %d\n", lock);
  79. printf("\ndone.\n");
  80. return 0;
  81. }