Skip to content

Commit

Permalink
InitializeStaticPointers: make init thread safe
Browse files Browse the repository at this point in the history
fixes TSan warnings:
Read of size 8 at 0x7f6c00f850e8 by thread T914:
    #0 InitializeStaticPointers
    #1 sjpeg::Encoder::Encoder(SjpegYUVMode, int, int, sjpeg::ByteSink*)
...
  Previous write of size 8 at 0x7f6c00f850e8 by thread T875:
    #0 InitializeStaticPointers
    #1 sjpeg::Encoder::Encoder(SjpegYUVMode, int, int, sjpeg::ByteSink*)
  • Loading branch information
jzern committed May 18, 2024
1 parent fb8c9d3 commit 2374ba9
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/enc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <math.h>
#include <float.h> // for FLT_MAX
#include <stdint.h>

#include <mutex> // NOLINT
#include <new>

#define SJPEG_NEED_ASM_HEADERS
Expand Down Expand Up @@ -256,12 +258,13 @@ void (*Encoder::fDCT_)(int16_t* in, int num_blocks) = nullptr;
Encoder::StoreHistoFunc Encoder::store_histo_ = nullptr;

void Encoder::InitializeStaticPointers() {
if (fDCT_ == nullptr) {
static std::once_flag once;
std::call_once(once, []() {
store_histo_ = GetStoreHistoFunc();
quantize_block_ = GetQuantizeBlockFunc();
quantize_error_ = GetQuantizeErrorFunc();
fDCT_ = GetFdct();
}
});
}

////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 2374ba9

Please sign in to comment.