Bug 1241075 - pthread_mutex_init stub is only provided by glibc for non-recursive mutexes
Summary: pthread_mutex_init stub is only provided by glibc for non-recursive mutexes
Keywords:
Status: CLOSED WORKSFORME
Alias: None
Product: Fedora
Classification: Fedora
Component: glibc
Version: 22
Hardware: Unspecified
OS: Unspecified
unspecified
unspecified
Target Milestone: ---
Assignee: Carlos O'Donell
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2015-07-08 11:52 UTC by Nikos Mavrogiannopoulos
Modified: 2016-01-05 10:43 UTC (History)
7 users (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2016-01-05 10:43:19 UTC
Type: Bug
Embargoed:


Attachments (Terms of Use)

Description Nikos Mavrogiannopoulos 2015-07-08 11:52:22 UTC
Description of problem:
glibc provides stubs for pthread_mutex_init(), lock and unlock, so that library that have no information on whether their caller uses threads, can provide locking when needed.

That means, that the following program will compile and run without linking to libpthread.

#include <pthread.h>

int main()
{
pthread_mutex_t mutex;

        pthread_mutex_init(&mutex, NULL);

        return 0;
}

However, with this approach a library cannot use a recursive mutex, because the mutex needs to be provided attributes which can only be initialized with pthread_mutexattr_init() which isn't available in glibc.

That is, the following program cannot compile without libpthread. Note that this program is identical to above, but uses a recursive mutex.

#include <pthread.h>

int main()
{
pthread_mutex_t mutex;
pthread_mutexattr_t attr;

        pthread_mutexattr_init(&attr);
        pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE_NP);     
        pthread_mutex_init(&mutex, &attr);

        return 0;
}


How reproducible:
Compile the second program provided.

Actual results:
$ gcc c.c
/tmp/ccsjztYf.o: In function `main':
c.c:(.text+0x10): undefined reference to `pthread_mutexattr_init'
c.c:(.text+0x21): undefined reference to `pthread_mutexattr_settype'


Expected results:
Should compile as expected.

Comment 1 Florian Weimer 2015-12-30 16:00:01 UTC
Have you tried using PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, avoiding the attribute?  It's not portable (hence the _NP), but it should work in this scenario.

Comment 2 Nikos Mavrogiannopoulos 2016-01-01 09:57:09 UTC
(In reply to Florian Weimer from comment #1)
> Have you tried using PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, avoiding the
> attribute?  It's not portable (hence the _NP), but it should work in this
> scenario.

I have not tried the static initializers, but they were not an option for the project that the above code comes from.

Comment 3 Florian Weimer 2016-01-01 10:20:51 UTC
(In reply to Nikos Mavrogiannopoulos from comment #2)
> (In reply to Florian Weimer from comment #1)
> > Have you tried using PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, avoiding the
> > attribute?  It's not portable (hence the _NP), but it should work in this
> > scenario.
> 
> I have not tried the static initializers, but they were not an option for
> the project that the above code comes from.

Could you elaborate why?  I would like to understand how we can improve this on the glibc side.

Comment 4 Nikos Mavrogiannopoulos 2016-01-05 10:35:16 UTC
(In reply to Florian Weimer from comment #3)
> (In reply to Nikos Mavrogiannopoulos from comment #2)
> > (In reply to Florian Weimer from comment #1)
> > > Have you tried using PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, avoiding the
> > > attribute?  It's not portable (hence the _NP), but it should work in this
> > > scenario.
> > 
> > I have not tried the static initializers, but they were not an option for
> > the project that the above code comes from.
> Could you elaborate why?  I would like to understand how we can improve this
> on the glibc side.

Actually I was wrong on that. The mutex is created dynamically (in the context of a "context" structure), but indeed the attributes could have been initialized statically. So that addresses the issue; sorry for the false alarm.

Comment 5 Florian Weimer 2016-01-05 10:43:19 UTC
(In reply to Nikos Mavrogiannopoulos from comment #4)

> Actually I was wrong on that. The mutex is created dynamically (in the
> context of a "context" structure), but indeed the attributes could have been
> initialized statically. So that addresses the issue; sorry for the false
> alarm.

Thanks for the clarification.  As the current glibc features suffice for your purposes, I'm closing this bug.


Note You need to log in before you can comment on or make changes to this bug.