Optimizing Allegro Games Library Threading Code

4 posts / 0 new
Last post
IamSONIC
IamSONIC's picture
Offline
Last seen: 4 years 7 months ago
Joined: 2011-09-09 11:16
Optimizing Allegro Games Library Threading Code

When using the Allegro Games Library Shared Object every Game using it causes immediately 100% CPU load (on an X5000).

I investigated the source code of the library and found within the file "AllegroSDK_1.1_Source/src/amiga/atimer.c" the following timer thread code:

  1. static void timer_thread_func(struct AmiThread *aAmiThread)
  2. {
  3. usecs_t Interval;
  4. ULONG Signal, ThreadSignal, TimerSignal;
  5. struct timeval OldTime, NewTime;
  6.  
  7. /* Determine the time at which the first timer request was made so we can determine the */
  8. /* real interval of the timer when it is called back, thus avoiding drift */
  9.  
  10. gettimeofday(&OldTime, NULL);
  11.  
  12. /* And request a timer callback after 1 MS, just to kickstart the timer processing. Allegro's */
  13. /* tick counter will handle calculating how often to call the callback in the future */
  14.  
  15. amithread_request_timeout(aAmiThread, 1000);
  16.  
  17. ThreadSignal = (1 << aAmiThread->at_ThreadSignalBit);
  18. TimerSignal = (1 << aAmiThread->at_TimerMsgPort->mp_SigBit);
  19.  
  20. for ( ; ; )
  21. {
  22. Signal = IExec->Wait(ThreadSignal | TimerSignal);
  23.  
  24. if (Signal & ThreadSignal)
  25. {
  26. break;
  27. }
  28.  
  29. if (Signal & TimerSignal)
  30. {
  31. /* Calculate actual time elapsed */
  32.  
  33. gettimeofday(&NewTime, NULL);
  34. Interval = ((NewTime.tv_sec - OldTime.tv_sec) * 1000000 + (NewTime.tv_usec - OldTime.tv_usec));
  35. OldTime = NewTime;
  36.  
  37. /* Handle a tick */
  38.  
  39. Interval = timer_thread_handle_tick(Interval);
  40. amithread_request_timeout(aAmiThread, Interval);
  41. }
  42. }
  43.  
  44. amithread_remove_sender(&gGraphicsThread);
  45. }

Line 20 creates an infinite loop. Maybe older OS 4.1 Versions (not FE) handled this better.

How could this be improved?
Thanks

OldFart
OldFart's picture
Offline
Last seen: 4 months 2 weeks ago
Joined: 2010-11-30 14:09
Re: Optimizing Allegro Games Library Threading Code

@IamSONIC

Although in line 20 the infinit loop starts, its enthousiasm is tempered in line 22 with an explicit wait for either ThreadSignal or TimerSignal to be received. As in line 24 it is checked which of the two signals is gotten and ThreadSignal would break out of the loop, TimerSignal is the repetitive one. But have you verified that it is this loop that takes up all the cpu-cycles?

  1. for ( ; ; )
  2. {
  3. Signal = IExec->Wait(ThreadSignal | TimerSignal);
  4.  
  5.  
  6. }

OldFart

IamSONIC
IamSONIC's picture
Offline
Last seen: 4 years 7 months ago
Joined: 2011-09-09 11:16
Re: Optimizing Allegro Games Library Threading Code

@OldFart

Thanks for your reply. No i have not verified that this loop takes all the cycles. I don't know how to identify a specific task on AmigaOS in terms of performance.

IamSONIC
IamSONIC's picture
Offline
Last seen: 4 years 7 months ago
Joined: 2011-09-09 11:16
Re: Optimizing Allegro Games Library Threading Code

Ok Embarrassing (╥_╥) ..did my homework about Line 22.

Got some information about that from this Video at ­01:10:55 and from the AutoDocs

Wait(signalSet)

will cause the current task to suspend waiting for one or more signals :D

Log in or register to post comments