1 /*
2   This file is part of Deadbeef Player source code
3   http://deadbeef.sourceforge.net
4 
5   generic message queue implementation
6 
7   Copyright (C) 2009-2013 Alexey Yakovenko
8 
9   This software is provided 'as-is', without any express or implied
10   warranty.  In no event will the authors be held liable for any damages
11   arising from the use of this software.
12 
13   Permission is granted to anyone to use this software for any purpose,
14   including commercial applications, and to alter it and redistribute it
15   freely, subject to the following restrictions:
16 
17   1. The origin of this software must not be misrepresented; you must not
18      claim that you wrote the original software. If you use this software
19      in a product, an acknowledgment in the product documentation would be
20      appreciated but is not required.
21   2. Altered source versions must be plainly marked as such, and must not be
22      misrepresented as being the original software.
23   3. This notice may not be removed or altered from any source distribution.
24 
25   Alexey Yakovenko waker@users.sourceforge.net
26 */
27 #include "handler.h"
28 #include <stdio.h>
29 #include <string.h>
30 #include <assert.h>
31 #include <stdlib.h>
32 #include "handler.h"
33 #include "threading.h"
34 
35 typedef struct message_s {
36     uint32_t id;
37     uintptr_t ctx;
38     uint32_t p1;
39     uint32_t p2;
40     struct message_s *next;
41 } message_t;
42 
43 typedef struct handler_s {
44     int queue_size;
45     message_t *mfree;
46     message_t *mqueue;
47     message_t *mqtail;
48     uintptr_t mutex;
49     uintptr_t cond;
50     message_t pool[1];
51 } handler_t;
52 
53 void
handler_reset(handler_t * h)54 handler_reset (handler_t *h) {
55     h->mqueue = NULL;
56     h->mfree = NULL;
57     h->mqtail = NULL;
58     memset (h->pool, 0, sizeof (message_t) * h->queue_size);
59     for (int i = 0; i < h->queue_size; i++) {
60         h->pool[i].next = h->mfree;
61         h->mfree = &h->pool[i];
62     }
63 }
64 
65 handler_t *
handler_alloc(int queue_size)66 handler_alloc (int queue_size) {
67     int sz = sizeof (handler_t) + (queue_size-1) * sizeof (message_t);
68     handler_t *h = malloc (sz);
69     memset (h, 0, sz);
70     h->queue_size = queue_size;
71     h->mutex = mutex_create ();
72     h->cond = cond_create ();
73     handler_reset (h);
74     return h;
75 }
76 
77 void
handler_free(handler_t * h)78 handler_free (handler_t *h) {
79     mutex_lock (h->mutex);
80     handler_reset (h);
81     mutex_unlock (h->mutex);
82     mutex_free (h->mutex);
83     cond_free (h->cond);
84     free (h);
85 }
86 
87 int
handler_push(handler_t * h,uint32_t id,uintptr_t ctx,uint32_t p1,uint32_t p2)88 handler_push (handler_t *h, uint32_t id, uintptr_t ctx, uint32_t p1, uint32_t p2) {
89     if (!h) {
90         return -1;
91     }
92     if (!h->mfree) {
93         return -1;
94     }
95     mutex_lock (h->mutex);
96     message_t *msg = h->mfree;
97     h->mfree = h->mfree->next;
98     if (h->mqtail) {
99         h->mqtail->next = msg;
100     }
101     h->mqtail = msg;
102     if (!h->mqueue) {
103         h->mqueue = msg;
104     }
105 
106     msg->next = NULL;
107     msg->id = id;
108     msg->ctx = ctx;
109     msg->p1 = p1;
110     msg->p2 = p2;
111     mutex_unlock (h->mutex);
112     cond_signal (h->cond);
113     return 0;
114 }
115 
116 void
handler_wait(handler_t * h)117 handler_wait (handler_t *h) {
118     cond_wait (h->cond, h->mutex);
119     mutex_unlock (h->mutex);
120 }
121 
122 int
handler_pop(handler_t * h,uint32_t * id,uintptr_t * ctx,uint32_t * p1,uint32_t * p2)123 handler_pop (handler_t *h, uint32_t *id, uintptr_t *ctx, uint32_t *p1, uint32_t *p2) {
124     mutex_lock (h->mutex);
125     if (!h->mqueue) {
126         mutex_unlock (h->mutex);
127         return -1;
128     }
129     *id = h->mqueue->id;
130     *ctx = h->mqueue->ctx;
131     *p1 = h->mqueue->p1;
132     *p2 = h->mqueue->p2;
133     message_t *next = h->mqueue->next;
134     h->mqueue->next = h->mfree;
135     h->mfree = h->mqueue;
136     h->mqueue = next;
137     if (!h->mqueue) {
138         h->mqtail = NULL;
139     }
140     mutex_unlock (h->mutex);
141     return 0;
142 }
143 
144 int
handler_hasmessages(handler_t * h)145 handler_hasmessages (handler_t *h) {
146     return h->mqueue ? 1 : 0;
147 }
148 
149