Back in 2012 kurt@ added an abort call for the undefined behaviour cases
on pthread_mutex_unlock. This helped me a great deal in examining the
cause of some weird behaviour in icinga (or in the case of openbsd, just
plain crash).

For shits and giggles I deceided to look into pthread_spin_unlock and
saw that we return EPERM here, while POSIX states that this behaviour
is unconditionally undefined.[0] Is there a reason why we shouldn't
abort here as well?

I don't have a particular usecase for this, but the mutex case helped me
and it only seems like the right thing to do from a semetrical point of
view. The only cases of pthread_spin_unlock in base I found were in
libunbound (which with my testing with unwind didn't caused any issues)
and gnu/llvm/compiler-rt/lib/tsan, which I don't know where to test.
I have no idea which ports use it.

martijn@

[0] 
https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_spin_unlock.html

Index: librthread/rthread_spin_lock.c
===================================================================
RCS file: /cvs/src/lib/librthread/rthread_spin_lock.c,v
retrieving revision 1.5
diff -u -p -r1.5 rthread_spin_lock.c
--- librthread/rthread_spin_lock.c      6 Apr 2020 00:01:08 -0000       1.5
+++ librthread/rthread_spin_lock.c      3 Oct 2020 09:42:21 -0000
@@ -102,12 +102,12 @@ pthread_spin_unlock(pthread_spinlock_t *
        pthread_spinlock_t l;
 
        if (lock == NULL || *lock == NULL)
-               return (EINVAL);
+               abort();
 
        l = *lock;
 
        if (l->owner != self)
-               return (EPERM);
+               abort();
 
        l->owner = NULL;
        _spinunlock(&l->lock);
Index: libpthread/man/pthread_spin_unlock.3
===================================================================
RCS file: /cvs/src/lib/libpthread/man/pthread_spin_unlock.3,v
retrieving revision 1.3
diff -u -p -r1.3 pthread_spin_unlock.3
--- libpthread/man/pthread_spin_unlock.3        6 Apr 2020 00:01:08 -0000       
1.3
+++ libpthread/man/pthread_spin_unlock.3        3 Oct 2020 09:42:21 -0000
@@ -38,17 +38,14 @@ functions.
 .Sh RETURN VALUES
 If successful,
 .Fn pthread_spin_unlock
-returns zero; otherwise an error number is returned to indicate the error.
+returns zero.
 .Sh ERRORS
 .Fn pthread_spin_unlock
-will fail if:
-.Bl -tag -width Er
-.It Bq Er EINVAL
-The value specified by
+will call
+.Xr abort 3
+if the value specified by
 .Fa lock
-is invalid.
-.It Bq Er EPERM
-The lock is not owned by the calling thread.
+is invalid or if the lock is not owned by the calling thread.
 .El
 .Sh SEE ALSO
 .Xr pthread_spin_init 3 ,

Reply via email to