aboutsummaryrefslogtreecommitdiff
path: root/winsup/cygwin/thread.cc
AgeCommit message (Collapse)AuthorFilesLines
2024-11-12Cygwin: Change pthread_sigqueue() to accept thread idMark Geisert1-4/+4
Change the first parameter of pthread_sigqueue() to be a thread id rather than a thread pointer. The change is to match the Linux implementation of this function. The user-visible function prototype is changed in include/pthread.h. The pthread_sigqueue() function is modified to work with a passed-in thread id rather than an indirect thread pointer as before. (It used to be "pthread_t *thread", i.e., class pthread **.) The release note for Cygwin 3.5.5 is updated. Reported-by: Christian Franke <Christian.Franke@t-online.de> Addresses: https://cygwin.com/pipermail/cygwin/2024-September/256439.html Signed-off-by: Mark Geisert <mark@maxrnd.com> Fixes: 50350cafb375 ("* cygwin.din (pthread_sigqueue): Export.")
2024-06-02Cygwin: pthread: Fix a race issue introduced by the commit 2c5433e5da82Takashi Yano1-16/+20
To avoid race issues, pthread::once() uses pthread_mutex. This caused the handle leak which was fixed by the commit 2c5433e5da82. However, this fix introduced another race issue, i.e., the mutex may be used after it is destroyed. This patch fixes the issue. Special thanks to Bruno Haible for discussing how to fix this. Addresses: https://cygwin.com/pipermail/cygwin/2024-May/255987.html Reported-by: Bruno Haible <bruno@clisp.org> Fixes: 2c5433e5da82 ("Cygwin: pthread: Fix handle leak in pthread_once.") Reviewed-by: Ken Brown <kbrown@cornell.edu> Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-01-24Cygwin: pthread: Fix handle leak in pthread_once.Takashi Yano1-0/+3
If pthread_once() is called with pthread_once_t initialized using PTREAD_ONCE_INIT, pthread_once does not release pthread_mutex used internally. This patch fixes that by calling pthread_mutex_destroy() in the thread which has called init_routine. Reviewed-by: Corinna Vinschen <corinna@vinschen.de> Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2023-07-18Cygwin: don't wait infinitely on a pthread cancel eventCorinna Vinschen1-6/+3
Starting with commit 42faed412857 ("* thread.h (class pthread): Add bool member canceled."), pthread::testcancel waits infinitely on cancel_event after it checked if the canceled variable is set. However, this might introduce a deadlock, if the thread calling pthread_cancel is terminated after setting canceled to true, but before calling SetEvent on cancel_event. In fact, it's not at all necessary to wait infinitely. By definition, the thread is only canceled if cancel_event is set. The canceled variable is just a helper to speed up code. We can safely assume that the thread hasn't been canceled yet, if canceled is set, but cancel_event isn't. Fixes: 42faed412857 ("* thread.h (class pthread): Add bool member canceled.") Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2023-07-18Cygwin: pthread: Take note of schedparam in pthread_createJon Turney1-0/+1
Take note of schedparam in any pthread_attr_t passed to pthread_create. postcreate() (racily, after the thread is actually created), sets the scheduling priority if it's inherited, but precreate() doesn't store any scheduling priority explicitly set via a non-default attr to pthread_create, so schedparam.sched_priority has the default value of 0. (I think this is another long-standing bug exposed by 4b51e4c1. Now we don't lie about the actual thread priority, it's apparent it's not really being set in this case.) Fixes testcase priority2. Signed-off-by: Jon Turney <jon.turney@dronecode.org.uk>
2023-06-27Cygwin: thread: Reset _my_tls.tid if it's pthread_null in init_mainthread().Takashi Yano1-1/+1
Currently, _my_tls.tid is set to pthread_null if pthread::self() is called before pthread::init_mainthread(). As a result, pthread:: init_mainthread() does not set _my_tls.tid appropriately. Due to this, pthread_join() fails in LDAP environment if the program is the first program which loads cygwin1.dll. https://cygwin.com/pipermail/cygwin/2023-June/253792.html With this patch, _my_tls.tid is re-initialized in pthread:: init_mainthread() if it is pthread_null. Reported-by: Mümin A. <muminaydin06@gmail.com> Reviewed-by: Corinna Vinschen <corinna@vinschen.de> Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2022-08-10Cygwin: move POSIX semaphore API functions to posix_ipc.ccCorinna Vinschen1-50/+0
This way, the sem API is all in the same place, even if the underlying semaphore class is still in thread.cc. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2022-08-10Cygwin: rename CygwinCreateThread to create_posix_threadCorinna Vinschen1-3/+3
Rename CygwinCreateThread to create_posix_thread and move from miscfuncs.cc to create_posix_thread.cc, inbcluding all related functions. Analogue for the prototypes. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2022-08-09Cygwin: pthreads: merge pthread.cc into thread.ccCorinna Vinschen1-1321/+1509
provide entire internal and external pthread API from inside the same file. While I dislike to have another even larger file, this is basically cleaning up the source and grouping the external API into useful chunks. Splitting the file cleanly is tricky due to usage of inline methods is_good_object and verifyable_object_isvalid. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2022-08-04Cygwin: drop all usages of WINAPICorinna Vinschen1-1/+1
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2022-05-29Cygwin: remove miscellaneous 32-bit codeKen Brown1-4/+0
2022-02-22Make __sdidinit unusedMatt Joyce1-2/+2
Remove dependency on __sdidinit member of struct _reent to check object initialization. Like __sdidinit, the __cleanup member of struct _reent is initialized in the __sinit() function. Checking initialization against __cleanup serves the same purpose and will reduce overhead in the __sfp() function in a follow up patch.
2021-11-22Cygwin: pthread_kill: check for invalid signal numberCorinna Vinschen1-0/+4
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2021-08-23fix race condition in List_insertAleksand Malikov1-4/+4
Revert mx parameter and mutex lock while operating the list. Mutex was removed with 94d24160 informing that: 'Use InterlockedCompareExchangePointer to ensure race safeness without using a mutex.' But it does not. Calling pthread_mutex_init and pthread_mutex_destroy from two or more threads occasionally leads to hang in pthread_mutex_destroy. To not change the behaviour of other cases where List_insert was called, List_insert_nolock is added.
2021-07-29Cygwin: implement pthread_rwlock_clockrdlock/pthread_rwlock_clockwrlockCorinna Vinschen1-4/+18
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2021-07-29Cygwin: implement pthread_mutex_clocklockCorinna Vinschen1-2/+9
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2021-07-29Cygwin: implement pthread_cond_clockwaitCorinna Vinschen1-11/+43
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2021-07-29Cygwin: implement sem_clockwaitCorinna Vinschen1-2/+3
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-08-30Cygwin: pthreads: iterate over key destructors per POSIXCorinna Vinschen1-0/+4
POSIX requires that key destructors are called in a loop for each key with a non-NULL value until all values are NULL, or until all destructors for non-NULL values have been called at least PTHREAD_DESTRUCTOR_ITERATIONS (per POSIX: 4) times. Cygwinonly called all destructors with non-NULL values exactly once. This patch fixes Cygwin to follow POSIX. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-07-12Cygwin: return full sigset_t from sig_sendCorinna Vinschen1-2/+2
So far sig_send's return type is int. The problem with this is that sig_send returns a sigset_t on __SIGPENDING, and sigset_t is defined as long type. So the function only returns the lower 32 bit of sigset_t, which is fine on 32 bit, but casts away the pending RT signals on 64 bit. Fix this by changing the return type of sig_send to sigset_t, so as not to narrow down the sigset when returning from handling __SIGPENDING. Make sure to cast correctly in all invocations of sig_send. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-06-24Cygwin: Implement sched_[gs]etaffinity()Mark Geisert1-0/+19
This patch set implements the Linux syscalls sched_getaffinity, sched_setaffinity, pthread_getaffinity_np, and pthread_setaffinity_np. Linux has a straightforward view of the cpu sets used in affinity masks. They are simply long (1024-bit) bit masks. This code emulates that view while internally dealing with Windows' distribution of available CPUs among processor groups.
2019-01-30Cygwin: x86_64: pthreads: Install exception handler after switching stackCorinna Vinschen1-0/+2
After creating a pthread, the stack gets moved to the desired memory location. While the 32 bit thread wrapper copies the exception handler information to the new stack (so we have at least *some* exception handler present), the x86_64 code didn't perform any exception handler magic. Fix that. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-01-18Cygwin: clock.h: add valid_timespec() to check timespec for validityCorinna Vinschen1-3/+1
Use throughout, drop local timespec_bad() in timer.cc. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-11-29Cygwin: implement extensible clock interfaceCorinna Vinschen1-8/+4
- Drop hires_[nm]s clocks, rename hires.h to clock.h. - Implement clk_t class as an extensible clock class in new file clock.cc. - Introduce get_clock(clock_id) returning a pointer to the clk_t instance for clock_id. Provide the following methods along the lines of the former hires classes: void clk_t::nsecs (struct timespec *); ULONGLONG clk_t::nsecs (); LONGLONG clk_t::usecs (); LONGLONG clk_t::msecs (); void clk_t::resolution (struct timespec *); - Add CLOCK_REALTIME_COARSE, CLOCK_MONOTONIC_RAW, CLOCK_MONOTONIC_COARSE and CLOCK_BOOTTIME clocks. - Allow clock_nanosleep, pthread_condattr_setclock and timer_create to use all new clocks (both clocks should be usable with a small tweak, though). - Bump DLL major version to 2.12. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-11-29Cygwin: pthread_cond_timedwait: make sure to wait until abstime finishesCorinna Vinschen1-8/+16
2018-07-25POSIX Asynchronous I/O support: other filesMark Geisert1-2/+2
Updates to misc files to integrate AIO into the Cygwin source tree. Much of it has to be done when adding any new syscalls. There are some updates to limits.h for AIO-specific limits. And some doc mods.
2018-06-27Cygwin: pthread_timedjoin_np: return ETIMEDOUT, not EBUSYCorinna Vinschen1-2/+2
pthread_timedjoin_np returns ETIMEDOUT if a thread is still running, not EBUSY as pthread_tryjoin_np. Also, clean up initializing timeout in pthread_tryjoin_np. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-06-27Cygwin: Implement pthread_tryjoin_np and pthread_timedjoin_npCorinna Vinschen1-2/+33
- Move pthread_join to thread.cc to have all `join' calls in the same file (pthread_timedjoin_np needs pthread_convert_abstime which is static inline in thread.cc) - Bump API version Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-07Cygwin: Cleanup time handlingCorinna Vinschen1-4/+6
* Redefine NSPERSEC to NS100PERSEC * Define NSPERSEC as nanosecs per second * Define USPERSEC as microsecs per second * Use above constants throughout where appropriate * Rename to_us to timespec_to_us and inline * Rename it_bad to timespec_bad and inline Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-01-24Define internal function mythreadname() -- revisedMark Geisert1-0/+14
This new function returns the name of the calling thread; works for both cygthreads and pthreads. All calls to cygthread::name(/*void*/) replaced by calls to mythreadname(/*void*/).
2017-12-18Cygwin: Implement sigtimedwaitMark Geisert1-1/+1
Abstract out common code from sigwait/sigwaitinfo/sigtimedwait to implement the latter.
2017-08-03cygwin: pthread timed locks: actually timeout on timeoutCorinna Vinschen1-4/+26
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2017-08-03cygwin: pthread_rwlock_rdlock: don't set errno, just return error codeCorinna Vinschen1-1/+1
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2017-08-03cygwin: Implement pthread_rwlock_timedrdlock, pthread_rwlock_timedwrlockCorinna Vinschen1-4/+66
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2017-08-03cygwin: Implement pthread_mutex_timedlockCorinna Vinschen1-2/+30
- pthread_mutex::lock now takes a PLARGE_INTEGER timeout pointer and uses that in the call to cygwait. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2017-08-03cygwin: simplify pthread timedwait handlingCorinna Vinschen1-64/+59
- Introduce inline helper pthread_convert_abstime. It converts an absolute timespec to a Windows LARGE_INTEGER timestamp, depending on the used clock. - Use this function from pthread_cond_timedwait and semaphore::timedwait - Merge semaphore::_wait and semaphore::_timedwait into single _wait method, taking a LARGER_INTEGER timestamp. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2017-03-10Drop redundant brackets in call to _reclaim_reentCorinna Vinschen1-1/+1
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2017-03-07Cygwin: pthread_cond_wait: Do as Linux and BSD do.Corinna Vinschen1-22/+5
POSIX states as follows about pthread_cond_wait: If a signal is delivered to a thread waiting for a condition variable, upon return from the signal handler the thread resumes waiting for the condition variable as if it was not interrupted, or it returns zero due to spurious wakeup. Cygwin so far employs the latter behaviour, while Linux and BSD employ the former one. Align Cygwin behaviour to Linux and BSD. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-08-23Send thread names to debuggerJon Turney1-0/+5
GDB since commit 24cdb46e [1] can report and use these names. Add utility function SetThreadName(), which sends a thread name to the debugger. Use that: - to set the default thread name for main thread and newly created pthreads. - in pthread_setname_np() for user thread names. - for helper thread names in cygthread::create() - for helper threads which are created directly with CreateThread. Note that there can still be anonymous threads, created by system or injected DLLs. [1] https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=24cdb46e9f0a694b4fbc11085e094857f08c0419
2016-08-23Add pthread_getname_np and pthread_setname_npJon Turney1-1/+64
This patch adds pthread_getname_np and pthread_setname_np. These were added to glibc in 2.12[1] and are also present in some form on NetBSD and several UNIXes. The code is based on NetBSD's implementation with changes to better match Linux behaviour. Implementation quirks: * pthread_setname_np with a NULL pointer segfaults (as linux) * pthread_setname_np returns ERANGE for names longer than 16 characters (as linux) * pthread_getname_np with a NULL pointer returns EFAULT (as linux) * pthread_getname_np with a buffer length of less than 16 returns ERANGE (as linux) * pthread_getname_np truncates the thread name to fit the buffer length. This guarantees success even when the default thread name is longer than 16 characters, but means there is no way to discover the actual length of the thread name. (Linux always truncates the thread name to 16 characters) * Changing program_invocation_short_name changes the default thread name (on linux, it has no effect on the default thread name) I'll leave it up to you to decide if any of these matter. This is implemented via class pthread_attr to make it easier to add pthread_attr_[gs]etname_np (present in NetBSD and some UNIXes) should it ever be added to Linux (or we decide we want it anyway). [1] https://sourceware.org/git/?p=glibc.git;a=blob;f=NEWS
2016-08-18Reinstantiate Cygwin function called `__getreent'Corinna Vinschen1-0/+7
This partially reverts commit 10a30e7 as far as the Cygwin version of the __getreent function is concerned. Remove _COMPILING_NEWLIB guard only allowing to use __getreent inline function when building newlib, since we wan to use it in Cygwin as well. Signed-off-by: Corinna Vinschen <corinna@vinschen.de> Signed-off-by: Stefan Assmann <sassmann@redhat.com>
2016-07-27Remove redundant macro and function called `__getreent'Corinna Vinschen1-7/+0
Just rely on the inline version in include/cygwin/config.h Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-06-23Switching the Cygwin DLL to LGPLv3+, dropping commercial buyout optioncygwin-2_5_2-releaseCorinna Vinschen1-3/+0
Bump GPLv2+ to GPLv3+ for some files, clarify BSD 2-clause. Everything else stays under GPLv3+. New Linking Exception exempts resulting executables from LGPLv3 section 4. Add CONTRIBUTORS file to keep track of licensing. Remove 'Copyright Red Hat Inc' comments. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-05-20Fix thread priority handlingCorinna Vinschen1-9/+11
So far pthread::postcreate() only sets the thread priority at all, only if the inherit-scheduler attribute is PTHREAD_EXPLICIT_SCHED. This completely ignores the PTHREAD_INHERIT_SCHED case, since in contrast to POSIX, a thread does not inherit its priority from the creating thread, but always starts with THREAD_PRIORITY_NORMAL. pthread_getschedparam() only returns what's stored in the thread attributes, not the actual thread priority. This patch fixes both problems. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-05-20Rewrite scheduler functions getting and setting process and thread priorityCorinna Vinschen1-1/+1
So far the scheduler priority handling is not POSIX compatible. The priorities use a range of -14 up to +15, which means it's not clear if the POSIX-required return value of -1 in case of an error is *really* an error or just the valid priority value -1. Even more confusing, -14 is the *max* value and 15 is the *min* value. Last but not least this range doesn't match the POSIX requirement of at least 32 priority values. This patch cleans up scheduler priority handling and moves the valid priority range to 1 (min) - 32 (max). It also adds a function sched_get_thread_priority() which will help to make thread priority more POSIX-like. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-04-20Don't test pthread objects for being already initialized at init timeCorinna Vinschen1-12/+0
For all pthread init functions, POSIX says Results are undefined if pthread_FOO_init() is called specifying an already initialized pthread_FOO object. So far our pthread init functions tested the incoming object if it's already an initialized object and, if so, returned EBUSY. That's ok *iff* the object was already initialized. However, as the example in https://cygwin.com/ml/cygwin/2016-04/msg00473.html shows, an uninitialized pthread object could also accidentally look like an initialized object and then returning EBUSY is not ok. Consequentially, all those tests are dangerous. Per POSIX, an application has to know what its doing when calling any of the pthread init functions anyway, and re-initializing the object is just as well as undefined behaviour as is returning EBUSY on already initialized objects. * thread.cc (pthread_attr_init): Drop check for already initialized object. (pthread_condattr_init): Ditto. (pthread_rwlockattr_init): Ditto. (pthread_mutexattr_init): Ditto. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-03-20Fix typoed comparisonPeter Foley1-1/+1
winsup/cygwin/ChangeLog * thread.cc (semaphore::open): Fix mistaken conditional. Signed-off-by: Peter Foley <pefoley2@pefoley.com>
2016-03-11Spinlock spin with pause instructionVáclav Haisman1-1/+19
Hi. I have noticed that Cygwin's spinlock goes into heavy sleeping code for each spin. It seems it would be a good idea to actually try to spin a bit first. There is this 'pause' instruction which let's the CPU make such busy loops be less busy. Here is a patch to do this. -- VH
2016-02-13POSIX barrier implementation, take 3Václav Haisman1-0/+262
The attached patch should address all of the review comments. Modifed change log: Newlib: * libc/include/sys/features.h (_POSIX_BARRIERS): Define for Cygwin. * libc/include/sys/types.h (pthread_barrier_t) (pthread_barrierattr_t): Do not define for Cygwin. Cygwin: * common.din (pthread_barrierattr_init) (pthread_barrierattr_setpshared, pthread_barrierattr_getpshared) (pthread_barrierattr_destroy, pthread_barrier_init) (pthread_barrier_destroy, pthread_barrier_wait): Export. * include/cygwin/types.h (pthread_barrierattr_t) (pthread_barrier_t): Declare. * include/pthread.h (PTHREAD_BARRIER_SERIAL_THREAD) (pthread_barrierattr_init, pthread_barrierattr_setpshared) (pthread_barrierattr_getpshared, pthread_barrierattr_destroy) (pthread_barrier_init, pthread_barrier_destroy) (pthread_barrier_wait): Declare. * thread.h (PTHREAD_BARRIER_MAGIC) (PTHREAD_BARRIERATTR_MAGIC): Define. (class pthread_barrierattr, class pthread_barrier): Declare. * thread.cc (delete_and_clear): New local helper function. (class pthread_barrierattr, class pthread_barrier): Implement. * miscfuncs.h (likely, unlikely): New macros. -- VH
2015-12-08Remove unnecessary locking in pthread_setcancelstate/pthread_setcanceltypeCorinna Vinschen1-26/+10
* thread.cc (pthread::setcancelstate): Remove unnecessary locking. (pthread::setcanceltype): Ditto. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>