src/tools/tToDo.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023   
00024 ***************************************************************************
00025 
00026 */
00027 
00028 #include "tToDo.h"
00029 #include "tArray.h"
00030 
00031 #ifdef HAVE_LIBZTHREAD
00032 #include <zthread/FastRecursiveMutex.h>
00033 
00034 static ZThread::FastRecursiveMutex st_mutex;
00035 #else
00036 class tMockMutex
00037 {
00038 public:
00039     void acquire(){};
00040     void release(){};
00041 };
00042 
00043 static tMockMutex st_mutex;
00044 #endif
00045 
00046 tArray<tTODO_FUNC *> tToDos;
00047 
00048 void st_ToDo(tTODO_FUNC *td){ // postpone something
00049     st_mutex.acquire();
00050     tToDos[tToDos.Len()]=td;
00051     st_mutex.release();
00052 }
00053 
00054 // a lone (but relatively safe) function pointer for things to do triggered by signals.
00055 static tTODO_FUNC * st_toDoFromSignal = 0;
00056 
00057 void st_DoToDo(){ // do the things that have been postponed
00058     if ( st_toDoFromSignal )
00059     {
00060         st_ToDo( st_toDoFromSignal );
00061         st_toDoFromSignal = 0;
00062     }
00063     st_mutex.acquire();
00064     while (tToDos.Len()){
00065         tTODO_FUNC *td=tToDos[tToDos.Len()-1];
00066         tToDos.SetLen(tToDos.Len()-1);
00067         (*td)();
00068     }
00069     st_mutex.release();
00070 }
00071 
00072 void st_ToDo_Signal(tTODO_FUNC *td){ // postpone something
00073     // simply ignore double todos from signals.
00074     if ( st_toDoFromSignal )
00075     {
00076         return;
00077     }
00078     st_toDoFromSignal = td;
00079 }

Generated on Sat Mar 15 22:56:02 2008 for Armagetron Advanced by  doxygen 1.5.4