Re: pthread_spin_lock family implementation

2012-04-23 Thread Paul Irofti
On Mon, Apr 23, 2012 at 10:40:27AM +0200, David Coppa wrote:
> On Fri, Apr 20, 2012 at 9:47 PM, Brad Smith  wrote:
> > On 13/04/12 11:37 AM, Paul Irofti wrote:
> >>
> >> On Fri, Apr 13, 2012 at 05:34:09PM +0300, Paul Irofti wrote:
> >>>
> >>> Okay? (Yes, I'll do the manpage bits as well).
> >>
> >>
> >> Now with documentation and style fixes and hppa support.
> >
> >
> > Tested with VLC which will use spin locks if available instead of falling
> > back to mutexes and no noticeable regressions.
> 
> Any chance for this to go in?

Sure, once I get some okays for it.



Re: pthread_spin_lock family implementation

2012-04-23 Thread David Coppa
On Fri, Apr 20, 2012 at 9:47 PM, Brad Smith  wrote:
> On 13/04/12 11:37 AM, Paul Irofti wrote:
>>
>> On Fri, Apr 13, 2012 at 05:34:09PM +0300, Paul Irofti wrote:
>>>
>>> Okay? (Yes, I'll do the manpage bits as well).
>>
>>
>> Now with documentation and style fixes and hppa support.
>
>
> Tested with VLC which will use spin locks if available instead of falling
> back to mutexes and no noticeable regressions.

Any chance for this to go in?

cheers,
david



Re: pthread_spin_lock family implementation

2012-04-20 Thread Brad Smith

On 13/04/12 11:37 AM, Paul Irofti wrote:

On Fri, Apr 13, 2012 at 05:34:09PM +0300, Paul Irofti wrote:

Okay? (Yes, I'll do the manpage bits as well).


Now with documentation and style fixes and hppa support.


Tested with VLC which will use spin locks if available instead of falling
back to mutexes and no noticeable regressions.

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.



Re: pthread_spin_lock family implementation

2012-04-13 Thread Paul Irofti
On Fri, Apr 13, 2012 at 05:34:09PM +0300, Paul Irofti wrote:
> Okay? (Yes, I'll do the manpage bits as well).

Now with documentation and style fixes and hppa support.


Index: lib/libpthread/include/pthread.h
===
RCS file: /cvs/src/lib/libpthread/include/pthread.h,v
retrieving revision 1.33
diff -u -p -r1.33 pthread.h
--- lib/libpthread/include/pthread.h11 Apr 2012 10:18:46 -  1.33
+++ lib/libpthread/include/pthread.h13 Apr 2012 15:36:09 -
@@ -141,6 +141,7 @@ typedef struct  pthread_rwlock  *pthread_
 typedef struct pthread_rwlockattr  *pthread_rwlockattr_t;
 typedef struct pthread_barrier *pthread_barrier_t;
 typedef struct pthread_barrierattr *pthread_barrierattr_t;
+typedef struct pthread_spinlock*pthread_spinlock_t;
 
 /*
  * Additional type definitions:
@@ -322,6 +323,11 @@ intpthread_barrierattr_init(pthread_ba
 intpthread_barrierattr_destroy(pthread_barrierattr_t *);
 intpthread_barrierattr_getpshared(pthread_barrierattr_t *, int *);
 intpthread_barrierattr_setpshared(pthread_barrierattr_t *, int);
+intpthread_spin_init(pthread_spinlock_t *, int);
+intpthread_spin_destroy(pthread_spinlock_t *);
+intpthread_spin_trylock(pthread_spinlock_t *);
+intpthread_spin_lock(pthread_spinlock_t *);
+intpthread_spin_unlock(pthread_spinlock_t *);
 __END_DECLS
 
 #endif /* _PTHREAD_H_ */
Index: lib/libpthread/man/Makefile.inc
===
RCS file: /cvs/src/lib/libpthread/man/Makefile.inc,v
retrieving revision 1.32
diff -u -p -r1.32 Makefile.inc
--- lib/libpthread/man/Makefile.inc 11 Apr 2012 10:18:46 -  1.32
+++ lib/libpthread/man/Makefile.inc 13 Apr 2012 15:36:09 -
@@ -58,6 +58,9 @@ MAN+= \
pthread_set_name_np.3 \
pthread_setspecific.3 \
pthread_sigmask.3 \
+   pthread_spin_init.3 \
+   pthread_spin_lock.3 \
+   pthread_spin_unlock.3 \
pthread_stackseg_np.3 \
pthread_testcancel.3 \
pthread_yield.3 \
@@ -107,6 +110,8 @@ MLINKS+=flockfile.3 funlockfile.3 \
pthread_mutexattr.3 pthread_mutexattr_settype.3 \
pthread_schedparam.3 pthread_getschedparam.3 \
pthread_schedparam.3 pthread_setschedparam.3 \
+   pthread_spin_init.3 pthread_spin_destroy.3 \
+   pthread_spin_lock.3 pthread_spin_trylock.3 \
pthread_testcancel.3 pthread_setcancelstate.3 \
pthread_testcancel.3 pthread_setcanceltype.3 \
pthread_getconcurrency.3 pthread_setconcurrency.3 \
Index: lib/libpthread/man/pthread_spin_init.3
===
RCS file: lib/libpthread/man/pthread_spin_init.3
diff -N lib/libpthread/man/pthread_spin_init.3
--- /dev/null   1 Jan 1970 00:00:00 -
+++ lib/libpthread/man/pthread_spin_init.3  13 Apr 2012 15:36:09 -
@@ -0,0 +1,89 @@
+.\"$OpenBSD$
+.\"
+.\" Copyright (c) 2012 Paul Irofti 
+.\"
+.\" Permission to use, copy, modify, and distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.\"
+.Dd $Mdocdate: April 13 2012 $
+.Dt PTHREAD_SPIN_INIT 3
+.Os
+.Sh NAME
+.Nm pthread_spin_init ,
+.Nm pthread_spin_destroy
+.Nd initialize and destroy a spinlock object
+.Sh SYNOPSIS
+.Fd #include 
+.Ft int
+.Fn pthread_spin_init "pthread_spinlock_t *lock" "int pshared"
+.Ft int
+.Fn pthread_spin_destroy "pthread_spinlock_t *lock"
+.Sh DESCRIPTION
+The
+.Fn pthread_spin_init
+function creates a new spinlock object, with sharing attributes specified by
+.Fa pshared
+.Pp
+The
+.Fn pthread_spin_destroy
+function frees the resources allocated for the
+.Fa lock .
+.Sh RETURN VALUES
+If successful,
+.Fn pthread_spin_init
+and
+.Fn pthread_spin_destroy
+will return zero, otherwise an error number will be returned to
+indicate the error.
+.Sh ERRORS
+.Fn pthread_spin_init
+will fail if:
+.Bl -tag -width Er
+.It Bq Er EINVAL
+The value specified by
+.Fa lock
+is invalid.
+.It Bq Er ENOMEM
+The process cannot allocate enough memory to create another spinlock object.
+.It Bq Er ENOTSUP
+The shared attributes specified by
+.Fa pshared
+are not supported by the current implementation.
+.El
+.Pp
+.Fn pthread_spin_destroy
+will fail if:

pthread_spin_lock family implementation

2012-04-13 Thread Paul Irofti
Okay? (Yes, I'll do the manpage bits as well).


Index: lib/libpthread/include/pthread.h
===
RCS file: /cvs/src/lib/libpthread/include/pthread.h,v
retrieving revision 1.33
diff -u -p -r1.33 pthread.h
--- lib/libpthread/include/pthread.h11 Apr 2012 10:18:46 -  1.33
+++ lib/libpthread/include/pthread.h13 Apr 2012 14:31:59 -
@@ -141,6 +141,7 @@ typedef struct  pthread_rwlock  *pthread_
 typedef struct pthread_rwlockattr  *pthread_rwlockattr_t;
 typedef struct pthread_barrier *pthread_barrier_t;
 typedef struct pthread_barrierattr *pthread_barrierattr_t;
+typedef struct pthread_spinlock*pthread_spinlock_t;
 
 /*
  * Additional type definitions:
@@ -322,6 +323,11 @@ intpthread_barrierattr_init(pthread_ba
 intpthread_barrierattr_destroy(pthread_barrierattr_t *);
 intpthread_barrierattr_getpshared(pthread_barrierattr_t *, int *);
 intpthread_barrierattr_setpshared(pthread_barrierattr_t *, int);
+intpthread_spin_init(pthread_spinlock_t *, int);
+intpthread_spin_destroy(pthread_spinlock_t *);
+intpthread_spin_trylock(pthread_spinlock_t *);
+intpthread_spin_lock(pthread_spinlock_t *);
+intpthread_spin_unlock(pthread_spinlock_t *);
 __END_DECLS
 
 #endif /* _PTHREAD_H_ */
Index: lib/librthread/Makefile
===
RCS file: /cvs/src/lib/librthread/Makefile,v
retrieving revision 1.29
diff -u -p -r1.29 Makefile
--- lib/librthread/Makefile 11 Apr 2012 10:18:46 -  1.29
+++ lib/librthread/Makefile 13 Apr 2012 14:31:59 -
@@ -38,6 +38,7 @@ SRCS= rthread.c \
rthread_sem.c \
rthread_sig.c \
rthread_stack.c \
+   rthread_spin_lock.c \
rthread_sync.c \
rthread_tls.c \
sched_prio.c
Index: lib/librthread/rthread.h
===
RCS file: /cvs/src/lib/librthread/rthread.h,v
retrieving revision 1.38
diff -u -p -r1.38 rthread.h
--- lib/librthread/rthread.h11 Apr 2012 10:18:46 -  1.38
+++ lib/librthread/rthread.h13 Apr 2012 14:32:00 -
@@ -134,6 +134,11 @@ struct pthread_barrierattr {
int pshared;
 };
 
+struct pthread_spinlock {
+   _spinlock_lock_t lock;
+   pthread_t owner;
+};
+
 struct pthread {
struct sem donesem;
 #if TLS_VARIANT == 1
Index: lib/librthread/rthread_spin_lock.c
===
RCS file: lib/librthread/rthread_spin_lock.c
diff -N lib/librthread/rthread_spin_lock.c
--- /dev/null   1 Jan 1970 00:00:00 -
+++ lib/librthread/rthread_spin_lock.c  13 Apr 2012 14:32:00 -
@@ -0,0 +1,114 @@
+/* $OpenBSD$   */
+/*
+ * Copyright (c) 2012 Paul Irofti 
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include 
+#include 
+
+#include 
+
+#include "rthread.h"
+
+int
+pthread_spin_init(pthread_spinlock_t *lock, int pshared)
+{
+   pthread_spinlock_t l = NULL;
+
+   if (lock == NULL) {
+   return EINVAL;
+   }
+   if (pshared != PTHREAD_PROCESS_PRIVATE) {
+   return ENOTSUP;
+   }
+   
+   l = calloc(1, sizeof *l);
+   if (l == NULL) {
+   return ENOMEM;
+   }
+   *lock = l;
+   return 0;
+}
+
+int
+pthread_spin_destroy(pthread_spinlock_t *lock)
+{
+   if (lock == NULL || *lock == NULL) {
+   return EINVAL;
+   }
+   if ((*lock)->owner != NULL) {
+   return EBUSY;
+   }
+
+   free(*lock);
+   *lock = NULL;
+   return 0;
+}
+
+int
+pthread_spin_trylock(pthread_spinlock_t *lock)
+{
+   pthread_t self = pthread_self();
+   pthread_spinlock_t l;
+
+   if (lock == NULL || *lock == NULL) {
+   return EINVAL;
+   }
+   l = *lock;
+   if (l->owner == self) {
+   return EDEADLK;
+   }
+   if (_atomic_lock(&l->lock)) {
+   return EBUSY;
+   }
+   l->owner = self;
+   return 0;
+}
+
+int
+pthread_spin_lock(pthread_spinlock_t *lock)
+{
+   pthread_t self = pthread_self();
+   pthread_spinlock_t l;
+
+   if (lock == NUL