This is the mail archive of the libc-help@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Strange error locking a pthread mutex...


Hi there,

I'm experiencing peculiar problems using a pthread mutex in my
program. Let me try to explain the setup of the program:

There is a main thread which initializes a recursive mutex for use
within (multiple) event threads running along the main thread, the
mutex is used for (what a surprise) outlining a critical section.

Here is a diagram (plaintext, ASCII-art):

+------------------+
| main() thread    |
+------------------+
| ...              |
| initialize mutex |
| ...              |
| spawn threads ---+-------------+------------------------+-------------//
| ...              |             |                        |
=                  =    +------------------+     +------------------+
+------------------+    | event thread1    |     | event thread2    |
                        +------------------+     +------------------+
                        | ...              |     | ...              |
                        | lock mutex       |     | lock mutex       |
                        | <critical stuff> |     | <critical stuff> |
                        | unlock mutex     |     | unlock mutex     |
                        | ...              |     | ...              |
                        +------------------+     +------------------+

The mutex variable is declared globally and initialised in the main thread.

---(globally)
pthread_mutex_t CritSec;
---

The initialisation of the mutex is being done as follows:

---
pthread_mutexattr_t mutexattr;

// Set the mutex as a recursive mutex
pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE_NP);

// create the mutex with the attributes set
pthread_mutex_init(&CritSec, &mutexattr);

// After initializing the mutex, the thread attribute can be destroyed
pthread_mutexattr_destroy(&mutexattr);
---

Locking and unlocking within the threads is simply done by a
'pthread_mutex_lock(&CritSec);' or a
'pthread_mutex_unlock(&CritSec);'.

This seems ok, it works most of the time, however sometimes an error
shows up from deep within the pthread library:

---
tpp.c:63: __pthread_tpp_change_priority: Assertion `new_prio == -1 ||
(new_prio >= __sched_fifo_min_prio && new_prio <=
__sched_fifo_max_prio)' failed.
---

Hmmm. Well I've done some research and the initialisation of the mutex
passes without errors, and the error occurs in the locking operation.
When all is ok, the pthread_mutex_t members are mostly zero and all
goes fine for the rest of the runtime. In case the error occurs the
'.__data.__lock' member of the pthread_mutex_t datatype has a
(seemingly) random value; mostly a very high one.

An even more strange thing is, that when I insert debug printf's in
some places (no matter what I print) the error disappears and is not
reproducable anymore. An addition to the initialization routine like:

---
int OldPrio = 0;
pthread_mutex_setprioceiling(&CritSec, 0, &OldPrio);
---

...fixes the problem, although I'm not sure whether I'm just
suppressing the symptoms or solving the problem. The error message and
the libpthread source code made me think about the priority ceiling.

Also, the problem only shows up when the program is compiled under
Suse Linux 10.2 with gcc-4.1.2 and Glibc-2.6 (more information is
attached at the bottom of this e-mail). I have Fedora Core 8 as
secondary system which does not have the problem even though it has
(almost) the same gcc version.

The problem shows up about a quarter of the times the program is being
started, if not everything works fine. The code, as described, is part
of a larger program of which this is the only part starting other
threads.

The question is: am I missing something? Any help is appreciated,
thanks in advance!

Greetings,

	Kris


PS. Some info on Glibc and GCC (SuSe Linux 10.2):
---
# /lib/libc.so.6
GNU C Library stable release version 2.5 (20061011), by Roland McGrath et al.
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Configured for i686-suse-linux.
Compiled by GNU CC version 4.1.2 20061115 (prerelease) (SUSE Linux).
Compiled on a Linux 2.6.18 system on 2007-11-21.
Available extensions:
        crypt add-on version 2.1 by Michael Glad and others
        GNU Libidn by Simon Josefsson
        GNU libio by Per Bothner
        NIS(YP)/NIS+ NSS modules 0.19 by Thorsten Kukuk
        NoVersion patch for broken glibc 2.0 binaries
        Native POSIX Threads Library by Ulrich Drepper et al
        BIND-8.2.3-T5B
Thread-local storage support included.
For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.
---

---
# gcc --version
gcc (GCC) 4.1.2 20061115 (prerelease) (SUSE Linux)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
---

PPS. Some info on Glibc and GCC (Fedora Core 8):
---
# /lib/libc.so.6
GNU C Library stable release version 2.7, by Roland McGrath et al.
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 4.1.2 20070925 (Red Hat 4.1.2-32).
Compiled on a Linux >>2.6.20-1.3001.fc6xen<< system on 2007-10-18.
Available extensions:
        The C stubs add-on version 2.1.2.
        crypt add-on version 2.1 by Michael Glad and others
        GNU Libidn by Simon Josefsson
        Native POSIX Threads Library by Ulrich Drepper et al
        BIND-8.2.3-T5B
        RT using linux kernel aio
For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.
---

---
# gcc --version
gcc (GCC) 4.1.2 20070925 (Red Hat 4.1.2-33)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
---


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]