Conditionals -- beta proposal

Okay, I think I’ve got something worth looking at. :slight_smile:

The following functions are available.

SDL_CreateCond()
SDL_WaitCond() - wait for condition
SDL_condSignalOne() - wake one thread waiting on condition
SDL_condSignalAll() - wake all threads waiting on condition
SDL_DestroyCond()

There are three implementations of the SDL_cond functions.

posix ...... uses POSIX conditional variables
mutex ...... uses SDL_mutex functions
semaphore .. uses Unix semaphores

The posix implementation is pretty self-explanatory. It is intended for
systems that implement the POSIX threads functions properly.

The mutex implementation uses a group of three SDL mutexes internally. It
is intended for systems like Win32 and BeOS where mutexes are supported
but conditionals are not.

The semaphore implementation is almost like the mutex implementation
except that it uses a single SDL mutex and two semaphores directly. It is
intended for those systems that implement the System V IPC but not POSIX
threads.

The reason there is a seperate semaphore implementation (when the mutex
code for those systems use semaphores anyway) is because of the SDL mutex
implementation itself. I needed to use semaphore operations that were not
rolled back when a process died.

Here’s some basic psuedocode for the operational logic of the
mutex/semaphore sections:

CreateCond() {
	CreateMutex(conditional)
	CreateMutex(talkback)
	LockMutex(conditional)
	LockMutex(talkback)
}

WaitCond() {
	LockMutex(internal)
	refcount++
	UnLockMutex(internal)
	LockMutex(conditional) // this is where we wait
	UnlockMutex(talkback)
	LockMutex(internal)
	refcount--
	UnLockMutex(internal)
}

SignalOne() {
	LockMutex(internal)
	if (refcount) {
		UnlockMutex(conditional)
		LockMutex(talkback)
	}
	UnlockMutex(internal)
}

SignalAll() {
	LockMutex(internal)
	while (refcount--) {
		UnlockMutex(conditional)
		LockMutex(talkback)
	}
	UnlockMutex(internal)
}

The only important note about the mutex/semaphore code is that all threads
that get restarted must wait until all other threads have been signaled
(in the case of SDL_condSignalAll()). I don’t think this should be a big
problem, though.

The other important note is that the mutex implementation (Win32/BeOS)
relies on the fact that any thread can unlock an SDL mutex. (As far as I
can tell, this is the case.)

Hopefully this code is robust/portable enough that Sam will consider it
for inclusion. :slight_smile: If so, maybe someone can help me with the autoconf
stuff so I can submit a patch that will be easier to integrate.

Feedback is always welcome.

