Commit 8d00d8b7 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

playlist: randomize in a thread-safe manner

parent f3096f3f
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#endif #endif
#include <vlc_common.h> #include <vlc_common.h>
#include <vlc_rand.h>
#define VLC_INTERNAL_PLAYLIST_SORT_FUNCTIONS #define VLC_INTERNAL_PLAYLIST_SORT_FUNCTIONS
#include "vlc_playlist.h" #include "vlc_playlist.h"
#include "playlist_internal.h" #include "playlist_internal.h"
...@@ -146,7 +147,7 @@ void playlist_ItemArraySort( unsigned i_items, playlist_item_t **pp_items, ...@@ -146,7 +147,7 @@ void playlist_ItemArraySort( unsigned i_items, playlist_item_t **pp_items,
for( i_position = i_items - 1; i_position > 0; i_position-- ) for( i_position = i_items - 1; i_position > 0; i_position-- )
{ {
i_new = rand() % (i_position+1); i_new = ((unsigned)vlc_mrand48()) % (i_position+1);
p_temp = pp_items[i_position]; p_temp = pp_items[i_position];
pp_items[i_position] = pp_items[i_new]; pp_items[i_position] = pp_items[i_new];
pp_items[i_new] = p_temp; pp_items[i_new] = p_temp;
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include <vlc_input.h> #include <vlc_input.h>
#include <vlc_interface.h> #include <vlc_interface.h>
#include <vlc_playlist.h> #include <vlc_playlist.h>
#include <vlc_rand.h>
#include "stream_output/stream_output.h" #include "stream_output/stream_output.h"
#include "playlist_internal.h" #include "playlist_internal.h"
...@@ -189,10 +190,9 @@ static void ResetCurrentlyPlaying( playlist_t *p_playlist, ...@@ -189,10 +190,9 @@ static void ResetCurrentlyPlaying( playlist_t *p_playlist,
if( var_GetBool( p_playlist, "random" ) ) if( var_GetBool( p_playlist, "random" ) )
{ {
/* Shuffle the array */ /* Shuffle the array */
srand( (unsigned int)mdate() ); for( unsigned j = p_playlist->current.i_size - 1; j > 0; j-- )
for( int j = p_playlist->current.i_size - 1; j > 0; j-- )
{ {
int i = rand() % (j+1); /* between 0 and j */ unsigned i = ((unsigned)vlc_mrand48()) % (j+1); /* between 0 and j */
playlist_item_t *p_tmp; playlist_item_t *p_tmp;
/* swap the two items */ /* swap the two items */
p_tmp = ARRAY_VAL(p_playlist->current, i); p_tmp = ARRAY_VAL(p_playlist->current, i);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment