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

Thread-safety: initialization and tear-down should be automatic and thread-safe #6

Closed
nicowilliams opened this issue Mar 22, 2016 · 3 comments

Comments

@nicowilliams
Copy link

For initialization (onig_init()) use pthread_once() (POSIX) or InitOnceExecuteOnce() (WIN32).

For finalization (onig_end()) either do nothing (it's OK to leak some global state like Unicode tables) or use .fini section (ELF) or C++ destructors, or DllMain() (WIN32). This includes lazy initialization of the cclass table (which could have its own once-initializer), and the EUC/JIS Hiragana and Katakana property lists initialization (which could have its own once-initializer).

You can then leave onig_init() and onig_end() as empty stubs. For static-link archives it would help to have a --disable-thread option to not require -lpthread and to instead use a dumb once-initializer.

For USE_PARSE_TREE_NODE_RECYCLE use pthread_key_create() and pthread_setspecific() (POSIX) or thread-local storage and DllMain() (WIN32). Or just delete the USE_PARSE_TREE_NODE_RECYCLE code?

I think you can then drop all the THREAD_ATOMIC_START and THREAD_ATOMIC_END business and just declare that the remainder of thread safety is the application's job.

(I don't understand ONIG_STATE_DEC() though. Can you explain why it exists? reg->state doesn't look like a reference count...)

@nicowilliams
Copy link
Author

I should explain why this is important.

Libraries aren't used just by programs. Sometimes (often!) libraries are used by other libraries. Ensuring that there is only one call (or non-racing calls) to a library's initializer or finalizer is then impossible. The situation for finalizers is worse because a library might call another's finalizer in its own, but this might happen as a result of a last dlcose(), and if the lower-level library was still in use by other callers in the same process... boom.

For example, jq uses Oniguruma in its libjq.

@kkos
Copy link
Owner

kkos commented Mar 23, 2016

For ONIG_STATE_DEC() question.
This is because it avoids that regexp recompile and regexp search rise at the same time.
Regexp recompile API existed in old GNU regex. I supported it for compatibility.
However, I do not support it now. (USE_RECOMPILE_API is not defined.)
So, ONIG_STATE_INC() etc... is empty declarations.

@kkos
Copy link
Owner

kkos commented Apr 7, 2016

I have removed all THREAD_ macro in develop branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants