Back to index

glibc  2.9
Functions
mutex.c File Reference
#include <bits/libc-lock.h>
#include <errno.h>
#include <sched.h>
#include <stddef.h>
#include <limits.h>
#include "pthread.h"
#include "internals.h"
#include "spinlock.h"
#include "queue.h"
#include "restart.h"

Go to the source code of this file.

Functions

int __pthread_mutex_init (pthread_mutex_t *mutex, const pthread_mutexattr_t *mutex_attr)
 strong_alias (__pthread_mutex_init, pthread_mutex_init)
 strong_alias (__pthread_mutex_destroy, pthread_mutex_destroy)
 strong_alias (__pthread_mutex_trylock, pthread_mutex_trylock)
 strong_alias (__pthread_mutex_lock, pthread_mutex_lock)
 strong_alias (__pthread_mutex_timedlock, pthread_mutex_timedlock)
 strong_alias (__pthread_mutex_unlock, pthread_mutex_unlock)
 strong_alias (__pthread_mutexattr_init, pthread_mutexattr_init)
 strong_alias (__pthread_mutexattr_destroy, pthread_mutexattr_destroy)
 weak_alias (strong_alias(__pthread_mutexattr_settype, pthread_mutexattr_settype)
 weak_alias (strong_alias(__pthread_mutexattr_gettype, pthread_mutexattr_gettype)
 weak_alias (__pthread_mutexattr_getpshared, pthread_mutexattr_getpshared)
 weak_alias (__pthread_mutexattr_setpshared, pthread_mutexattr_setpshared)
static void pthread_once_cancelhandler (void *arg)
int __pthread_once (pthread_once_t *once_control, void(*init_routine)(void))
 strong_alias (__pthread_once, pthread_once)
void __pthread_once_fork_parent (void)
void __pthread_once_fork_child (void)

Function Documentation

Definition at line 28 of file mutex.c.

{
  __pthread_init_lock(&mutex->__m_lock);
  mutex->__m_kind =
    mutex_attr == NULL ? PTHREAD_MUTEX_TIMED_NP : mutex_attr->__mutexkind;
  mutex->__m_count = 0;
  mutex->__m_owner = NULL;
  return 0;
}

Here is the call graph for this function:

int __pthread_once ( pthread_once_t once_control,
void(*)(void)  init_routine 
)

Definition at line 287 of file mutex.c.

{
  /* flag for doing the condition broadcast outside of mutex */
  int state_changed;

  /* Test without locking first for speed */
  if (*once_control == DONE) {
    READ_MEMORY_BARRIER();
    return 0;
  }
  /* Lock and test again */

  state_changed = 0;

  pthread_mutex_lock(&once_masterlock);

  /* If this object was left in an IN_PROGRESS state in a parent
     process (indicated by stale generation field), reset it to NEVER. */
  if ((*once_control & 3) == IN_PROGRESS && (*once_control & ~3) != fork_generation)
    *once_control = NEVER;

  /* If init_routine is being called from another routine, wait until
     it completes. */
  while ((*once_control & 3) == IN_PROGRESS) {
    pthread_cond_wait(&once_finished, &once_masterlock);
  }
  /* Here *once_control is stable and either NEVER or DONE. */
  if (*once_control == NEVER) {
    *once_control = IN_PROGRESS | fork_generation;
    pthread_mutex_unlock(&once_masterlock);
    pthread_cleanup_push(pthread_once_cancelhandler, once_control);
    init_routine();
    pthread_cleanup_pop(0);
    pthread_mutex_lock(&once_masterlock);
    WRITE_MEMORY_BARRIER();
    *once_control = DONE;
    state_changed = 1;
  }
  pthread_mutex_unlock(&once_masterlock);

  if (state_changed)
    pthread_cond_broadcast(&once_finished);

  return 0;
}

Here is the caller graph for this function:

void __pthread_once_fork_child ( void  )

Definition at line 354 of file mutex.c.

{
  pthread_mutex_init(&once_masterlock, NULL);
  pthread_cond_init(&once_finished, NULL);
  if (fork_generation <= INT_MAX - 4)
    fork_generation += 4;   /* leave least significant two bits zero */
  else
    fork_generation = 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void __pthread_once_fork_parent ( void  )

Definition at line 349 of file mutex.c.

{
  pthread_mutex_unlock(&once_masterlock);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void pthread_once_cancelhandler ( void *  arg) [static]

Definition at line 277 of file mutex.c.

{
    pthread_once_t *once_control = arg;

    pthread_mutex_lock(&once_masterlock);
    *once_control = NEVER;
    pthread_mutex_unlock(&once_masterlock);
    pthread_cond_broadcast(&once_finished);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 38 of file mutex.c.

{
  switch (mutex->__m_kind) {
  case PTHREAD_MUTEX_ADAPTIVE_NP:
  case PTHREAD_MUTEX_RECURSIVE_NP:
    if ((mutex->__m_lock.__status & 1) != 0)
      return EBUSY;
    return 0;
  case PTHREAD_MUTEX_ERRORCHECK_NP:
  case PTHREAD_MUTEX_TIMED_NP:
    if (mutex->__m_lock.__status != 0)
      return EBUSY;
    return 0;
  default:
    return EINVAL;
  }
}

Definition at line 58 of file mutex.c.

{
  pthread_descr self;
  int retcode;

  switch(mutex->__m_kind) {
  case PTHREAD_MUTEX_ADAPTIVE_NP:
    retcode = __pthread_trylock(&mutex->__m_lock);
    return retcode;
  case PTHREAD_MUTEX_RECURSIVE_NP:
    self = thread_self();
    if (mutex->__m_owner == self) {
      mutex->__m_count++;
      return 0;
    }
    retcode = __pthread_trylock(&mutex->__m_lock);
    if (retcode == 0) {
      mutex->__m_owner = self;
      mutex->__m_count = 0;
    }
    return retcode;
  case PTHREAD_MUTEX_ERRORCHECK_NP:
    retcode = __pthread_alt_trylock(&mutex->__m_lock);
    if (retcode == 0) {
      mutex->__m_owner = thread_self();
    }
    return retcode;
  case PTHREAD_MUTEX_TIMED_NP:
    retcode = __pthread_alt_trylock(&mutex->__m_lock);
    return retcode;
  default:
    return EINVAL;
  }
}

Here is the call graph for this function:

Definition at line 95 of file mutex.c.

{
  pthread_descr self;

  switch(mutex->__m_kind) {
  case PTHREAD_MUTEX_ADAPTIVE_NP:
    __pthread_lock(&mutex->__m_lock, NULL);
    return 0;
  case PTHREAD_MUTEX_RECURSIVE_NP:
    self = thread_self();
    if (mutex->__m_owner == self) {
      mutex->__m_count++;
      return 0;
    }
    __pthread_lock(&mutex->__m_lock, self);
    mutex->__m_owner = self;
    mutex->__m_count = 0;
    return 0;
  case PTHREAD_MUTEX_ERRORCHECK_NP:
    self = thread_self();
    if (mutex->__m_owner == self) return EDEADLK;
    __pthread_alt_lock(&mutex->__m_lock, self);
    mutex->__m_owner = self;
    return 0;
  case PTHREAD_MUTEX_TIMED_NP:
    __pthread_alt_lock(&mutex->__m_lock, NULL);
    return 0;
  default:
    return EINVAL;
  }
}

Here is the call graph for this function:

Definition at line 129 of file mutex.c.

{
  pthread_descr self;
  int res;

  if (__builtin_expect (abstime->tv_nsec, 0) < 0
      || __builtin_expect (abstime->tv_nsec, 0) >= 1000000000)
    return EINVAL;

  switch(mutex->__m_kind) {
  case PTHREAD_MUTEX_ADAPTIVE_NP:
    __pthread_lock(&mutex->__m_lock, NULL);
    return 0;
  case PTHREAD_MUTEX_RECURSIVE_NP:
    self = thread_self();
    if (mutex->__m_owner == self) {
      mutex->__m_count++;
      return 0;
    }
    __pthread_lock(&mutex->__m_lock, self);
    mutex->__m_owner = self;
    mutex->__m_count = 0;
    return 0;
  case PTHREAD_MUTEX_ERRORCHECK_NP:
    self = thread_self();
    if (mutex->__m_owner == self) return EDEADLK;
    res = __pthread_alt_timedlock(&mutex->__m_lock, self, abstime);
    if (res != 0)
      {
       mutex->__m_owner = self;
       return 0;
      }
    return ETIMEDOUT;
  case PTHREAD_MUTEX_TIMED_NP:
    /* Only this type supports timed out lock. */
    return (__pthread_alt_timedlock(&mutex->__m_lock, NULL, abstime)
           ? 0 : ETIMEDOUT);
  default:
    return EINVAL;
  }
}

Here is the call graph for this function:

Definition at line 174 of file mutex.c.

{
  switch (mutex->__m_kind) {
  case PTHREAD_MUTEX_ADAPTIVE_NP:
    __pthread_unlock(&mutex->__m_lock);
    return 0;
  case PTHREAD_MUTEX_RECURSIVE_NP:
    if (mutex->__m_owner != thread_self())
      return EPERM;
    if (mutex->__m_count > 0) {
      mutex->__m_count--;
      return 0;
    }
    mutex->__m_owner = NULL;
    __pthread_unlock(&mutex->__m_lock);
    return 0;
  case PTHREAD_MUTEX_ERRORCHECK_NP:
    if (mutex->__m_owner != thread_self() || mutex->__m_lock.__status == 0)
      return EPERM;
    mutex->__m_owner = NULL;
    __pthread_alt_unlock(&mutex->__m_lock);
    return 0;
  case PTHREAD_MUTEX_TIMED_NP:
    __pthread_alt_unlock(&mutex->__m_lock);
    return 0;
  default:
    return EINVAL;
  }
}

Here is the call graph for this function:

Definition at line 205 of file mutex.c.

{
  attr->__mutexkind = PTHREAD_MUTEX_TIMED_NP;
  return 0;
}

Definition at line 213 of file mutex.c.

{
  return 0;
}

Definition at line 219 of file mutex.c.

Definition at line 332 of file mutex.c.

{
  pthread_mutex_lock(&once_masterlock);
}

Here is the call graph for this function:

weak_alias ( strong_alias __pthread_mutexattr_settype,
pthread_mutexattr_settype   
)

Definition at line 231 of file mutex.c.

{
  *kind = attr->__mutexkind;
  return 0;
}
weak_alias ( strong_alias __pthread_mutexattr_gettype,
pthread_mutexattr_gettype   
)

Definition at line 240 of file mutex.c.

{
  *pshared = PTHREAD_PROCESS_PRIVATE;
  return 0;
}

Definition at line 250 of file mutex.c.

{
  if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
    return EINVAL;

  /* For now it is not possible to shared a conditional variable.  */
  if (pshared != PTHREAD_PROCESS_PRIVATE)
    return ENOSYS;

  return 0;
}

Definition at line 263 of file mutex.c.

     { NEVER = 0, IN_PROGRESS = 1, DONE = 2 };