~ubuntu-branches/ubuntu/oneiric/nis/oneiric-proposed

« back to all changes in this revision

Viewing changes to ypbind-mt-1.19.1/src/pthread_np.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2005-11-16 23:42:06 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20051116234206-p00omaw5ji5q0qhr
Tags: 3.15-3ubuntu1
Resynchronise with Debian.  (me)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 1998, 1999, 2000 Thorsten Kukuk, Germany
 
2
 
 
3
   This file is part of ypbind-mt.
 
4
 
 
5
   Author: Thorsten Kukuk <kukuk@suse.de>
 
6
 
 
7
   The ypbind-mt are free software; you can redistribute it and/or
 
8
   modify it under the terms of the GNU General Public License version 2
 
9
   as published by the Free Software Foundation.
 
10
 
 
11
   ypbind-mt is distributed in the hope that it will be useful,
 
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
   General Public License for more details.
 
15
 
 
16
   You should have received a copy of the GNU General Public
 
17
   License along with ypbind-mt; see the file COPYING.  If not,
 
18
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
19
   Boston, MA 02111-1307, USA. */
 
20
 
 
21
#include <pthread.h>
 
22
#include <pthread_np.h>
 
23
 
 
24
int
 
25
pthread_rdwr_init_np (pthread_rdwr_t *rdwrp)
 
26
{
 
27
  rdwrp->readers = 0;
 
28
  rdwrp->writers = 0;
 
29
  rdwrp->wishwrite = 0;
 
30
  pthread_mutex_init (&(rdwrp->mutex), NULL);
 
31
  pthread_cond_init (&(rdwrp->lock_free), NULL);
 
32
  return 0;
 
33
}
 
34
 
 
35
int
 
36
pthread_rdwr_rlock_np (pthread_rdwr_t *rdwrp)
 
37
{
 
38
  pthread_mutex_lock (&(rdwrp->mutex));
 
39
 
 
40
  while (rdwrp->writers || rdwrp->wishwrite)
 
41
    pthread_cond_wait (&(rdwrp->lock_free), &(rdwrp->mutex));
 
42
 
 
43
  rdwrp->readers++;
 
44
  pthread_mutex_unlock (&(rdwrp->mutex));
 
45
  return 0;
 
46
}
 
47
 
 
48
int
 
49
pthread_rdwr_wlock_np (pthread_rdwr_t *rdwrp)
 
50
{
 
51
  pthread_mutex_lock (&(rdwrp->mutex));
 
52
  rdwrp->wishwrite++;
 
53
  while (rdwrp->writers || rdwrp->readers)
 
54
    pthread_cond_wait (&(rdwrp->lock_free), &(rdwrp->mutex));
 
55
  rdwrp->writers++;
 
56
  rdwrp->wishwrite--;
 
57
  pthread_mutex_unlock (&(rdwrp->mutex));
 
58
  return 0;
 
59
}
 
60
 
 
61
int
 
62
pthread_rdwr_runlock_np (pthread_rdwr_t *rdwrp)
 
63
{
 
64
  int status;
 
65
 
 
66
  pthread_mutex_lock (&(rdwrp->mutex));
 
67
  if (rdwrp->readers == 0)
 
68
    {
 
69
      status = -1;
 
70
    }
 
71
  else
 
72
    {
 
73
      rdwrp->readers--;
 
74
      if (rdwrp->readers == 0)
 
75
       pthread_cond_signal (&(rdwrp->lock_free));
 
76
      status = 0;
 
77
    }
 
78
  pthread_mutex_unlock (&rdwrp->mutex);
 
79
  return status;
 
80
}
 
81
 
 
82
int
 
83
pthread_rdwr_wunlock_np (pthread_rdwr_t *rdwrp)
 
84
{
 
85
  int status;
 
86
 
 
87
  pthread_mutex_lock (&(rdwrp->mutex));
 
88
  if (rdwrp->writers == 0)
 
89
    {
 
90
      status = -1;
 
91
    }
 
92
  else
 
93
    {
 
94
      rdwrp->writers = 0;
 
95
      pthread_cond_broadcast (&(rdwrp->lock_free));
 
96
      status = 0;
 
97
    }
 
98
  pthread_mutex_unlock (&(rdwrp->mutex));
 
99
  return status;
 
100
}