Paul Braman
@Paul_Braman
-------------- next part --------------
begin 660 cond.tar.gz
M’XL(“U5+S@"V-O;F0N=&%R.T<:6_32#1?ZU\Q6\3B=)/B(X=$&VWS:(
M7FK2HA6+ at FN/$ZN.’=E.H4OY[SLS=GS%9\A%F2<@\7C>O’N.-R_(IJ&\K
P7
M0(-KMSE00"T6XW(IP<<:N%XL=44&IP(,]QE<!S<H&8&8[D at 518FD=4O
M[_U/"C
V_Z5I:U]?[H#]&\UFHX7LS_-B@]I_P_;OGYP.>.AO&(:)Q;K73[
M-QH-(69_D1/:%<!1^Z=GFF&K,\4"%Y/G;$%)>5P_)9A at E;D%,0QH&695O3=
MON\PXWV&L1UK)CM at W@:^,7O>B.1YZ#\T0E:)S,'?D7-Y+/#?.\PC(]\@+\= MHVX./$;/[+VI*54\9-#ZLYFFE(.L+<“SV”-X-^E4GDJZ;,FMK_T%39<-X MU2IB1U,!^UNX$9,A="YFSH5Z!B>F]<#BGGL6=&:6P9Y?GY[BY^^,BQT14S,T MA_T]-%[]+7ZH8(5#-Z’3A>KE-V-J1;‘0+’!#(1&(20]PE=U8
0#;-8@!E7
MNPG<D!=EV”$(+B,1216(3&X^)A;BFFO*=H3O=,UYD^2)I#?"#1]$2"=!,&
M8EUM at T5(!’!%U7L<5’GTW6)G. at N29<Y6L1H81UF4B**_2EN%!YXG at H8!J+
M\D9%FQFIPA543L8(V1RZB)D*\AJXJ%]@2GUMAA=&/#7<0Z;B)P4=-F47+RE M9-NF;8]T_=>Q[:UE2HHLV<X3-2]>Q8E]3]REH\"TOF#<,DM05,[DSL$LE[1@ M?4=L5YXTD/U_L)%;"XV<_3\0VTUO_R^*#8Z<_]J<2/?_FX#8MKW#.]3J$5 MQ/?S00\&?G6 at 98#LW7IGWBUC([?8*6U5S^H972."GCG3#>I8H4#B_TRZ at ZJF MPS71R(O_9JL9RO]Q./Z;#9[&_R8‘8Y?,7M_0GEL at A<3Y=@BI-!CS:<2-.Q M:<%'LD*^8!C2COJ.9!G4>X?@LZW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W00Z3VP7;A\8>C;V5
MQYZDZYXX=H
3XX3<3CAY/.$29BJV#:2’_H7Z;[];LOU7TU!%!JD_KO1I/5?
M6[#_6FK<NJ_>$%LQ^POHOXT_[/9_]:4PSFNGQ4T*AI@?[I3:5%QMM.%E# M\=@SM!J4-4,J+##8?FJE$%CX]!4[][AO]>GP^OST^Z??.NR=59F;@@S5B
M:&:0’#LZ-_C7PGO>:5HZ(VF#!4;’-S.5-0\LY’O.S@'8$D/I&+L&42KA^KG
M-(\A#,
?E)V$?A$UK/T?C?%<C?\#WFO?$DR<\4MH([OW"1SX\1AVA]2Z/ MA^<7'XYZQ=_5>5SN&N0N]<4K\G/_N,6EQSA-YP:.;XX/^D->A?GP$_EUW$
M[ZQGZGI)"$,CD[?W5T-[O/"H[’<B::Y3)<D)Z,IT8P,B$*WO4XUH,T
MDC1<1HUW14B%YI3%G3T[U(!0!:\!%UQX$4\&;]Z;N]\<.5=+XU,QP3SL;S[ MHMCMSW,;^!7H[K63.8661-(TQ(G1KHIPE;272DV&S:6\R91R43;70YOA:3+
MX->[L%J:XR5K)/?"P89
;D[+)E==-JDIB%6DX!%T6#P]7%[U;HX&73++L%R+ MXQYQZ_%5]VC@S)MH!1/RBRE]'ULP^64/RAW>)RB8OO7S!&9T2$?Q$W[+_K
M#?L7UU?'7302ZB$[>IJBR8
V;LZZYW@(:!NPW)82,A0;%6YJK\LK:M<$47O MX3WY_S,XO%CFL]KO#FZ.3FLD[JN)*"$C)&$$U_7N.I!*++H&A>[NT\Y\F069 M"\0B;$:6KV5)T>K%7:Q>7,;+,@>[*>5%M-J05AN6JS9<\+$"#IM5C%5^YJ.U M at 4^]-I#ZV%HJ^0KN&N,[I-"F<76;S#0B"7O,W=FCTJI"A0H4*!@0(%"A0H E4*!@0(%"A0H4*!@0(%"A0H4*!@0(%"A1RX’\6OYG’@`````
`
end

Paul Braman wrote:

The mutex implementation uses a group of three SDL mutexes internally. It
is intended for systems like Win32 and BeOS where mutexes are supported
but conditionals are not.

You can write condition variables on Win32 using event objects.
PulseEvent is used to signal a single waiter and SetEvent to
signal all waiters.

Cheers,
Nat.–
±-----------------------------------------±--------------------+
| Name: Nat Pryce MEng ACGI | Dept. of Computing, |
| Email: np2 at doc.ic.ac.uk | Imperial College, |
| Tel: +44 (0)171 594 8394 | 180 Queen’s Gate, |
| Fax: +44 (0)171 581 8024 | London SW7 2BZ, |
| WWW: http://www-dse.doc.ic.ac.uk/~np2 | United Kingdom |
±-----------------------------------------±--------------------+

You can write condition variables on Win32 using event objects.
PulseEvent is used to signal a single waiter and SetEvent to
signal all waiters.

I’m not too familiar with the Win32 API but the SDL_mutex code uses binary
semaphores there. That’s all I really need (I think). If you have some
documentation on the event objects you’re talking about, fill me in. :slight_smile:

I still need to fill in the bits about handling the associated mutex that
goes along with a conditional (the posted code was more proof-of-concept
that I could get an XP thread signalling mechanism working).

I figure once I get the SDL_WaitCond() function to handle
unlocking/locking a mutex for the calling thread I should have a pretty
good proposal for conditonal variables in SDL.

Paul Braman
@Paul_BramanOn Mon, 15 Nov 1999, Nat Pryce wrote: