summaryrefslogtreecommitdiffstats
path: root/kdecore/malloc/x86.h
blob: b215173746c7030c24558e5db35eb48859e62ba1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <sched.h>
#include <time.h>

typedef struct {
  volatile unsigned int lock;
  int pad0_;
} mutex_t;

#define MUTEX_INITIALIZER          { 0, 0 }

static __inline__ int lock(mutex_t *m) {
  int cnt = 0, r;
  struct timespec tm;

  for(;;) {
    __asm__ __volatile__
      ("xchgl %0, %1"
       : "=r"(r), "=m"(m->lock)
       : "0"(1), "m"(m->lock)
       : "memory");
    if(!r)
      return 0;
#ifdef _POSIX_PRIORITY_SCHEDULING
    if(cnt < 50) {
      sched_yield();
      cnt++;
    } else
#endif
        {
      tm.tv_sec = 0;
      tm.tv_nsec = 2000001;
      nanosleep(&tm, NULL);
      cnt = 0;
    }
  }
}

static __inline__ int unlock(mutex_t *m) {
  __asm __volatile ("movl $0,%0" : "=m" (m->lock));
  return 0;
}