Alpha conditionals

I have attached some alpha code testing the following functions…

SDL_CreateCond()
SDL_DestroyCond()
SDL_WaitCond()
SDL_condSignalOne()
SDL_condSignalAll()

…I have only tested on Linux and need some testing on other platforms.

For POSIX threads-enabled systems we can use the POSIX threads conditional
variables themselves.

Now, it turns out that if my design works as I think it does we can use
mutexes easily on systems that don’t support conditionals naturally.

Here’s the basic design for non-POSIX-enabled systems…

SDL_CreateCond()

	Create and lock a mutex.

SDL_WaitCond()

	Increment the reference count and attempt to lock mutex.
	When signaled decrement the reference count.

SDL_condSignalOne()

	Unlock mutex.

SDL_condSignalAll()

	Unlock mutex "reference count" times.

Thoughts on this design?

Paul Braman
@Paul_Braman
-------------- next part --------------
begin 660 cond.tar.gz
M’XL(“HM+C@"V-O;F0N=&%R.U:6V_;-A3.J_4K3AUT5=PXL2S)!NJV6-$$ MV[!D[9JTV[-2W3ME!9,B0Z;3KTOX\7R;I8DB4WL=NYR&1>3DW?B0/#VEY M[NAX[VX)C$Z_WX$].CWC-3_D#JTI/I1K>G]W4K=/IFGM@[FV!%@%!/L>
MPIY;UFY=7=%AO_BY.S9Q-5L0.G(NF49=#A[O>+Q-[6>GAE_73/T/>C(
M;]S"HB_LA$"(#_E;[YD"%O\W2!7Y>&QY"Y=<174+-[G+AZ![1*(:@?*
MEX&BD)LY’N$Q9(5$’[3)LJS%OE[Z&!’\DOY6KSU[=!"I(UJPOS:Q/1<YM&<C
M\1.>@1JW.Y at AQ
$L-;_8V^L)OL='%"-[3&HJ<[/X+>W9V=<&A?W:D%>C<_Q MS/-O5-:AX6.R\%V5MZ*_OZ2EMY\+WSR#V(9S5J+F2%LVS at J]P.34]SU?;;[T M%L[(?43XJR=VAR1<8^QDENE;1+#UE5-;.]-M*7-3S!=/R]&R%JU1.U[&): M+L'X.H];J8(4%1\V4:^\7=I3M>P)B](M:06;2TSH'\@F?#KDS@)N*W/)@VQA MQ@&O41#0&8KX$$*B<3.I6%NKXN:,M?#@&71J.7U%6B[[QX\'647>?:4B"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MXCE at A'.;Q[),>/)C>1%1,TQ2B0:;@61KB(!3Q!5Q]X!M^H1]\&[H01W$;. M, at 6\=/Z5*UT)?H+I;G8I(XL_#I_0X<7H,[\7])FH(OQHPP(FB@/@:S]76P
MU
AA5:.&A48-<XT:[MHJZI15J%15JY1UNY7"^1$\SLH6#+8S<5.E at P&[YJ+ MAHF6J\8JNV%]=L,2=E9]=E;U-2U]3U!PLATA at L3%0'1MA'T*07.0N>G?G37
M(43$9Q[HBPK_$#<6<AUD;>(W'=(3"D+3$D[C+*5>R6J]CM5-6Q^U4Z9GE% M0\.@[[E at DP"\C^[1T9'KD?TPZ64VJ9G;4M+T0$1 at F=SL9+0&!>'L3,56!B’
MJXDC^(%@?
:SLQ,NH=PBL/54^+WC=4,^-MG203#*!\&C\B.ACE<E
YM
MM’)#FGJ"D7QAQ/D.5ME::#&9DC
6I6J;F>N]=+(G/&)R<X0KEY(?!NLMXEY=
M’E/3MD7;3P[?4I,R/,N-0]E!"9L+R[/NI0!A"W8^JV%M5L,B5E9M5E;(ZOZ]
M_URF6K=Y_Z<;7Z4^_V=(V]_S2Z?9G_WV[^?Q[G[I6<5Z%AM<`)`9’V79;R MI9L6A_GA^_[XDS%).82EJ]4?4B\BQ/HM>
]]G+)ZC&S6?A>9’HL<C<+]NYY*
MB3<K8M’(D;-,?J^/(!,^Y-%‘G^OT’@2GGLKT%TZ;<#8L;A at Q>ZTB2(,
MRS6RHI-*.-SC!X2IH1-Q4MZT6.^!,;R7)P[6K?\K’!%]:'OH9&%O)5VK.< MP0;:W\9CMSH+5!JX^8WE6S9)DB1)DB1)DB1)DB1)DB1)DB1)DB1)DB1)DB1) ,WQG]#UW6;X4```
end

Paul Braman wrote:

I have attached some alpha code testing the following functions…

    SDL_CreateCond()
    SDL_DestroyCond()
    SDL_WaitCond()
    SDL_condSignalOne()
    SDL_condSignalAll()

…I have only tested on Linux and need some testing on other platforms.

For POSIX threads-enabled systems we can use the POSIX threads conditional
variables themselves.

Now, it turns out that if my design works as I think it does we can use
mutexes easily on systems that don’t support conditionals naturally.

Here’s the basic design for non-POSIX-enabled systems…

    SDL_CreateCond()

            Create and lock a mutex.

    SDL_WaitCond()

            Increment the reference count and attempt to lock mutex.
            When signaled decrement the reference count.

    SDL_condSignalOne()

            Unlock mutex.

    SDL_condSignalAll()

            Unlock mutex "reference count" times.

Thoughts on this design?
It seems good to me. As long as SDL_condSignalAll() is intended to
restart all blocked threads simultaneously (or thereabouts) then it
works.

Stuart

SDL_condSignalAll()

  Unlock mutex "reference count" times.

This is the one thing that doesn’t work.
You can’t guarantee that counting semaphores will be available.
You can only unlock the mutex once. Further unlocks are undefined.

-Sam Lantinga				(slouken at devolution.com)

Lead Programmer, Loki Entertainment Software–
“Any sufficiently advanced bug is indistinguishable from a feature”
– Rich Kulawiec

  Unlock mutex "reference count" times.

This is the one thing that doesn’t work.
You can’t guarantee that counting semaphores will be available.
You can only unlock the mutex once. Further unlocks are undefined.

Actually, I don’t rely on counting mutexes. I use a seperate integer as
the reference counter.

while (conditional->refcount--) {
	SDL_mutexV()
}

Unlocking the mutex causes another thread to obtain the lock. (With some
further fine-tuning of the synchronization.) The other thread that gets
the lock allows this thread to continue unlocking the mutex.

Note, this relies on the fact that semaphores are shared between threads.
It’s a hack for the mutexes, so it might as well be a hack with the
condition variables.

Paul Braman
@Paul_BramanOn Sat, 13 Nov 1999, Sam Lantinga wrote: