[vlc-commits] [Git][videolan/vlc][master] 3 commits: posix/rand: use timespec_get instead of NTP time
Steve Lhomme (@robUx4)
gitlab at videolan.org
Fri Jul 14 14:53:50 UTC 2023
Steve Lhomme pushed to branch master at VideoLAN / VLC
Commits:
961a977c by Steve Lhomme at 2023-07-14T14:29:54+00:00
posix/rand: use timespec_get instead of NTP time
The NTP time is basically time timespec_get(TIME_UTC) with bits shifted.
vlc_tick_now is clock_gettime(CLOCK_MONOTONIC) also giving a timespec
but not shifted.
We don't need the the bit and EPOCH shifting.
- - - - -
271d5fc2 by Steve Lhomme at 2023-07-14T14:29:54+00:00
posix/rand: increment the buffer size by the amount it's filled
It is 16 but it's better not to hardcode the value. It's also more logical
compared to the break condition of the loop.
- - - - -
0e9a1033 by Steve Lhomme at 2023-07-14T14:29:54+00:00
posix/rand: use VLC mutex instead of pthread
It only locks in the (unlikely) case this function is called concurrently.
- - - - -
1 changed file:
- src/posix/rand.c
Changes:
=====================================
src/posix/rand.c
=====================================
@@ -32,11 +32,11 @@
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
-#include <pthread.h>
#include <vlc_fs.h>
#include <vlc_hash.h>
#include <vlc_tick.h>
+#include <vlc_threads.h>
/*
* Pseudo-random number generator using a HMAC-MD5 in counter mode.
@@ -76,10 +76,11 @@ static void vlc_rand_init (void)
void vlc_rand_bytes (void *buf, size_t len)
{
- static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
+ static vlc_mutex_t lock = VLC_STATIC_MUTEX;
static uint64_t counter = 0;
+ struct timespec ts;
- uint64_t stamp = vlc_ntp_time ();
+ timespec_get(&ts, TIME_UTC);
while (len > 0)
{
@@ -91,16 +92,16 @@ void vlc_rand_bytes (void *buf, size_t len)
vlc_hash_md5_Init (&mdi);
vlc_hash_md5_Init (&mdo);
- pthread_mutex_lock (&lock);
+ vlc_mutex_lock (&lock);
if (counter == 0)
vlc_rand_init ();
val = counter++;
vlc_hash_md5_Update (&mdi, ikey, sizeof (ikey));
vlc_hash_md5_Update (&mdo, okey, sizeof (okey));
- pthread_mutex_unlock (&lock);
+ vlc_mutex_unlock (&lock);
- vlc_hash_md5_Update (&mdi, &stamp, sizeof (stamp));
+ vlc_hash_md5_Update (&mdi, &ts, sizeof (ts));
vlc_hash_md5_Update (&mdi, &val, sizeof (val));
vlc_hash_md5_Finish (&mdi, mdi_buf, sizeof(mdi_buf));
vlc_hash_md5_Update (&mdo, mdi_buf, sizeof(mdi_buf));
@@ -111,7 +112,7 @@ void vlc_rand_bytes (void *buf, size_t len)
if (len < sizeof(mdo_buf))
break;
- len -= 16;
- buf = ((uint8_t *)buf) + 16;
+ len -= sizeof(mdo_buf);
+ buf = ((uint8_t *)buf) + sizeof(mdo_buf);
}
}
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/33bf55c40a0b12821b95c7730b174929597b029d...0e9a1033b10b3e58074e7da620df382585d0ff94
--
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/33bf55c40a0b12821b95c7730b174929597b029d...0e9a1033b10b3e58074e7da620df382585d0ff94
You're receiving this email because of your account on code.videolan.org.
VideoLAN code repository instance
More information about the vlc-commits
mailing list