Browse Source

Reduce sound mutex lock range

louyihua 8 years ago
parent
commit
6b1286beb9
6 changed files with 41 additions and 32 deletions
  1. 7 9
      audio.c
  2. 3 2
      mp3play.c
  3. 3 2
      oggplay.c
  4. 9 7
      players.h
  5. 12 11
      rixplay.cpp
  6. 7 1
      sound.c

+ 7 - 9
audio.c

@@ -249,7 +249,7 @@ AUDIO_OpenDevice(
    //
    // Initialize the sound subsystem.
    //
-   gAudioDevice.pSoundPlayer = SOUND_Init();
+   gAudioDevice.pSoundPlayer = SOUND_Init(gAudioDevice.mtx);
 
    //
    // Initialize the music subsystem.
@@ -257,21 +257,21 @@ AUDIO_OpenDevice(
    switch (gConfig.eMusicType)
    {
    case MUSIC_RIX:
-	   if (!(gAudioDevice.pMusPlayer = RIX_Init(va("%s%s", gConfig.pszGamePath, "mus.mkf"))))
+	   if (!(gAudioDevice.pMusPlayer = RIX_Init(va("%s%s", gConfig.pszGamePath, "mus.mkf"), gAudioDevice.mtx)))
 	   {
-		   gAudioDevice.pMusPlayer = RIX_Init(va("%s%s", gConfig.pszGamePath, "MUS.MKF"));
+		   gAudioDevice.pMusPlayer = RIX_Init(va("%s%s", gConfig.pszGamePath, "MUS.MKF"), gAudioDevice.mtx);
 	   }
 	   break;
    case MUSIC_MP3:
 #if PAL_HAS_MP3
-	   gAudioDevice.pMusPlayer = MP3_Init(NULL);
+	   gAudioDevice.pMusPlayer = MP3_Init(gAudioDevice.mtx);
 #else
 	   gAudioDevice.pMusPlayer = NULL;
 #endif
 	   break;
    case MUSIC_OGG:
 #if PAL_HAS_OGG
-	   gAudioDevice.pMusPlayer = OGG_Init(NULL);
+	   gAudioDevice.pMusPlayer = OGG_Init(gAudioDevice.mtx);
 #else
 	   gAudioDevice.pMusPlayer = NULL;
 #endif
@@ -314,14 +314,14 @@ AUDIO_OpenDevice(
    }
    case MUSIC_MP3:
 #if PAL_HAS_MP3
-	   gAudioDevice.pCDPlayer = MP3_Init(NULL);
+	   gAudioDevice.pCDPlayer = MP3_Init(gAudioDevice.mtx);
 #else
 	   gAudioDevice.pCDPlayer = NULL;
 #endif
 	   break;
    case MUSIC_OGG:
 #if PAL_HAS_OGG
-	   gAudioDevice.pCDPlayer = OGG_Init(NULL);
+	   gAudioDevice.pCDPlayer = OGG_Init(gAudioDevice.mtx);
 #else
 	   gAudioDevice.pCDPlayer = NULL;
 #endif
@@ -489,12 +489,10 @@ AUDIO_PlaySound(
 
 --*/
 {
-   SDL_mutexP(gAudioDevice.mtx);
    if (gAudioDevice.pSoundPlayer)
    {
       gAudioDevice.pSoundPlayer->Play(gAudioDevice.pSoundPlayer, abs(iSoundNum), FALSE, 0.0f);
    }
-   SDL_mutexV(gAudioDevice.mtx);
 }
 
 VOID

+ 3 - 2
mp3play.c

@@ -138,8 +138,8 @@ MP3_Play(
 
 LPAUDIOPLAYER
 MP3_Init(
-	LPCSTR szFileName
-	)
+	SDL_mutex *mutex
+)
 {
 	LPMP3PLAYER player;
 	if (player = (LPMP3PLAYER)malloc(sizeof(MP3PLAYER)))
@@ -147,6 +147,7 @@ MP3_Init(
 		player->FillBuffer = MP3_FillBuffer;
 		player->Play = MP3_Play;
 		player->Shutdown = MP3_Shutdown;
+		player->mutex = mutex;
 
 		player->pMP3 = NULL;
 		player->iMusic = -1;

+ 3 - 2
oggplay.c

@@ -472,8 +472,8 @@ OGG_Shutdown(
 
 LPAUDIOPLAYER
 OGG_Init(
-	LPCSTR szFileName
-	)
+	SDL_mutex *mutex
+)
 {
 	LPOGGPLAYER player;
 	if (player = (LPOGGPLAYER)malloc(sizeof(OGGPLAYER)))
@@ -483,6 +483,7 @@ OGG_Init(
 		player->FillBuffer = OGG_FillBuffer;
 		player->Play = OGG_Play;
 		player->Shutdown = OGG_Shutdown;
+		player->mutex = mutex;
 
 		player->fp = NULL;
 		player->iMusic = -1;

+ 9 - 7
players.h

@@ -31,12 +31,13 @@ extern "C"
 {
 #endif
 
-typedef struct tagAUDIOPLAYER
-{
+	typedef struct tagAUDIOPLAYER
+	{
 #define AUDIOPLAYER_COMMONS \
 	VOID (*Shutdown)(VOID*); \
 	BOOL (*Play)(VOID*, INT, BOOL, FLOAT); \
-	VOID (*FillBuffer)(VOID*, LPBYTE, INT)
+	VOID (*FillBuffer)(VOID*, LPBYTE, INT); \
+	SDL_mutex *mutex
 
 	AUDIOPLAYER_COMMONS;
 } AUDIOPLAYER, *LPAUDIOPLAYER;
@@ -45,7 +46,8 @@ typedef struct tagAUDIOPLAYER
 
 LPAUDIOPLAYER
 RIX_Init(
-   LPCSTR     szFileName
+   LPCSTR     szFileName,
+   SDL_mutex *mutex
 );
 
 /* OGG */
@@ -53,7 +55,7 @@ RIX_Init(
 
 LPAUDIOPLAYER
 OGG_Init(
-	LPCSTR    szFileName
+	SDL_mutex *mutex
 );
 
 #endif
@@ -63,14 +65,14 @@ OGG_Init(
 
 LPAUDIOPLAYER
 MP3_Init(
-	LPCSTR    szFileName
+	SDL_mutex *mutex
 );
 
 #endif
 
 LPAUDIOPLAYER
 SOUND_Init(
-	VOID
+	SDL_mutex *mutex
 );
 
 #ifdef __cplusplus

+ 12 - 11
rixplay.cpp

@@ -375,22 +375,22 @@ RIX_Play(
 
 LPAUDIOPLAYER
 RIX_Init(
-	LPCSTR     szFileName
-	)
-	/*++
-	Purpose:
+	LPCSTR     szFileName,
+	SDL_mutex *mutex
+)
+/*++
+  Purpose:
 
-	Initialize the RIX player subsystem.
+    Initialize the RIX player subsystem.
 
-	Parameters:
+  Parameters:
 
-	[IN]  szFileName - Filename of the mus.mkf file.
+    [IN]  szFileName - Filename of the mus.mkf file.
 
-	Return value:
-
-	0 if success, -1 if cannot allocate memory, -2 if file not found.
+  Return value:
 
-	--*/
+    0 if success, -1 if cannot allocate memory, -2 if file not found.
+--*/
 {
 	LPRIXPLAYER pRixPlayer = new RIXPLAYER;
 	if (pRixPlayer == NULL)
@@ -403,6 +403,7 @@ RIX_Init(
 		pRixPlayer->FillBuffer = RIX_FillBuffer;
 		pRixPlayer->Shutdown = RIX_Shutdown;
 		pRixPlayer->Play = RIX_Play;
+		pRixPlayer->mutex = mutex;
 	}
 
 	if (gConfig.fUseSurroundOPL)

+ 7 - 1
sound.c

@@ -840,6 +840,8 @@ SOUND_Play(
 		return FALSE;
 	}
 
+	SDL_mutexP(player->mutex);
+
 	cursnd = &player->soundlist;
 	while (cursnd->next && cursnd->base)
 		cursnd = cursnd->next;
@@ -868,6 +870,8 @@ SOUND_Play(
 	cursnd->ResampleMix = mixer;
 	player->cursounds++;
 
+	SDL_mutexV(player->mutex);
+
 	return TRUE;
 }
 
@@ -961,7 +965,7 @@ SOUND_FillBuffer(
 
 LPAUDIOPLAYER
 SOUND_Init(
-	VOID
+	SDL_mutex *mutex
 )
 /*++
   Purpose:
@@ -1003,6 +1007,8 @@ SOUND_Init(
 			player->Play = SOUND_Play;
 			player->FillBuffer = SOUND_FillBuffer;
 			player->Shutdown = SOUND_Shutdown;
+			player->mutex = mutex;
+
 			player->LoadSound = func[i];
 			player->mkf = mkf;
 			player->soundlist.resampler[0] = resampler_create();