annotate lib/glthread/threadlib.c @ 14079:97fc9a21a8fb

maint: update almost all copyright ranges to include 2011 Run the new "make update-copyright" rule.
author Jim Meyering <meyering@redhat.com>
date Sat, 01 Jan 2011 20:17:23 +0100
parents c2cbabec01dd
children 8250f2777afc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10322
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Multithreading primitives.
14079
97fc9a21a8fb maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents: 12559
diff changeset
2 Copyright (C) 2005-2011 Free Software Foundation, Inc.
10322
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software; you can redistribute it and/or modify
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 2, or (at your option)
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 any later version.
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 along with this program; if not, write to the Free Software Foundation,
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 /* Written by Bruno Haible <bruno@clisp.org>, 2005. */
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #include <config.h>
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 /* ========================================================================= */
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #if USE_POSIX_THREADS
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 /* Use the POSIX threads library. */
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27
10337
d2a9509e45db Include <pthread.h>.
Bruno Haible <bruno@clisp.org>
parents: 10322
diff changeset
28 # include <pthread.h>
11089
43ca345955b3 Avoid a compilation warning.
Bruno Haible <bruno@clisp.org>
parents: 11088
diff changeset
29 # include <stdlib.h>
10337
d2a9509e45db Include <pthread.h>.
Bruno Haible <bruno@clisp.org>
parents: 10322
diff changeset
30
10322
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 # if PTHREAD_IN_USE_DETECTION_HARD
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 /* The function to be executed by a dummy thread. */
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 static void *
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 dummy_thread_func (void *arg)
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 {
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 return arg;
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 }
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 int
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 glthread_in_use (void)
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 {
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 static int tested;
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 static int result; /* 1: linked with -lpthread, 0: only with libc */
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 if (!tested)
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 {
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 pthread_t thread;
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 if (pthread_create (&thread, NULL, dummy_thread_func, NULL) != 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11089
diff changeset
51 /* Thread creation failed. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11089
diff changeset
52 result = 0;
10322
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11089
diff changeset
54 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11089
diff changeset
55 /* Thread creation works. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11089
diff changeset
56 void *retval;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11089
diff changeset
57 if (pthread_join (thread, &retval) != 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11089
diff changeset
58 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11089
diff changeset
59 result = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11089
diff changeset
60 }
10322
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 tested = 1;
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 }
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 return result;
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 }
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 # endif
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 #endif
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69
794059e94b36 Move some code from module 'lock' to module 'threadlib'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 /* ========================================================================= */
11088
c8d99e7b214a Avoid warning from OSF/1 C compiler.
Bruno Haible <bruno@clisp.org>
parents: 10337
diff changeset
71
c8d99e7b214a Avoid warning from OSF/1 C compiler.
Bruno Haible <bruno@clisp.org>
parents: 10337
diff changeset
72 /* This declaration is solely to ensure that after preprocessing
c8d99e7b214a Avoid warning from OSF/1 C compiler.
Bruno Haible <bruno@clisp.org>
parents: 10337
diff changeset
73 this file is never empty. */
c8d99e7b214a Avoid warning from OSF/1 C compiler.
Bruno Haible <bruno@clisp.org>
parents: 10337
diff changeset
74 typedef int dummy;