[x265] [PATCH] common: prevent an API race hazard

Steve Borho steve at borho.org
Tue Sep 24 00:23:07 CEST 2013


# HG changeset patch
# User Steve Borho <steve at borho.org>
# Date 1379974804 18000
#      Mon Sep 23 17:20:04 2013 -0500
# Node ID 3445ab59e0ce8fbe6760a22b4d97679abf05f8ec
# Parent  0e094f695420d4d8b495a152be6ccf4b8f66a7c9
common: prevent an API race hazard

The public API should be thread-safe for a given encoder or param object (they
are distinct data structures).  However x265 has a small number of global vars
that are configured on first use and must be the same for all encoders in the
same process (max CTU size and pixel bit-depth).  Using an atomic compare-and-
swap here prevents simultaneous encoder creations from violating those rules.

diff -r 0e094f695420 -r 3445ab59e0ce source/common/common.cpp
--- a/source/common/common.cpp	Mon Sep 23 17:08:04 2013 -0500
+++ b/source/common/common.cpp	Mon Sep 23 17:20:04 2013 -0500
@@ -25,6 +25,7 @@
 #include "TLibCommon/TComRom.h"
 #include "TLibCommon/TComSlice.h"
 #include "x265.h"
+#include "threading.h"
 #include "common.h"
 
 #include <stdio.h>
@@ -314,7 +315,7 @@
 
     static int once /* = 0 */;
 
-    if (once)
+    if (ATOMIC_CAS(&once, 0, 1) == 1)
     {
         if (param->maxCUSize != g_maxCUWidth)
         {
@@ -329,8 +330,6 @@
     }
     else
     {
-        once = 1;
-
         // set max CU width & height
         g_maxCUWidth  = param->maxCUSize;
         g_maxCUHeight = param->maxCUSize;


More information about the x265-devel mailing list