+
+// This is called by the main CPU thread every ~21.333 cycles.
+void WriteSampleToBuffer(void)
+{
+//WriteLog("WriteSampleToBuffer(): SDL_mutexP(mutex2)\n");
+ SDL_mutexP(mutex2);
+
+ // This should almost never happen, but...
+ while (soundBufferPos >= (SOUND_BUFFER_SIZE - 1))
+ {
+//WriteLog("WriteSampleToBuffer(): Waiting for sound thread. soundBufferPos=%i, SOUNDBUFFERSIZE-1=%i\n", soundBufferPos, SOUND_BUFFER_SIZE-1);
+ SDL_mutexV(mutex2); // Release it so sound thread can get it,
+ SDL_mutexP(mutex); // Must lock the mutex for the cond to work properly...
+ SDL_CondWait(conditional, mutex); // Sleep/wait for the sound thread
+ SDL_mutexV(mutex); // Must unlock the mutex for the cond to work properly...
+ SDL_mutexP(mutex2); // Re-lock it until we're done with it...
+ }
+
+ soundBuffer[soundBufferPos++] = sample;
+//WriteLog("WriteSampleToBuffer(): SDL_mutexV(mutex2)\n");
+ SDL_mutexV(mutex2);
+}
+
+