1 /*
2 
3 *************************************************************************
4 
5 ArmageTron -- Just another Tron Lightcycle Game in 3D.
6 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
7 
8 **************************************************************************
9 
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
14 
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23 
24 ***************************************************************************
25 
26 */
27 
28 #include "tToDo.h"
29 #include "tArray.h"
30 
31 #ifdef HAVE_LIBZTHREAD
32 #include <zthread/FastRecursiveMutex.h>
33 
34 static ZThread::FastRecursiveMutex st_mutex;
35 #elif defined(HAVE_PTHREAD)
36 #include "pthread-binding.h"
37 static tPThreadRecursiveMutex st_mutex;
38 #else
39 class tMockMutex
40 {
41 public:
acquire()42     void acquire(){}
release()43     void release(){}
44 };
45 
46 static tMockMutex st_mutex;
47 #endif
48 
49 tArray<tTODO_FUNC *> tToDos;
50 
st_ToDo(tTODO_FUNC * td)51 void st_ToDo(tTODO_FUNC *td){ // postpone something
52     st_mutex.acquire();
53     tToDos[tToDos.Len()]=td;
54     st_mutex.release();
55 }
56 
57 // a lone (but relatively safe) function pointer for things to do triggered by signals.
58 static tTODO_FUNC * st_toDoFromSignal = 0;
59 
st_DoToDo()60 void st_DoToDo(){ // do the things that have been postponed
61     if ( st_toDoFromSignal )
62     {
63         st_ToDo( st_toDoFromSignal );
64         st_toDoFromSignal = 0;
65     }
66     st_mutex.acquire();
67     while (tToDos.Len()){
68         tTODO_FUNC *td=tToDos[tToDos.Len()-1];
69         tToDos.SetLen(tToDos.Len()-1);
70         (*td)();
71     }
72     st_mutex.release();
73 }
74 
st_ToDo_Signal(tTODO_FUNC * td)75 void st_ToDo_Signal(tTODO_FUNC *td){ // postpone something
76     // simply ignore double todos from signals.
77     if ( st_toDoFromSignal )
78     {
79         return;
80     }
81     st_toDoFromSignal = td;
82 }
83