summaryrefslogtreecommitdiff
path: root/libpthread/sysdeps/hurd
diff options
context:
space:
mode:
authormarcus <marcus>2004-03-18 02:44:20 +0000
committermarcus <marcus>2004-03-18 02:44:20 +0000
commit82dcb58429439e48e973c6b9772f60d27db15d89 (patch)
treef10beac43fb87c25dc71af4e654da03c71ad8e28 /libpthread/sysdeps/hurd
parentfc975a0a67947f11ef0ac09278ae16b6c68d1bf3 (diff)
2004-03-17 Marcus Brinkmann <marcus@gnu.org>
* libpthread: New directory, populated with Neal H. Walfields pthread implementation.
Diffstat (limited to 'libpthread/sysdeps/hurd')
-rw-r--r--libpthread/sysdeps/hurd/pt-destroy-specific.c80
-rw-r--r--libpthread/sysdeps/hurd/pt-getspecific.c37
-rw-r--r--libpthread/sysdeps/hurd/pt-init-specific.c30
-rw-r--r--libpthread/sysdeps/hurd/pt-key-create.c109
-rw-r--r--libpthread/sysdeps/hurd/pt-key-delete.c45
-rw-r--r--libpthread/sysdeps/hurd/pt-key.h76
-rw-r--r--libpthread/sysdeps/hurd/pt-kill.c49
-rw-r--r--libpthread/sysdeps/hurd/pt-setspecific.c43
8 files changed, 469 insertions, 0 deletions
diff --git a/libpthread/sysdeps/hurd/pt-destroy-specific.c b/libpthread/sysdeps/hurd/pt-destroy-specific.c
new file mode 100644
index 0000000..2006f2b
--- /dev/null
+++ b/libpthread/sysdeps/hurd/pt-destroy-specific.c
@@ -0,0 +1,80 @@
+/* __pthread_destory_specific. Hurd version.
+ Copyright (C) 2002 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <pthread.h>
+#include <stdlib.h>
+#include <hurd/ihash.h>
+
+#include <pt-internal.h>
+
+void
+__pthread_destroy_specific (struct __pthread *thread)
+{
+ error_t err;
+ int i;
+ int seen_one;
+
+ /* Check if there is any thread specific data. */
+ if (! thread->thread_specifics)
+ return;
+
+ __pthread_key_lock_ready ();
+
+ /* Iterate and call the destructors on any thread specific data. */
+ for (;;)
+ {
+ seen_one = 0;
+
+ __pthread_mutex_lock (&__pthread_key_lock);
+
+ for (i = 0; i < __pthread_key_count; i ++)
+ {
+ void *value;
+
+ if (__pthread_key_destructors[i] == PTHREAD_KEY_INVALID)
+ break;
+
+ value = hurd_ihash_find (thread->thread_specifics, i);
+ if (value)
+ {
+ err = hurd_ihash_remove (thread->thread_specifics, i);
+ assert (err == 1);
+
+ if (__pthread_key_destructors[i])
+ {
+ seen_one = 1;
+ __pthread_key_destructors[i] (value);
+ }
+ }
+ }
+
+ __pthread_mutex_unlock (&__pthread_key_lock);
+
+ if (! seen_one)
+ break;
+
+ /* This may take a very long time. Let those blocking on
+ pthread_key_create or pthread_key_delete make progress. */
+ /* FIXME what should we do with this one? */
+ /* sched_yield (); */
+ }
+
+ hurd_ihash_free (thread->thread_specifics);
+ thread->thread_specifics = 0;
+}
diff --git a/libpthread/sysdeps/hurd/pt-getspecific.c b/libpthread/sysdeps/hurd/pt-getspecific.c
new file mode 100644
index 0000000..3060598
--- /dev/null
+++ b/libpthread/sysdeps/hurd/pt-getspecific.c
@@ -0,0 +1,37 @@
+/* pthread_getspecific. Hurd version.
+ Copyright (C) 2002 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <pthread.h>
+#include <hurd/ihash.h>
+
+#include <pt-internal.h>
+
+void *
+pthread_getspecific (pthread_key_t key)
+{
+ struct __pthread *self;
+
+ assert (key < __pthread_key_count);
+
+ self = _pthread_self ();
+ if (! self->thread_specifics)
+ return 0;
+
+ return hurd_ihash_find (self->thread_specifics, key);
+}
diff --git a/libpthread/sysdeps/hurd/pt-init-specific.c b/libpthread/sysdeps/hurd/pt-init-specific.c
new file mode 100644
index 0000000..c1bacbc
--- /dev/null
+++ b/libpthread/sysdeps/hurd/pt-init-specific.c
@@ -0,0 +1,30 @@
+/* __pthread_init_specific. Hurd version.
+ Copyright (C) 2002 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <pthread.h>
+#include <stdlib.h>
+
+#include <pt-internal.h>
+
+error_t
+__pthread_init_specific (struct __pthread *thread)
+{
+ thread->thread_specifics = 0;
+ return 0;
+}
diff --git a/libpthread/sysdeps/hurd/pt-key-create.c b/libpthread/sysdeps/hurd/pt-key-create.c
new file mode 100644
index 0000000..b3e0141
--- /dev/null
+++ b/libpthread/sysdeps/hurd/pt-key-create.c
@@ -0,0 +1,109 @@
+/* pthread_key_create. Hurd version.
+ Copyright (C) 2002 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <pthread.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#include <pt-internal.h>
+
+pthread_mutex_t __pthread_key_lock;
+
+void (**__pthread_key_destructors) (void *arg);
+int __pthread_key_size;
+int __pthread_key_count;
+int __pthread_key_invalid_count;
+
+int
+pthread_key_create (pthread_key_t *key, void (*destructor) (void *))
+{
+ /* Where to look for the next key slot. */
+ static int index;
+
+ __pthread_key_lock_ready ();
+
+ __pthread_mutex_lock (&__pthread_key_lock);
+
+ do_search:
+ /* Use the search hint and try to find a free slot. */
+ for (; index < __pthread_key_count
+ && __pthread_key_destructors[index] != PTHREAD_KEY_INVALID;
+ index ++)
+ ;
+
+ /* See if we actually found a free element. */
+ if (index < __pthread_key_count)
+ {
+ assert (__pthread_key_destructors[index] == PTHREAD_KEY_INVALID);
+ assert (__pthread_key_invalid_count > 0);
+
+ __pthread_key_invalid_count --;
+ __pthread_key_destructors[index] = destructor;
+ *key = index ++;
+
+ __pthread_mutex_unlock (&__pthread_key_lock);
+ return 0;
+ }
+
+ assert (index == __pthread_key_count);
+
+ /* No space at the end. */
+ if (__pthread_key_size == __pthread_key_count)
+ {
+ /* See if it is worth looking for a free element. */
+ if (__pthread_key_invalid_count > 4
+ && __pthread_key_invalid_count > __pthread_key_size / 8)
+ {
+ index = 0;
+ goto do_search;
+ }
+
+
+ /* Resize the array. */
+ {
+ void *t;
+ int newsize;
+
+ if (__pthread_key_size == 0)
+ newsize = 8;
+ else
+ newsize = __pthread_key_size * 2;
+
+ t = realloc (__pthread_key_destructors,
+ newsize * sizeof (*__pthread_key_destructors));
+ if (! t)
+ {
+ __pthread_mutex_unlock (&__pthread_key_lock);
+ return ENOMEM;
+ }
+
+ __pthread_key_size = newsize;
+ __pthread_key_destructors = t;
+ }
+ }
+
+ __pthread_key_destructors[index] = destructor;
+ *key = index;
+
+ index ++;
+ __pthread_key_count ++;
+
+ __pthread_mutex_unlock (&__pthread_key_lock);
+ return 0;
+}
diff --git a/libpthread/sysdeps/hurd/pt-key-delete.c b/libpthread/sysdeps/hurd/pt-key-delete.c
new file mode 100644
index 0000000..2426bb1
--- /dev/null
+++ b/libpthread/sysdeps/hurd/pt-key-delete.c
@@ -0,0 +1,45 @@
+/* pthread_key_delete. Hurd version.
+ Copyright (C) 2002 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <pthread.h>
+
+#include <pt-internal.h>
+
+int
+pthread_key_delete (pthread_key_t key)
+{
+ error_t err = 0;
+
+ __pthread_key_lock_ready ();
+
+ __pthread_mutex_lock (&__pthread_key_lock);
+
+ if (key < 0 || key >= __pthread_key_count
+ || __pthread_key_destructors[key] == PTHREAD_KEY_INVALID)
+ err = EINVAL;
+ else
+ {
+ __pthread_key_destructors[key] = PTHREAD_KEY_INVALID;
+ __pthread_key_invalid_count ++;
+ }
+
+ __pthread_mutex_unlock (&__pthread_key_lock);
+
+ return err;
+}
diff --git a/libpthread/sysdeps/hurd/pt-key.h b/libpthread/sysdeps/hurd/pt-key.h
new file mode 100644
index 0000000..494e01d
--- /dev/null
+++ b/libpthread/sysdeps/hurd/pt-key.h
@@ -0,0 +1,76 @@
+/* pthread_key internal declatations for the Hurd version.
+ Copyright (C) 2002 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <pthread.h>
+#include <hurd/ihash.h>
+
+#define PTHREAD_KEY_MEMBERS \
+ hurd_ihash_t thread_specifics;
+
+#define PTHREAD_KEY_INVALID (void *) (-1)
+
+
+/* __PTHREAD_KEY_DESTRUCTORS is an array of destructors with
+ __PTHREAD_KEY_SIZE elements. If an element with index less than
+ __PTHREAD_KEY_COUNT is invalid, it shall contain the value
+ PTHREAD_KEY_INVALID which shall be distinct from NULL.
+
+ Normally, we just add new keys to the end of the array and realloc
+ it as necessary. The pthread_key_create routine may decide to
+ rescan the array if __PTHREAD_KEY_FREE is large. */
+extern void (**__pthread_key_destructors) (void *arg);
+extern int __pthread_key_size;
+extern int __pthread_key_count;
+/* Number of invalid elements in the array. Does not include elements
+ for which memory has been allocated but which have not yet been
+ used (i.e. those elements with indexes greater than
+ __PTHREAD_KEY_COUNT). */
+extern int __pthread_key_invalid_count;
+
+/* Protects the above variables. This must be a recursive lock: the
+ destructors may call pthread_key_delete. */
+extern pthread_mutex_t __pthread_key_lock;
+
+#include <assert.h>
+
+static inline void
+__pthread_key_lock_ready (void)
+{
+ static pthread_once_t o = PTHREAD_ONCE_INIT;
+
+ void do_init (void)
+ {
+ int err;
+ pthread_mutexattr_t attr;
+
+ err = pthread_mutexattr_init (&attr);
+ assert_perror (err);
+
+ err = pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
+ assert_perror (err);
+
+ err = pthread_mutex_init (&__pthread_key_lock, &attr);
+ assert_perror (err);
+
+ err = pthread_mutexattr_destroy (&attr);
+ assert_perror (err);
+ }
+
+ pthread_once (&o, do_init);
+}
diff --git a/libpthread/sysdeps/hurd/pt-kill.c b/libpthread/sysdeps/hurd/pt-kill.c
new file mode 100644
index 0000000..f970e06
--- /dev/null
+++ b/libpthread/sysdeps/hurd/pt-kill.c
@@ -0,0 +1,49 @@
+/* pthread_kill. Hurd version.
+ Copyright (C) 2002 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <pthread.h>
+#include <assert.h>
+#include <signal.h>
+#include <hurd/signal.h>
+
+#include <pt-internal.h>
+
+int
+pthread_kill (pthread_t thread, int sig)
+{
+ struct __pthread *pthread;
+ struct hurd_signal_detail detail;
+ struct hurd_sigstate *ss;
+
+ /* Lookup the thread structure for THREAD. */
+ pthread = __pthread_getid (thread);
+ if (pthread == NULL)
+ return ESRCH;
+
+ ss = _hurd_thread_sigstate (pthread->kernel_thread);
+ assert (ss);
+
+ detail.exc = 0;
+ detail.code = sig;
+ detail.error = 0;
+
+ _hurd_raise_signal (ss, sig, &detail);
+
+ return 0;
+}
diff --git a/libpthread/sysdeps/hurd/pt-setspecific.c b/libpthread/sysdeps/hurd/pt-setspecific.c
new file mode 100644
index 0000000..bd75225
--- /dev/null
+++ b/libpthread/sysdeps/hurd/pt-setspecific.c
@@ -0,0 +1,43 @@
+/* pthread_setspecific. Generic version.
+ Copyright (C) 2002 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <pthread.h>
+#include <hurd/ihash.h>
+
+#include <pt-internal.h>
+
+int
+pthread_setspecific (pthread_key_t key, const void *value)
+{
+ error_t err;
+ struct __pthread *self = _pthread_self ();
+
+ if (! self->thread_specifics)
+ {
+ err = ihash_create (&self->thread_specifics);
+ if (err)
+ return ENOMEM;
+ }
+
+ err = ihash_add (self->thread_specifics, key, (void *) value, 0);
+ if (err)
+ return ENOMEM;
+
+ return 0;
+}