Skip to content

Commit

Permalink
Merge pull request #1375 from ampli/em-detect
Browse files Browse the repository at this point in the history
Fix the thread code inclusion problem in Emscripten builds
  • Loading branch information
linas authored Jan 18, 2023
2 parents 43d2392 + 59bf7d5 commit 750315d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,6 @@ data/th/old_words/**
# Visual Studio keeps adding the directory ".vs". If it got added while
# rebasing and it is not git-ignored, it somehow breaks the rebase.
.vs
# a.wasm is created by "emcc" (in "emconfigure ./configure").
# For now just ignore it.
a.wasm
48 changes: 19 additions & 29 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -119,38 +119,28 @@ AC_CHECK_FUNCS(prctl)
AC_DEFINE(__STDC_FORMAT_MACROS)
AC_DEFINE(__STDC_LIMIT_MACROS)

# Don't use thread functions with Emscripten.
# Emscripten does not like threads.
if test "$EMSCRIPTEN" != ''; then
AC_CHECK_DEFINE(__EMSCRIPTEN__, [emscripten=yes],
[AC_MSG_ERROR([EMSCRIPTEN environment variable found, but emcc is not used])])
fi

if test "x$emscripten" != "xyes"; then

# Check for a keyword for Thread Local Storage declaration.
# If found - define TLS to it.
lg_tls=no
AX_TLS(,:)
test "$ac_cv_tls" != "none" && lg_tls=yes

AX_PTHREAD
if test -n "$ax_pthread_ok"; then
CC="${PTHREAD_CC:-CC}"
dnl #CCX="${PTHREAD_CC:-CCX}"

dnl Currently, libtool uses --nostdlib when linking shared libraries,
dnl which causes the -pthread flag to be ignored. Try to work around that
dnl by explicitly specifying the pthread library unless it is already set.
if test -z "$PTHREAD_LIBS"; then
PTHREAD_LIBS=-lpthread
fi
# Check for a keyword for Thread Local Storage declaration.
# If found - define TLS to it.
lg_tls=no
AX_TLS(,:)
test "$ac_cv_tls" != "none" && lg_tls=yes

AX_PTHREAD
if test -n "$ax_pthread_ok"; then
CC="${PTHREAD_CC:-CC}"
dnl #CCX="${PTHREAD_CC:-CCX}"

dnl Currently, libtool uses --nostdlib when linking shared libraries,
dnl which causes the -pthread flag to be ignored. Try to work around that
dnl by explicitly specifying the pthread library unless it is already set.
if test -z "$PTHREAD_LIBS"; then
PTHREAD_LIBS=-lpthread
fi

dnl Check if we can use C11 threads functions
AC_CHECK_HEADERS_ONCE([threads.h])
fi

dnl Check if we can use C11 threads functions
AC_CHECK_HEADERS_ONCE([threads.h])

dnl If the visibility __attribute__ is supported, define HAVE_VISIBILITY
dnl and a variable CFLAG_VISIBILITY, to be added to CFLAGS/CXXFLAGS.
LG_C_ATTRIBUTE_VISIBILITY
Expand Down
8 changes: 4 additions & 4 deletions link-grammar/dict-common/regex-morph.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
/* */
/*************************************************************************/

#if HAVE_THREADS_H
#if HAVE_THREADS_H && !__EMSCRIPTEN__
#include <threads.h>
#endif
#endif // HAVE_THREADS_H && !__EMSCRIPTEN__

/**
* Support for the regular-expression based token matching system
Expand Down Expand Up @@ -177,7 +177,7 @@ static void alloc_key(void)

static pcre2_match_data* alloc_match_data(void)
{
#if HAVE_THREADS_H
#if HAVE_THREADS_H && !__EMSCRIPTEN__
call_once(&call_once_flag, alloc_key);

pcre2_match_data *pmd = (pcre2_match_data *) tss_get(re_md_key);
Expand All @@ -194,7 +194,7 @@ static pcre2_match_data* alloc_match_data(void)
pmd = pcre2_match_data_create(MAX_CAPTURE_GROUPS, NULL);
if (pmd) return pmd;

#endif // HAVE_THREADS_H
#endif // HAVE_THREADS_H && !__EMSCRIPTEN__
prt_error("Error: pcre2_match_data_create() failed\n");
return NULL;
}
Expand Down
12 changes: 6 additions & 6 deletions link-grammar/parse/count.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

#include <limits.h>
#include <inttypes.h> // PRIu64
#if HAVE_THREADS_H
#if HAVE_THREADS_H && !__EMSCRIPTEN__
#include <threads.h>
#endif /* HAVE_THREADS_H */
#endif /* HAVE_THREADS_H && !__EMSCRIPTEN__ */

#include "link-includes.h"
#include "api-structures.h"
Expand Down Expand Up @@ -124,7 +124,7 @@ struct count_context_s
#define MAX_TABLE_SIZE(s) (s / 10) /* Low load factor, for speed */
#define MAX_LOG2_TABLE_SIZE ((sizeof(size_t)==4) ? 25 : 34)

#if HAVE_THREADS_H
#if HAVE_THREADS_H && !__EMSCRIPTEN__
/* Each thread will get it's own version of the `kept_table`.
* If the program creates zillions of threads, then there will
* be a mem-leak if this table is not released when each thread
Expand All @@ -147,7 +147,7 @@ static void make_key(void)
{
tss_create(&key, free_tls_table);
}
#endif /* HAVE_THREADS_H */
#endif /* HAVE_THREADS_H && !__EMSCRIPTEN__ */

/**
* Allocate memory for the connector-pair table and initialize table-size
Expand All @@ -164,14 +164,14 @@ static void table_alloc(count_context_t *ctxt, unsigned int shift)
static TLS Table_connector **kept_table = NULL;
static TLS unsigned int log2_kept_table_size = 0;

#if HAVE_THREADS_H
#if HAVE_THREADS_H && !__EMSCRIPTEN__
// Install a thread-exit handler, to free kept_table on thread-exit.
static once_flag flag = ONCE_FLAG_INIT;
call_once(&flag, make_key);

if (NULL == kept_table)
tss_set(key, &kept_table);
#endif /* HAVE_THREADS_H */
#endif /* HAVE_THREADS_H && !__EMSCRIPTEN__ */

if (shift == 0)
shift = ctxt->log2_table_size + 1; /* Double the table size */
Expand Down

0 comments on commit 750315d

Please sign in to comment.