summaryrefslogtreecommitdiff
path: root/Source/DirectFB/lib/voodoo/dispatcher.cpp
blob: af98c0a6c2ce2c230f92de20b65bfe555e85bb9a (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
   (c) Copyright 2001-2011  The world wide DirectFB Open Source Community (directfb.org)
   (c) Copyright 2000-2004  Convergence (integrated media) GmbH

   All rights reserved.

   Written by Denis Oliver Kropp <dok@directfb.org>,
              Andreas Hundt <andi@fischlustig.de>,
              Sven Neumann <neo@directfb.org>,
              Ville Syrjälä <syrjala@sci.fi> and
              Claudio Ciccani <klan@users.sf.net>.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

//#define DIRECT_ENABLE_DEBUG

#include <config.h>

extern "C" {
#include <direct/debug.h>
#include <direct/list.h>
#include <direct/mem.h>
#include <direct/messages.h>
#include <direct/thread.h>
#include <direct/util.h>

#include <voodoo/link.h>
#include <voodoo/message.h>
}

#include <voodoo/dispatcher.h>
#include <voodoo/manager.h>
#include <voodoo/packet.h>


D_DEBUG_DOMAIN( Voodoo_Dispatcher, "Voodoo/Dispatcher", "Voodoo Dispatcher" );

/**********************************************************************************************************************/

VoodooDispatcher::VoodooDispatcher( VoodooManager *manager )
     :
     magic(0),
     manager(manager),
     packets(NULL)
{
     D_DEBUG_AT( Voodoo_Dispatcher, "VoodooDispatcher::%s( %p )\n", __func__, this );

     /* Initialize lock. */
     direct_mutex_init( &lock );

     /* Initialize wait queue. */
     direct_waitqueue_init( &queue );

     D_MAGIC_SET( this, VoodooDispatcher );


     dispatch_loop = direct_thread_create( DTT_MESSAGING, DispatchLoopMain, this, "Voodoo Dispatch" );
}

VoodooDispatcher::~VoodooDispatcher()
{
     D_DEBUG_AT( Voodoo_Dispatcher, "VoodooDispatcher::%s( %p )\n", __func__, this );

     D_MAGIC_ASSERT( this, VoodooDispatcher );

     /* Acquire lock and wake up waiters. */
     direct_mutex_lock( &lock );
     direct_waitqueue_broadcast( &queue );
     direct_mutex_unlock( &lock );

     /* Wait for dispatcher loop exiting. */
     direct_thread_join( dispatch_loop );
     direct_thread_destroy( dispatch_loop );

     /* Destroy queue. */
     direct_waitqueue_deinit( &queue );

     /* Destroy lock. */
     direct_mutex_deinit( &lock );

     D_MAGIC_CLEAR( this );
}

bool
VoodooDispatcher::Ready()
{
     D_DEBUG_AT( Voodoo_Dispatcher, "VoodooDispatcher::%s( %p )\n", __func__, this );

     D_MAGIC_ASSERT( this, VoodooDispatcher );

     direct_mutex_lock( &lock );

     bool ready = direct_list_count_elements_EXPENSIVE( packets ) < 3;

     direct_mutex_unlock( &lock );

     return ready;
}

void
VoodooDispatcher::PutPacket( VoodooPacket *packet )
{
     D_DEBUG_AT( Voodoo_Dispatcher, "VoodooDispatcher::%s( %p, packet %p )\n", __func__, this, packet );

     D_MAGIC_ASSERT( this, VoodooDispatcher );

     direct_mutex_lock( &lock );

     direct_list_append( &packets, &packet->link );

     direct_waitqueue_broadcast( &queue );

     direct_mutex_unlock( &lock );
}

void
VoodooDispatcher::ProcessMessages( VoodooMessageHeader *first,
                                   size_t               total_length )
{
     D_DEBUG_AT( Voodoo_Dispatcher, "VoodooDispatcher::%s( %p, first %p, total_length "_ZU" )\n",
                 __func__, this, first, total_length );

     D_MAGIC_ASSERT( this, VoodooDispatcher );

     VoodooMessageHeader *header = first;
     size_t               offset = 0;
     size_t               aligned;

     while (offset < total_length) {
          /* Get the message header. */
          header  = (VoodooMessageHeader *)((char*) first + offset);
          aligned = VOODOO_MSG_ALIGN( header->size );

          D_DEBUG_AT( Voodoo_Dispatcher, "  -> Next message has %d ("_ZU") bytes and is of type %d... (offset "_ZU"/"_ZU")\n",
                      header->size, aligned, header->type, offset, total_length );

          D_ASSERT( header->size >= (int) sizeof(VoodooMessageHeader) );
          D_ASSERT( header->size <= MAX_MSG_SIZE );

          D_ASSERT( offset + aligned <= total_length );

          switch (header->type) {
               case VMSG_SUPER:
                    manager->handle_super( (VoodooSuperMessage*) header );
                    break;

               case VMSG_REQUEST:
                    manager->handle_request( (VoodooRequestMessage*) header );
                    break;

               case VMSG_RESPONSE:
                    manager->handle_response( (VoodooResponseMessage*) header );
                    break;

               default:
                    D_BUG( "invalid message type %d", header->type );
                    break;
          }

          offset += aligned;
     }

     D_ASSERT( offset == total_length );
}

/**********************************************************************************************************************/

void *
VoodooDispatcher::DispatchLoop()
{
     D_DEBUG_AT( Voodoo_Dispatcher, "VoodooDispatcher::%s( %p )\n", __func__, this );

     direct_mutex_lock( &lock );

     while (!manager->is_quit) {
          VoodooPacket *packet;

          D_MAGIC_ASSERT( this, VoodooDispatcher );

          if (packets) {
               packet = (VoodooPacket*) packets;

               direct_list_remove( &packets, &packet->link );

               manager->link->WakeUp( manager->link );
          }
          else {
               direct_waitqueue_wait( &queue, &lock );

               continue;
          }


          direct_mutex_unlock( &lock );

          ProcessMessages( (VoodooMessageHeader*) packet->data_start(), packet->size() );

          D_FREE( packet );

          direct_mutex_lock( &lock );
     }

     direct_mutex_unlock( &lock );

     return NULL;
}

void *
VoodooDispatcher::DispatchLoopMain( DirectThread *thread, void *arg )
{
     D_DEBUG_AT( Voodoo_Dispatcher, "VoodooDispatcher::%s( %p, thread %p )\n", __func__, arg, thread );

     VoodooDispatcher *dispatcher = (VoodooDispatcher*) arg;

     return dispatcher->DispatchLoop();
}