~ubuntu-branches/ubuntu/trusty/sblim-sfcb/trusty-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * $Id: mrwlock.c,v 1.2 2005/11/20 17:43:54 bestorga-oss Exp $
 *
 * (C) Copyright IBM Corp. 2003
 *
 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
 * ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
 * CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
 *
 * You can obtain a current copy of the Eclipse Public License from
 * http://www.opensource.org/licenses/eclipse-1.0.php
 *
 * Author:       Viktor Mihajlovski <mihajlov@de.ibm.cim>
 * Contributors: 
 *
 * Description: Multiple Reader/Single Writer Locks
 */

#include "mrwlock.h"

int
MReadLock(MRWLOCK * mrwl)
{
  if (mrwl && pthread_mutex_lock(&mrwl->mrw_mutex) == 0) {
    mrwl->mrw_rnum += 1;
    return pthread_mutex_unlock(&mrwl->mrw_mutex);
  } else {
    return -1;
  }
}

int
MReadUnlock(MRWLOCK * mrwl)
{
  if (mrwl && pthread_mutex_lock(&mrwl->mrw_mutex) == 0) {
    mrwl->mrw_rnum -= 1;
    if (mrwl->mrw_rnum == 0)
      pthread_cond_broadcast(&mrwl->mrw_cond);
    return pthread_mutex_unlock(&mrwl->mrw_mutex);
  } else {
    return -1;
  }
}

int
MWriteLock(MRWLOCK * mrwl)
{
  if (mrwl && pthread_mutex_lock(&mrwl->mrw_mutex) == 0) {
    while (mrwl->mrw_rnum)
      pthread_cond_wait(&mrwl->mrw_cond, &mrwl->mrw_mutex);
    return 0;
  } else {
    return -1;
  }
}

int
MWriteUnlock(MRWLOCK * mrwl)
{
  if (mrwl && pthread_mutex_unlock(&mrwl->mrw_mutex) == 0) {
    return 0;
  } else {
    return -1;
  }
}

int
MRWInit(MRWLOCK * mrwl)
{
  static pthread_mutex_t mi = PTHREAD_MUTEX_INITIALIZER;
  static pthread_cond_t ci = PTHREAD_COND_INITIALIZER;
  mrwl->mrw_mutex = mi;
  mrwl->mrw_cond = ci;
  mrwl->mrw_rnum = 0;
  return 0;
}
/* MODELINES */
/* DO NOT EDIT BELOW THIS COMMENT */
/* Modelines are added by 'make pretty' */
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
/* vi:set ts=2 sts=2 sw=2 expandtab: */