Commit Graph
70 Commits
Author SHA1 Message Date
Martin Willi d36b30803e thread: Add a function to pop and call all registered cleanup handlers 2015-04-15 14:38:43 +02:00
Martin Willi ef33f8e85d thread: Don't acquire lock for thread_cleanup_push/pop
This is called only by the thread for its own thread_t, and does not need
synchronization.
2015-04-15 14:38:42 +02:00
Martin Willi 7394ad92ae thread: Remove unneeded thread startup synchronization
sem_init() is deprecated on OS X, and it actually fails with ENOSYS. Using our
wrapped semaphore object is not an option, as it relies on the thread cleanup
that we can't rely on at this stage.

It is unclear why startup synchronization is required, as we can allocate the
thread ID just before creating the pthread. There is a chance that we allocate
a thread ID for a thread that fails to create, but the risk and consequences
are negligible.
2015-04-13 14:50:45 +02:00
Martin Willi f65779dd0f apple: Introduce a central compatibility header with all __APPLE__ quirks 2014-11-21 12:02:08 +01:00
Martin Willi 78725e68c1 thread: Test for pending cancellation requests before poll()ing on OS X
As we are now using poll(2) instead of select(2), we need the work-around from
76dc329e for poll() as well.
2014-11-21 12:02:07 +01:00
Martin Willi 61e29aca0d threading: Support rwlock try_write_lock() on Windows
We explicitly avoided TryAcquireSRWLockExclusive() because of crashes. This
issue was caused by a MinGW-w64 bug (mingw-w64 fix 46f77afc). Using a newer
toolchain works fine.

While try_write_lock() obviously can fail, not supporting it is not really an
option, as some algorithms depend on occasionally successful calls. Certificate
caching in the certificate manager and the cred_set cache rely on successful
try_write_lock()ing.
2014-10-30 10:45:24 +01:00
Martin Willi 76dc329e47 thread: Test for pending cancellation requests before select()ing on OS X
This fixes some vici test cases on OS X, where the test thread tries to cancel
the watcher thread during cleanup, but fails as select() does not honor the
pre-issued cancellation request.
2014-10-14 16:34:44 +02:00
Tobias Brunner dbd7f4be31 mutex: Use atomics to set current thread in recursive mutex
Because this->thread is also read by threads that don't hold the
mutex the previous implementation was problematic (especially since
pthread_t is an opaque type of unknown length).

Fixes #654.
2014-09-09 10:56:15 +02:00
Tobias Brunner 32a262e7a1 windows: Remove useless assignment in put_thread() 2014-07-02 12:12:05 +02:00
Tobias Brunner e351169900 windows: Fix parameter name in Doxygen comment 2014-06-30 13:16:17 +02:00
Martin Willi 23750961d5 thread-value: Defer cleanup handling to thread termination on Windows
Instead of cleaning up all thread-values during destruction, cleanup handler
is invoked when a thread detaches. Thread detaching is cough using the Windows
DllMain() entry point, and allows us to basically revert 204098a7.

Using this mechanism, we make sure that the cleanup handler is invoked by the
the correct thread. Further, this mechanism works for externally-spawned
threads which run outside of our thread_cb() routine, and works more efficiently
with short-running threads.
2014-06-17 15:19:40 +02:00
Martin Willi cab59c73fc windows: Use WINAPI call convention for Windows API callbacks
For x86_64 it does not actually matter, but for i686 builds the call convention
is different with WINAPI.
2014-06-06 16:28:28 +02:00
Martin Willi 89e46c41f1 windows: Include for Vista instead of defining CondVar/SRWLock functions ourself 2014-06-04 15:53:05 +02:00
Martin Willi 204098a752 thread-value: Immediately cleanup all Windows TLS values on destroy 2014-06-04 15:53:02 +02:00
Martin Willi 9dec601f30 windows: Prevent queueing of multiple thread cancel APCs
This avoids any races during cleanup invocation if multiple cancel() requests
come in.
2014-06-04 15:53:01 +02:00
Martin Willi 0fa9c95811 windows: Provide a complete native Windows threading backend 2014-06-04 15:53:01 +02:00
Martin Willi c6b588bf06 thread: Add a Windows pthread variant to print thread identifiers 2014-06-03 12:24:34 +02:00
Tobias Brunner 0d30d73eb9 thread: Properly clean up meta data of main thread 2014-03-07 18:28:38 +01:00
Martin Willi bbb62267e0 thread: Note that tread_cancellation_point temporarily activates cancelability 2013-11-06 10:31:00 +01:00
Tobias Brunner 1dd58b0e21 Fixed some typos 2013-10-29 11:44:23 +01:00
Martin Willi 1a20a22d09 rwlock: Disable thread cancelability while waiting in (fallback) rwlock
An rwlock wait is not a thread cancellation point. As a canceled thread
would not have released the mutex, the rwlock would have been left in unusable
state.
2013-10-24 14:53:53 +02:00
Martin Willi 181d071363 rwlock: Don't use buggy pthread_rwlock on OS X
Recursive read locks don't seem to work properly, at least on 10.9.
2013-10-24 14:53:47 +02:00
Martin Willi b08292a520 semaphore: Support cancellation in wait functions of semaphore fallback
Semaphore wait functions should be a thread cancellation point, but did
not properly release the mutex in the fallback implementation.
2013-10-23 16:08:40 +02:00
Martin Willi 47c76c1b05 rwlock: Re-acquire rwlock even if condvar wait times out
A caller expects that the associated rwlock is held, whether the condvar
gets signaled or the wait times out.
2013-10-23 11:52:26 +02:00
Martin Willi 01c0267778 thread: implicitly create thread_t if an external thread calls thread_current() 2013-07-18 12:17:53 +02:00
Martin Willi b0ccd14f2a semaphore: similar to thread_create(), semaphore_create() is used by Mach
The compiler spits no warning, but the wrong symbol is used when calling
semaphore_create() from strongSwan. Override the name with a #define to force
the use of our semaphore_create().
2013-05-06 16:06:48 +02:00
Tobias Brunner bc07fef09c Use SIGUSR2 for SIG_CANCEL on Android
SIGRTMIN is defined as 32 while sigset_t is defined as
unsigned long (i.e. holds 32 signals).  Hence, the signal
could never be blocked.  Sending the signal still canceled
threads, but sometimes in situations where they shouldn't
have been canceled (e.g. while holding a lock).

