Search
lxdream.org :: lxdream/src/eventq.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/eventq.c
changeset 736:a02d1475ccfd
prev730:a0f02e769c2e
next1065:bc1cc0c54917
author nkeynes
date Mon Jul 14 07:44:42 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Re-indent everything consistently
Fix include guards for consistency as well
file annotate diff log raw
1.1 --- a/src/eventq.c Tue Jul 08 12:28:10 2008 +0000
1.2 +++ b/src/eventq.c Mon Jul 14 07:44:42 2008 +0000
1.3 @@ -20,6 +20,7 @@
1.4 */
1.5
1.6 #include <assert.h>
1.7 +#include "dream.h"
1.8 #include "dreamcast.h"
1.9 #include "eventq.h"
1.10 #include "asic.h"
1.11 @@ -53,29 +54,29 @@
1.12 int event_load_state( FILE * f );
1.13
1.14 struct dreamcast_module eventq_module = { "EVENTQ", event_init, event_reset, NULL, event_run_slice,
1.15 - NULL, event_save_state, event_load_state };
1.16 + NULL, event_save_state, event_load_state };
1.17
1.18 static void event_update_pending( )
1.19 {
1.20 if( event_head == NULL ) {
1.21 - if( !(sh4r.event_types & PENDING_IRQ) ) {
1.22 - sh4r.event_pending = NOT_SCHEDULED;
1.23 - }
1.24 - sh4r.event_types &= (~PENDING_EVENT);
1.25 + if( !(sh4r.event_types & PENDING_IRQ) ) {
1.26 + sh4r.event_pending = NOT_SCHEDULED;
1.27 + }
1.28 + sh4r.event_types &= (~PENDING_EVENT);
1.29 } else {
1.30 - if( !(sh4r.event_types & PENDING_IRQ) ) {
1.31 - sh4r.event_pending = event_head->nanosecs;
1.32 - }
1.33 - sh4r.event_types |= PENDING_EVENT;
1.34 + if( !(sh4r.event_types & PENDING_IRQ) ) {
1.35 + sh4r.event_pending = event_head->nanosecs;
1.36 + }
1.37 + sh4r.event_types |= PENDING_EVENT;
1.38 }
1.39 }
1.40
1.41 uint32_t event_get_next_time( )
1.42 {
1.43 if( event_head == NULL ) {
1.44 - return NOT_SCHEDULED;
1.45 + return NOT_SCHEDULED;
1.46 } else {
1.47 - return event_head->nanosecs;
1.48 + return event_head->nanosecs;
1.49 }
1.50 }
1.51
1.52 @@ -85,61 +86,61 @@
1.53 static void event_enqueue( event_t event )
1.54 {
1.55 if( event_head == NULL || event->nanosecs < event_head->nanosecs ) {
1.56 - event->next = event_head;
1.57 - event_head = event;
1.58 - event_update_pending();
1.59 + event->next = event_head;
1.60 + event_head = event;
1.61 + event_update_pending();
1.62 } else {
1.63 - event_t cur = event_head;
1.64 - event_t next = cur->next;
1.65 - while( next != NULL && event->nanosecs >= next->nanosecs ) {
1.66 - cur = next;
1.67 - next = cur->next;
1.68 - }
1.69 - event->next = next;
1.70 - cur->next = event;
1.71 + event_t cur = event_head;
1.72 + event_t next = cur->next;
1.73 + while( next != NULL && event->nanosecs >= next->nanosecs ) {
1.74 + cur = next;
1.75 + next = cur->next;
1.76 + }
1.77 + event->next = next;
1.78 + cur->next = event;
1.79 }
1.80 }
1.81
1.82 static void event_dequeue( event_t event )
1.83 {
1.84 if( event_head == NULL ) {
1.85 - ERROR( "Empty event queue but should contain event %d", event->id );
1.86 + ERROR( "Empty event queue but should contain event %d", event->id );
1.87 } else if( event_head == event ) {
1.88 - /* removing queue head */
1.89 - event_head = event_head->next;
1.90 - event_update_pending();
1.91 + /* removing queue head */
1.92 + event_head = event_head->next;
1.93 + event_update_pending();
1.94 } else {
1.95 - event_t cur = event_head;
1.96 - event_t next = cur->next;
1.97 - while( next != NULL ) {
1.98 - if( next == event ) {
1.99 - cur->next = next->next;
1.100 - break;
1.101 - }
1.102 - cur = next;
1.103 - next = cur->next;
1.104 - }
1.105 + event_t cur = event_head;
1.106 + event_t next = cur->next;
1.107 + while( next != NULL ) {
1.108 + if( next == event ) {
1.109 + cur->next = next->next;
1.110 + break;
1.111 + }
1.112 + cur = next;
1.113 + next = cur->next;
1.114 + }
1.115 }
1.116 }
1.117
1.118 static void event_dequeue_long( event_t event )
1.119 {
1.120 if( long_event_head == NULL ) {
1.121 - ERROR( "Empty long event queue but should contain event %d", event->id );
1.122 + ERROR( "Empty long event queue but should contain event %d", event->id );
1.123 } else if( long_event_head == event ) {
1.124 - /* removing queue head */
1.125 - long_event_head = long_event_head->next;
1.126 + /* removing queue head */
1.127 + long_event_head = long_event_head->next;
1.128 } else {
1.129 - event_t cur = long_event_head;
1.130 - event_t next = cur->next;
1.131 - while( next != NULL ) {
1.132 - if( next == event ) {
1.133 - cur->next = next->next;
1.134 - break;
1.135 - }
1.136 - cur = next;
1.137 - next = cur->next;
1.138 - }
1.139 + event_t cur = long_event_head;
1.140 + event_t next = cur->next;
1.141 + while( next != NULL ) {
1.142 + if( next == event ) {
1.143 + cur->next = next->next;
1.144 + break;
1.145 + }
1.146 + cur = next;
1.147 + next = cur->next;
1.148 + }
1.149 }
1.150 }
1.151
1.152 @@ -155,49 +156,49 @@
1.153 event_t event = &events[eventid];
1.154
1.155 if( event->nanosecs != NOT_SCHEDULED ) {
1.156 - /* Event is already scheduled. Remove it from the list first */
1.157 - event_cancel(eventid);
1.158 + /* Event is already scheduled. Remove it from the list first */
1.159 + event_cancel(eventid);
1.160 }
1.161
1.162 event->id = eventid;
1.163 event->seconds = 0;
1.164 event->nanosecs = nanosecs;
1.165 -
1.166 +
1.167 event_enqueue( event );
1.168 }
1.169
1.170 void event_schedule_long( int eventid, uint32_t seconds, uint32_t nanosecs ) {
1.171 if( seconds == 0 ) {
1.172 - event_schedule( eventid, nanosecs );
1.173 + event_schedule( eventid, nanosecs );
1.174 } else {
1.175 - event_t event = &events[eventid];
1.176 + event_t event = &events[eventid];
1.177
1.178 - if( event->nanosecs != NOT_SCHEDULED ) {
1.179 - /* Event is already scheduled. Remove it from the list first */
1.180 - event_cancel(eventid);
1.181 - }
1.182 + if( event->nanosecs != NOT_SCHEDULED ) {
1.183 + /* Event is already scheduled. Remove it from the list first */
1.184 + event_cancel(eventid);
1.185 + }
1.186
1.187 - event->id = eventid;
1.188 - event->seconds = seconds;
1.189 - event->nanosecs = nanosecs;
1.190 - event->next = long_event_head;
1.191 - long_event_head = event;
1.192 + event->id = eventid;
1.193 + event->seconds = seconds;
1.194 + event->nanosecs = nanosecs;
1.195 + event->next = long_event_head;
1.196 + long_event_head = event;
1.197 }
1.198 -
1.199 +
1.200 }
1.201
1.202 void event_cancel( int eventid )
1.203 {
1.204 event_t event = &events[eventid];
1.205 if( event->nanosecs == NOT_SCHEDULED ) {
1.206 - return; /* not scheduled */
1.207 + return; /* not scheduled */
1.208 } else {
1.209 - event->nanosecs = NOT_SCHEDULED;
1.210 - if( event->seconds != 0 ) { /* long term event */
1.211 - event_dequeue_long( event );
1.212 - } else {
1.213 - event_dequeue( event );
1.214 - }
1.215 + event->nanosecs = NOT_SCHEDULED;
1.216 + if( event->seconds != 0 ) { /* long term event */
1.217 + event_dequeue_long( event );
1.218 + } else {
1.219 + event_dequeue( event );
1.220 + }
1.221 }
1.222 }
1.223
1.224 @@ -206,12 +207,12 @@
1.225 {
1.226 /* Loop in case we missed some or got a couple scheduled for the same time */
1.227 while( event_head != NULL && event_head->nanosecs <= sh4r.slice_cycle ) {
1.228 - event_t event = event_head;
1.229 - event_head = event->next;
1.230 - event->nanosecs = NOT_SCHEDULED;
1.231 - // Note: Make sure the internal state is consistent before calling the
1.232 - // user function, as it will (quite likely) enqueue another event.
1.233 - event->func( event->id );
1.234 + event_t event = event_head;
1.235 + event_head = event->next;
1.236 + event->nanosecs = NOT_SCHEDULED;
1.237 + // Note: Make sure the internal state is consistent before calling the
1.238 + // user function, as it will (quite likely) enqueue another event.
1.239 + event->func( event->id );
1.240 }
1.241
1.242 event_update_pending();
1.243 @@ -226,14 +227,14 @@
1.244 {
1.245 int i;
1.246 for( i=0; i<MAX_EVENT_ID; i++ ) {
1.247 - events[i].id = i;
1.248 - events[i].nanosecs = NOT_SCHEDULED;
1.249 - if( i < 96 ) {
1.250 - events[i].func = event_asic_callback;
1.251 - } else {
1.252 - events[i].func = NULL;
1.253 - }
1.254 - events[i].next = NULL;
1.255 + events[i].id = i;
1.256 + events[i].nanosecs = NOT_SCHEDULED;
1.257 + if( i < 96 ) {
1.258 + events[i].func = event_asic_callback;
1.259 + } else {
1.260 + events[i].func = NULL;
1.261 + }
1.262 + events[i].next = NULL;
1.263 }
1.264 event_head = NULL;
1.265 long_event_head = NULL;
1.266 @@ -249,7 +250,7 @@
1.267 long_event_head = NULL;
1.268 long_scan_time_remaining = LONG_SCAN_PERIOD;
1.269 for( i=0; i<MAX_EVENT_ID; i++ ) {
1.270 - events[i].nanosecs = NOT_SCHEDULED;
1.271 + events[i].nanosecs = NOT_SCHEDULED;
1.272 }
1.273 }
1.274
1.275 @@ -262,9 +263,9 @@
1.276 fwrite( &id, sizeof(id), 1, f );
1.277 fwrite( &long_scan_time_remaining, sizeof(long_scan_time_remaining), 1, f );
1.278 for( i=0; i<MAX_EVENT_ID; i++ ) {
1.279 - fwrite( &events[i].id, sizeof(uint32_t), 3, f ); /* First 3 words from structure */
1.280 - id = events[i].next == NULL ? -1 : events[i].next->id;
1.281 - fwrite( &id, sizeof(id), 1, f );
1.282 + fwrite( &events[i].id, sizeof(uint32_t), 3, f ); /* First 3 words from structure */
1.283 + id = events[i].next == NULL ? -1 : events[i].next->id;
1.284 + fwrite( &id, sizeof(id), 1, f );
1.285 }
1.286 }
1.287
1.288 @@ -277,9 +278,9 @@
1.289 long_event_head = id == -1 ? NULL : &events[id];
1.290 fread( &long_scan_time_remaining, sizeof(long_scan_time_remaining), 1, f );
1.291 for( i=0; i<MAX_EVENT_ID; i++ ) {
1.292 - fread( &events[i].id, sizeof(uint32_t), 3, f );
1.293 - fread( &id, sizeof(id), 1, f );
1.294 - events[i].next = id == -1 ? NULL : &events[id];
1.295 + fread( &events[i].id, sizeof(uint32_t), 3, f );
1.296 + fread( &id, sizeof(id), 1, f );
1.297 + events[i].next = id == -1 ? NULL : &events[id];
1.298 }
1.299 return 0;
1.300 }
1.301 @@ -291,23 +292,23 @@
1.302 static void event_scan_long()
1.303 {
1.304 while( long_event_head != NULL && --long_event_head->seconds == 0 ) {
1.305 - event_t event = long_event_head;
1.306 - long_event_head = event->next;
1.307 - event_enqueue(event);
1.308 + event_t event = long_event_head;
1.309 + long_event_head = event->next;
1.310 + event_enqueue(event);
1.311 }
1.312
1.313 if( long_event_head != NULL ) {
1.314 - event_t last = long_event_head;
1.315 - event_t cur = last->next;
1.316 - while( cur != NULL ) {
1.317 - if( --cur->seconds == 0 ) {
1.318 - last->next = cur->next;
1.319 - event_enqueue(cur);
1.320 - } else {
1.321 - last = cur;
1.322 - }
1.323 - cur = last->next;
1.324 - }
1.325 + event_t last = long_event_head;
1.326 + event_t cur = last->next;
1.327 + while( cur != NULL ) {
1.328 + if( --cur->seconds == 0 ) {
1.329 + last->next = cur->next;
1.330 + event_enqueue(cur);
1.331 + } else {
1.332 + last = cur;
1.333 + }
1.334 + cur = last->next;
1.335 + }
1.336 }
1.337 }
1.338
1.339 @@ -320,18 +321,18 @@
1.340 {
1.341 event_t event = event_head;
1.342 while( event != NULL ) {
1.343 - if( event->nanosecs <= nanosecs ) {
1.344 - event->nanosecs = 0;
1.345 - } else {
1.346 - event->nanosecs -= nanosecs;
1.347 - }
1.348 - event = event->next;
1.349 + if( event->nanosecs <= nanosecs ) {
1.350 + event->nanosecs = 0;
1.351 + } else {
1.352 + event->nanosecs -= nanosecs;
1.353 + }
1.354 + event = event->next;
1.355 }
1.356
1.357 long_scan_time_remaining -= nanosecs;
1.358 if( long_scan_time_remaining <= 0 ) {
1.359 - long_scan_time_remaining += LONG_SCAN_PERIOD;
1.360 - event_scan_long();
1.361 + long_scan_time_remaining += LONG_SCAN_PERIOD;
1.362 + event_scan_long();
1.363 }
1.364
1.365 event_update_pending();
.