Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pthread_once to call bindtextdomain only once #32

Open
wants to merge 1 commit into
base: i18n
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,25 @@ ENDIF()
STRING(REPLACE ";" " " libdiscid_OSDEP_STR "${libdiscid_OSDEP_SRCS}")
MESSAGE(STATUS "Using discid implementation ${libdiscid_OSDEP_STR}")

# check if pthread is available
IF(ENABLE_NLS)
SET(CMAKE_THREAD_PREFER_PTHREAD TRUE)
IF(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER MATCHES "clang")
# FIND_PACKAGE(Threads) prefers -lpthread over -pthread, -pthread is
# recommend though
SET(THREADS_HAVE_PTHREAD_ARG "-pthread")
ENDIF()
FIND_PACKAGE(Threads)
IF(CMAKE_USE_PTHREADS_INIT)
SET(HAVE_PTHREAD TRUE)
SET(libdiscid_LIBS ${LIBDISCID_LIBS} ${CMAKE_THREAD_LIBS_INIT})
IF(${CMAKE_THREAD_LIBS_INIT} MATCHES "-pthread")
# if we have -pthread, also pass -pthread to CC
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
ENDIF()
ENDIF()
ENDIF()

ADD_LIBRARY(libdiscid SHARED ${libdiscid_OSDEP_SRCS} ${libdiscid_RCS} src/base64.c src/disc.c src/sha1.c)
TARGET_LINK_LIBRARIES(libdiscid ${libdiscid_OSDEP_LIBS} ${libdiscid_LIBS})
SET_TARGET_PROPERTIES(libdiscid PROPERTIES
Expand Down
3 changes: 3 additions & 0 deletions config-cmake.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
/* enable string translation (gettext) */
#cmakedefine ENABLE_NLS

/* pthread is available */
#cmakedefine HAVE_PTHREAD

/**
* Values needed by our sha1.h
*/
Expand Down
8 changes: 8 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ AC_CHECK_SIZEOF(long)

AM_GNU_GETTEXT([external])
AM_GNU_GETTEXT_VERSION(0.18.1)
if test "$USE_NLS" = "yes"; then
AX_PTHREAD([
AC_DEFINE(HAVE_PTHREAD, [], "pthread is available")
LIBS="$PTHREAD_LIBS $LIBS"
CFLAGS="$CLAFGS $PTHREAD_CFLAGS"
CC="$PTHREAD_CC"
])
fi

if test "$cross_compiling" = "yes" && test "$os" = "win32"; then
AC_MSG_WARN([detected cross compilation: disabling tests!!!])
Expand Down
17 changes: 16 additions & 1 deletion src/disc.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@
#define TRACK_NUM_IS_VALID(disc, i) \
( i >= disc->first_track_num && i <= disc->last_track_num )

#ifdef ENABLE_NLS
static void init_nls(void) {
bindtextdomain(PACKAGE, LOCALEDIR);
}

#ifdef HAVE_PTHREAD
#include <pthread.h>
static pthread_once_t nls_initialized = PTHREAD_ONCE_INIT;
#endif
#endif


static void create_disc_id(mb_disc_private *d, char buf[]);
static void create_freedb_disc_id(mb_disc_private *d, char buf[]);
Expand All @@ -61,7 +72,11 @@ DiscId *discid_new() {
* just in case the prefix was set weird
*/
#ifdef ENABLE_NLS
bindtextdomain(PACKAGE, LOCALEDIR);
#ifdef HAVE_PTHREAD
pthread_once(&nls_initialized, init_nls);
#else
init_nls();
#endif
#endif
printf(_("one two three\n"));
/* initializes everything to zero */
Expand Down