summaryrefslogtreecommitdiff
path: root/src/Dolphin/dvd/dvdqueue.c
blob: a381ccf0de142e52eabc89c7ffbd80da1f05cd60 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "dolphin/DVDPriv.h"

#define MAX_QUEUES 4
typedef struct {
  DVDCommandBlock* next;
  DVDCommandBlock* prev;
} DVDQueue;

static DVDQueue WaitingQueue[MAX_QUEUES];

void __DVDClearWaitingQueue(void) {
  u32 i;

  for (i = 0; i < MAX_QUEUES; i++) {
    DVDCommandBlock* q;

    q = (DVDCommandBlock*)&(WaitingQueue[i]);
    q->next = q;
    q->prev = q;
  }
}

BOOL __DVDPushWaitingQueue(s32 prio, DVDCommandBlock* block) {
  BOOL enabled;
  DVDCommandBlock* q;

  enabled = OSDisableInterrupts();

  q = (DVDCommandBlock*)&(WaitingQueue[prio]);

  q->prev->next = block;
  block->prev = q->prev;
  block->next = q;
  q->prev = block;

  OSRestoreInterrupts(enabled);

  return TRUE;
}

static DVDCommandBlock* PopWaitingQueuePrio(s32 prio) {
  DVDCommandBlock* tmp;
  BOOL enabled;
  DVDCommandBlock* q;

  enabled = OSDisableInterrupts();

  q = (DVDCommandBlock*)&(WaitingQueue[prio]);

  tmp = q->next;
  q->next = tmp->next;
  tmp->next->prev = q;

  OSRestoreInterrupts(enabled);

  tmp->next = (DVDCommandBlock*)NULL;
  tmp->prev = (DVDCommandBlock*)NULL;

  return tmp;
}

DVDCommandBlock* __DVDPopWaitingQueue(void) {
  u32 i;
  BOOL enabled;
  DVDCommandBlock* q;

  enabled = OSDisableInterrupts();

  for (i = 0; i < MAX_QUEUES; i++) {
    q = (DVDCommandBlock*)&(WaitingQueue[i]);
    if (q->next != q) {
      OSRestoreInterrupts(enabled);
      return PopWaitingQueuePrio((s32)i);
    }
  }

  OSRestoreInterrupts(enabled);

  return (DVDCommandBlock*)NULL;
}

BOOL __DVDCheckWaitingQueue(void) {
  u32 i;
  BOOL enabled;
  DVDCommandBlock* q;

  enabled = OSDisableInterrupts();

  for (i = 0; i < MAX_QUEUES; i++) {
    q = (DVDCommandBlock*)&(WaitingQueue[i]);
    if (q->next != q) {
      OSRestoreInterrupts(enabled);
      return TRUE;
    }
  }

  OSRestoreInterrupts(enabled);

  return FALSE;
}

BOOL __DVDDequeueWaitingQueue(DVDCommandBlock* block) {
  BOOL enabled;
  DVDCommandBlock* prev;
  DVDCommandBlock* next;

  enabled = OSDisableInterrupts();

  prev = block->prev;
  next = block->next;

  if ((prev == (DVDCommandBlock*)NULL) || (next == (DVDCommandBlock*)NULL)) {
    OSRestoreInterrupts(enabled);
    return FALSE;
  }

  prev->next = next;
  next->prev = prev;

  OSRestoreInterrupts(enabled);

  return TRUE;
}

BOOL __DVDIsBlockInWaitingQueue(DVDCommandBlock* block) {
  u32 i;
  DVDCommandBlock* start;
  DVDCommandBlock* q;

  for (i = 0; i < MAX_QUEUES; i++) {
    start = (DVDCommandBlock*)&(WaitingQueue[i]);

    if (start->next != start) {
      for (q = start->next; q != start; q = q->next) {
        if (q == block)
          return TRUE;
      }
    }
  }

  return FALSE;
}