Rev 3555 | Rev 5201 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3555 | Rev 3725 | ||
---|---|---|---|
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
2 | ;; ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2010. All rights reserved. ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2010. All rights reserved. ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
5 | ;; ;; |
6 | ;; queue.inc ;; |
6 | ;; queue.inc ;; |
7 | ;; ;; |
7 | ;; ;; |
8 | ;; Written by hidnplayr@kolibrios.org ;; |
8 | ;; Written by hidnplayr@kolibrios.org ;; |
9 | ;; ;; |
9 | ;; ;; |
10 | ;; GNU GENERAL PUBLIC LICENSE ;; |
10 | ;; GNU GENERAL PUBLIC LICENSE ;; |
11 | ;; Version 2, June 1991 ;; |
11 | ;; Version 2, June 1991 ;; |
12 | ;; ;; |
12 | ;; ;; |
13 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
13 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
14 | 14 | ||
15 | $Revision: 3555 $ |
15 | $Revision: 3725 $ |
16 | 16 | ||
17 | ; The Queues implemented by these macros form a ring-buffer. |
17 | ; The Queues implemented by these macros form a ring-buffer. |
18 | ; The data to these queue's always looks like this: |
18 | ; The data to these queue's always looks like this: |
19 | ; |
19 | ; |
20 | ; At top, you have the queue struct, wich has the size (number of currently queued packets, read and write pointers. |
20 | ; At top, you have the queue struct, wich has the size (number of currently queued packets, read and write pointers. |
21 | ; This struct is followed by a number of slots wich you can read and write to using the macros. |
21 | ; This struct is followed by a number of slots wich you can read and write to using the macros. |
22 | ; How these slots look like is up to you to chose, normally they should have at least a pointer to where the real data is. |
22 | ; How these slots look like is up to you to chose, normally they should have at least a pointer to where the real data is. |
23 | ; (you can see some examples below) |
23 | ; (you can see some examples below) |
24 | 24 | ||
25 | 25 | ||
26 | struct queue |
26 | struct queue |
27 | 27 | ||
28 | size dd ? ; number of queued packets in this queue |
28 | size dd ? ; number of queued packets in this queue |
29 | w_ptr dd ? ; current writing pointer in queue |
29 | w_ptr dd ? ; current writing pointer in queue |
30 | r_ptr dd ? ; current reading pointer |
30 | r_ptr dd ? ; current reading pointer |
- | 31 | mutex MUTEX |
|
31 | 32 | ||
32 | ends |
33 | ends |
33 | 34 | ||
34 | ; The following macros share these inputs: |
35 | ; The following macros share these inputs: |
35 | 36 | ||
36 | ; ptr = pointer to where the queue data is located |
37 | ; ptr = pointer to where the queue data is located |
37 | ; size = number of slots/entrys in the queue |
38 | ; size = number of slots/entrys in the queue |
38 | ; entry_size = size of one slot, in bytes |
39 | ; entry_size = size of one slot, in bytes |
39 | ; failaddr = the address where macro will jump to when there is no data in the queue |
40 | ; failaddr = the address where macro will jump to when there is no data in the queue |
40 | 41 | ||
41 | ; additionally, add_to_queue requires you to set esi to the data wich you want to queue |
42 | ; additionally, add_to_queue requires you to set esi to the data wich you want to queue |
42 | ; get_from_queue on the other hand will return a pointer in esi, to the entry you're interessed in |
43 | ; get_from_queue on the other hand will return a pointer in esi, to the entry you're interessed in |
43 | ; PS: macros WILL destroy ecx and edi |
44 | ; PS: macros WILL destroy ecx and edi |
44 | 45 | ||
45 | macro add_to_queue ptr, size, entry_size, failaddr { |
46 | macro add_to_queue ptr, size, entry_size, failaddr { |
- | 47 | ||
- | 48 | local .ok, .no_wrap |
|
- | 49 | ||
- | 50 | pusha |
|
- | 51 | lea ecx, [ptr + queue.mutex] |
|
- | 52 | call mutex_lock |
|
- | 53 | popa |
|
46 | 54 | ||
- | 55 | cmp [ptr + queue.size], size ; Check if queue isnt full |
|
- | 56 | jb .ok |
|
- | 57 | ||
- | 58 | pusha |
|
- | 59 | lea ecx, [ptr + queue.mutex] |
|
- | 60 | call mutex_unlock |
|
47 | cmp [ptr + queue.size], size ; Check if queue isnt full |
61 | popa |
- | 62 | jmp failaddr |
|
48 | jae failaddr |
63 | |
49 | 64 | .ok: |
|
50 | inc [ptr + queue.size] ; if not full, queue one more |
65 | inc [ptr + queue.size] ; if not full, queue one more |
51 | 66 | ||
52 | mov edi, [ptr + queue.w_ptr] ; Current write pointer (FIFO!) |
67 | mov edi, [ptr + queue.w_ptr] ; Current write pointer (FIFO!) |
53 | mov ecx, entry_size/4 ; Write the queue entry |
68 | mov ecx, entry_size/4 ; Write the queue entry |
54 | rep movsd ; |
69 | rep movsd ; |
55 | 70 | ||
56 | lea ecx, [size*entry_size+ptr+sizeof.queue] |
71 | lea ecx, [size*entry_size+ptr+sizeof.queue] |
57 | cmp edi, ecx ; entry size |
72 | cmp edi, ecx ; entry size |
58 | jb .no_wrap |
73 | jb .no_wrap |
59 | 74 | ||
60 | sub edi, size*entry_size |
75 | sub edi, size*entry_size |
61 | - | ||
62 | .no_wrap: |
76 | .no_wrap: |
63 | mov [ptr + queue.w_ptr], edi |
77 | mov [ptr + queue.w_ptr], edi |
- | 78 | ||
- | 79 | pusha |
|
- | 80 | lea ecx, [ptr + queue.mutex] |
|
- | 81 | call mutex_unlock |
|
- | 82 | popa |
|
64 | 83 | ||
65 | } |
84 | } |
66 | 85 | ||
67 | 86 | ||
68 | 87 | ||
69 | macro get_from_queue ptr, size, entry_size, failaddr { |
88 | macro get_from_queue ptr, size, entry_size, failaddr { |
- | 89 | ||
- | 90 | local .ok, .no_wrap |
|
- | 91 | ||
- | 92 | pusha |
|
- | 93 | lea ecx, [ptr + queue.mutex] |
|
- | 94 | call mutex_lock |
|
- | 95 | popa |
|
70 | 96 | ||
- | 97 | cmp [ptr + queue.size], 0 ; any packets queued? |
|
- | 98 | ja .ok |
|
- | 99 | ||
- | 100 | pusha |
|
- | 101 | lea ecx, [ptr + queue.mutex] |
|
- | 102 | call mutex_unlock |
|
71 | cmp [ptr + queue.size], 0 ; any packets queued? |
103 | popa |
- | 104 | jmp failaddr |
|
72 | je failaddr |
105 | |
73 | 106 | .ok: |
|
74 | dec [ptr + queue.size] ; if so, dequeue one |
107 | dec [ptr + queue.size] ; if so, dequeue one |
75 | 108 | ||
76 | mov esi, [ptr + queue.r_ptr] |
109 | mov esi, [ptr + queue.r_ptr] |
77 | push esi |
110 | push esi |
78 | 111 | ||
79 | add esi, entry_size |
112 | add esi, entry_size |
80 | 113 | ||
81 | lea ecx, [size*entry_size+ptr+sizeof.queue] |
114 | lea ecx, [size*entry_size+ptr+sizeof.queue] |
82 | cmp esi, ecx ; entry size |
115 | cmp esi, ecx ; entry size |
83 | jb .no_wrap |
116 | jb .no_wrap |
84 | 117 | ||
85 | sub esi, size*entry_size |
118 | sub esi, size*entry_size |
86 | 119 | ||
87 | .no_wrap: |
120 | .no_wrap: |
88 | mov dword [ptr + queue.r_ptr], esi |
121 | mov dword [ptr + queue.r_ptr], esi |
89 | 122 | ||
90 | pop esi |
123 | pop esi |
- | 124 | ||
- | 125 | pusha |
|
- | 126 | lea ecx, [ptr + queue.mutex] |
|
- | 127 | call mutex_unlock |
|
- | 128 | popa |
|
91 | 129 | ||
92 | } |
130 | } |
93 | 131 | ||
94 | macro init_queue ptr { |
132 | macro init_queue ptr { |
95 | 133 | ||
96 | mov [ptr + queue.size] , 0 |
134 | mov [ptr + queue.size] , 0 |
97 | lea edi, [ptr + sizeof.queue] |
135 | lea edi, [ptr + sizeof.queue] |
98 | mov [ptr + queue.w_ptr], edi |
136 | mov [ptr + queue.w_ptr], edi |
99 | mov [ptr + queue.r_ptr], edi |
137 | mov [ptr + queue.r_ptr], edi |
- | 138 | ||
- | 139 | lea ecx, [ptr + queue.mutex] |
|
- | 140 | call mutex_init |
|
100 | } |
141 | } |
101 | 142 |