]>
Dogcows Code - chaz/yoink/blob - src/Moof/fastevents.c
2 NET2 is a threaded, event based, network IO library for SDL.
3 Copyright (C) 2002 Bob Pendleton
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License
7 as published by the Free Software Foundation; either version 2.1
8 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 If you do not wish to comply with the terms of the LGPL please
21 contact the author as other terms are available for a fee.
31 #include "SDL_thread.h"
33 #include "fastevents.h"
35 //----------------------------------------
37 // error handling code
40 static char* error
= NULL
;
42 static __inline__
void setError(char *err
)
52 //----------------------------------------
54 // Threads, mutexs, thread utils, and
55 // thread safe wrappers
58 static SDL_mutex
*eventLock
= NULL
;
59 static SDL_cond
*eventWait
= NULL
;
60 static SDL_TimerID eventTimer
= 0;
62 //----------------------------------------
67 int FE_PushEvent(SDL_Event
*ev
)
69 SDL_LockMutex(eventLock
);
70 while (-1 == SDL_PushEvent(ev
))
72 SDL_CondWait(eventWait
, eventLock
);
74 SDL_UnlockMutex(eventLock
);
75 SDL_CondSignal(eventWait
);
80 //----------------------------------------
87 SDL_LockMutex(eventLock
);
89 SDL_UnlockMutex(eventLock
);
92 //----------------------------------------
97 int FE_PollEvent(SDL_Event
*event
)
101 SDL_LockMutex(eventLock
);
102 val
= SDL_PollEvent(event
);
103 SDL_UnlockMutex(eventLock
);
107 SDL_CondSignal(eventWait
);
113 //----------------------------------------
115 // Replacement for SDL_WaitEvent();
118 int FE_WaitEvent(SDL_Event
*event
)
122 SDL_LockMutex(eventLock
);
123 while (0 >= (val
= SDL_PollEvent(event
)))
125 SDL_CondWait(eventWait
, eventLock
);
127 SDL_UnlockMutex(eventLock
);
128 SDL_CondSignal(eventWait
);
133 //----------------------------------------
138 static Uint32
timerCallback(Uint32 interval
, void *param
)
140 SDL_CondBroadcast(eventWait
);
145 //----------------------------------------
152 if (0 == (SDL_INIT_TIMER
& SDL_WasInit(SDL_INIT_TIMER
)))
154 SDL_InitSubSystem(SDL_INIT_TIMER
);
157 eventLock
= SDL_CreateMutex();
158 if (NULL
== eventLock
)
160 setError("FE: can't create a mutex");
164 eventWait
= SDL_CreateCond();
165 if (NULL
== eventWait
)
167 setError("FE: can't create a condition variable");
171 eventTimer
= SDL_AddTimer(10, timerCallback
, NULL
);
172 if (NULL
== eventTimer
)
174 setError("FE: can't add a timer");
181 //----------------------------------------
188 SDL_DestroyMutex(eventLock
);
191 SDL_DestroyCond(eventWait
);
194 SDL_RemoveTimer(eventTimer
);
This page took 0.040643 seconds and 4 git commands to generate.