Rev 1451 | Rev 1560 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1451 | Rev 1476 | ||
---|---|---|---|
Line 25... | Line 25... | ||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <key_buffer.h> |
29 | #include <key_buffer.h> |
- | 30 | #include <futex.h> |
|
- | 31 | ||
- | 32 | atomic_t keybuffer_futex = FUTEX_INITIALIZER; |
|
30 | 33 | ||
31 | /** Clear key buffer. |
34 | /** Clear key buffer. |
32 | */ |
35 | */ |
33 | void keybuffer_free(keybuffer_t *keybuffer) |
36 | void keybuffer_free(keybuffer_t *keybuffer) |
34 | { |
37 | { |
35 | - | ||
- | 38 | futex_down(&keybuffer_futex); |
|
- | 39 | keybuffer->head = 0; |
|
- | 40 | keybuffer->tail = 0; |
|
36 | keybuffer->items = 0; |
41 | keybuffer->items = 0; |
37 | keybuffer->head = keybuffer->tail = keybuffer->items = 0; |
42 | futex_up(&keybuffer_futex); |
38 | } |
43 | } |
39 | 44 | ||
40 | /** Key buffer initialization. |
45 | /** Key buffer initialization. |
41 | * |
46 | * |
42 | */ |
47 | */ |
Line 67... | Line 72... | ||
67 | * If buffer is full, character is ignored. |
72 | * If buffer is full, character is ignored. |
68 | * @param key code of stored key |
73 | * @param key code of stored key |
69 | */ |
74 | */ |
70 | void keybuffer_push(keybuffer_t *keybuffer, char key) |
75 | void keybuffer_push(keybuffer_t *keybuffer, char key) |
71 | { |
76 | { |
- | 77 | futex_down(&keybuffer_futex); |
|
72 | if (keybuffer->items < KEYBUFFER_SIZE) { |
78 | if (keybuffer->items < KEYBUFFER_SIZE) { |
- | 79 | keybuffer->fifo[keybuffer->tail] = key; |
|
73 | keybuffer->fifo[keybuffer->tail = (keybuffer->tail + 1) < keybuffer->items ? (keybuffer->tail + 1) : 0] = (key); |
80 | keybuffer->tail = (keybuffer->tail + 1) % KEYBUFFER_SIZE; |
74 | keybuffer->items++; |
81 | keybuffer->items++; |
75 | } |
82 | } |
- | 83 | futex_up(&keybuffer_futex); |
|
76 | } |
84 | } |
77 | 85 | ||
78 | /** Pop character from buffer. |
86 | /** Pop character from buffer. |
79 | * @param c pointer to space where to store character from buffer. |
87 | * @param c pointer to space where to store character from buffer. |
80 | * @return zero on empty buffer, nonzero else |
88 | * @return zero on empty buffer, nonzero else |
81 | */ |
89 | */ |
82 | int keybuffer_pop(keybuffer_t *keybuffer, char *c) |
90 | int keybuffer_pop(keybuffer_t *keybuffer, char *c) |
83 | { |
91 | { |
- | 92 | futex_down(&keybuffer_futex); |
|
84 | if (keybuffer->items > 0) { |
93 | if (keybuffer->items > 0) { |
85 | keybuffer->items--; |
94 | keybuffer->items--; |
- | 95 | *c = keybuffer->fifo[keybuffer->head]; |
|
86 | *c = keybuffer->fifo[keybuffer->head = (keybuffer->head + 1) < keybuffer->items ? (keybuffer->head + 1) : 0]; |
96 | keybuffer->head = (keybuffer->head + 1) % KEYBUFFER_SIZE; |
- | 97 | futex_up(&keybuffer_futex); |
|
87 | return 1; |
98 | return 1; |
88 | } |
99 | } |
- | 100 | futex_up(&keybuffer_futex); |
|
89 | return 0; |
101 | return 0; |
90 | } |
102 | } |
91 | 103 | ||
92 | 104 |