Fixes #298.
2013-02-26 11:40:34 +01:00
Martin Willi 4185c64464 Use a ./configure check to detect pthread spinlock availability
_POSIX_SPIN_LOCKS does not seem to be defined correctly on all
systems (Debian libc 2.3.6). Fixes #262.
2012-12-18 09:51:33 +01:00
Tobias Brunner f05b427265 Moved debug.[ch] to utils folder 2012-10-24 16:00:51 +02:00
Tobias Brunner 12642a6831 Moved data structures to new collections subfolder 2012-10-24 16:00:49 +02:00
Tobias Brunner eecd41e349 Use a helper function to add milliseconds to timeval structs 2012-10-18 12:25:59 +02:00
Tobias Brunner e811cf152a Properly handle thread cancelation in rwlock_condvar_t 2012-09-21 18:16:27 +02:00
Tobias Brunner 60dc44648f Added a condvar implementation that works with rwlock_t 2012-09-21 18:16:27 +02:00
Martin Willi 88a5abf5e2 Don't use POSIX semaphores if a MONOTONIC clock is available
POSIX semaphores use CLOCK_REALTIME, but our semaphore_t abstraction
expects CLOCK_MONOTONIC based times. Use the mutex/condvar based
fallback if time_monotonic() actuall returns monotonic times.
2012-08-20 18:01:20 +02:00
Martin Willi 907d3d0f1d Add a mutex/condvar based semaphore implementation if sem_timedwait is unavailable
Fixes #214.
2012-08-20 17:20:26 +02:00
Tobias Brunner 09ae3d79ca Merge branch 'android-app'
This branch introduces a userland IPsec implementation (libipsec) and an
Android App which targets the VpnService API that is provided by Android 4+.

The implementation is based on the bachelor thesis 'Userland IPsec for
Android 4' by Giuliano Grassi and Ralf Sager.
2012-08-13 12:07:52 +02:00
Tobias Brunner efbb5e8c57 Ensure thread IDs always start with 1 even if the library is reused
Within the Android App the library stays loaded in memory and is just
initialized/deinitialized with each connection, the static thread
counter would continuously increase without this patch.
2012-08-13 11:28:08 +02:00
Martin Willi a63192479d If _POSIX_SPIN_LOCKS is defined as -1, it is not available 2012-08-10 17:02:46 +02:00
Tobias Brunner decc467a4f Implemented recursive mutex without thread-specific counter 2012-08-03 11:30:18 +02:00
Tobias Brunner 920f29e7d5 Use a single thread-specific value for our custom rwlock_t implementation
The pthread implementation on Android currently only supports 64
different thread-specific values per process, which we hit easily when
every rwlock_t requires one.
2012-08-03 11:30:18 +02:00
Tobias Brunner 2b19dd3502 Properly cleanup thread-local values for the threads destroying thread_value_t objects 2012-07-12 09:43:27 +02:00
Tobias Brunner 12fa85c664 Added wrapper for POSIX spin locks 2012-07-04 10:13:49 +02:00
Tobias Brunner 4d21000cf7 Added recursive read_lock support to our own implementation of rwlock_t. 2012-05-02 14:45:38 +02:00
Tobias Brunner 8f6c13271c Added a wrapper class around POSIX semaphores. 2012-05-02 14:45:37 +02:00
Tobias Brunner 17e3a92661 Fix gettid() on Android, which is defined in unistd.h there. 2012-01-12 11:08:22 +01:00
Tobias Brunner 66f16d9629 Use native gettid() if available (which is the case on Android). 2012-01-10 18:31:33 +01:00
Tobias Brunner c17f6f96e2 Log native thread ID when a thread is created.
If possible gettid() is used, otherwise pthread_self() is logged (which is
not completely portable, but seems to work on most supported platforms).
2011-12-16 16:44:38 +01:00
Martin Willi 005b02cbe3 Create a dummy pthread key for value "0", as some buggy PKCS#11 libraries mangle it 2011-12-07 13:42:07 +01:00
Tobias Brunner c8541efe70 Migrated thread_t to INIT/METHOD macros. 2011-10-03 15:54:36 +02:00
Tobias Brunner f5808078cb Migrated mutex_t to INIT/METHOD macros. 2011-10-03 15:45:17 +02:00