~ubuntu-branches/ubuntu/raring/eucalyptus/raring

« back to all changes in this revision

Viewing changes to util/ipc.c

  • Committer: Package Import Robot
  • Author(s): Brian Thomason
  • Date: 2011-11-29 13:17:52 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 185.
  • Revision ID: package-import@ubuntu.com-20111129131752-rq31al3ntutv2vvl
Tags: upstream-3.0.999beta1
Import upstream version 3.0.999beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- mode: C; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-                                                                                                     // vim: set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
 
2
 
1
3
/*
2
4
Copyright (c) 2009  Eucalyptus Systems, Inc.    
3
5
 
52
54
  SOFTWARE, AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
53
55
  IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA, SANTA
54
56
  BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY, WHICH IN
55
 
  THE REGENTS’ DISCRETION MAY INCLUDE, WITHOUT LIMITATION, REPLACEMENT
 
57
  THE REGENTS' DISCRETION MAY INCLUDE, WITHOUT LIMITATION, REPLACEMENT
56
58
  OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO IDENTIFIED, OR
57
59
  WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT NEEDED TO COMPLY WITH
58
60
  ANY SUCH LICENSES OR RIGHTS.
59
61
*/
 
62
#define _FILE_OFFSET_BITS 64 // so large-file support works on 32-bit systems
60
63
#include <stdio.h>
61
64
#include <stdlib.h>
62
65
#include <sys/types.h>
63
66
#include <semaphore.h>
64
 
#define _FILE_OFFSET_BITS 64
65
67
#include <sys/stat.h>
66
68
#include <sys/ipc.h>
67
69
#include <sys/sem.h>
68
70
#include <fcntl.h> /* For O_* */
69
71
#include <string.h>
70
72
#include <strings.h>
 
73
#include <assert.h>
71
74
 
72
75
#include "misc.h" /* logprintfl */
73
76
#include "ipc.h"
85
88
sem * sem_realloc (const int val, const char * name, int flags) 
86
89
{
87
90
    DECLARE_ARG;
88
 
 
 
91
    
 
92
    assert (name);
89
93
    sem * s = malloc (sizeof (sem));
90
 
    if (s==NULL) return NULL;
 
94
    if (s==NULL) 
 
95
        return NULL;
91
96
    bzero (s, sizeof (sem));
92
97
    s->sysv = -1;
93
98
    s->flags = flags;
94
99
    
95
100
    if (name && !strcmp(name, "mutex")) { /* use pthread mutex */
96
 
      s->usemutex = 1;
97
 
      s->mutcount = val;
98
 
      s->mutwaiters = 0;
99
 
      pthread_mutex_init(&(s->mutex), NULL);
100
 
      pthread_cond_init(&(s->cond), NULL);
 
101
        s->usemutex = 1;
 
102
        s->mutcount = val;
 
103
        s->mutwaiters = 0;
 
104
        pthread_mutex_init(&(s->mutex), NULL);
 
105
        pthread_cond_init(&(s->cond), NULL);
101
106
    } else if (name) { /* named semaphores */
102
107
        if (s->flags & O_EXCL) {
103
108
            if ( sem_unlink (name) == 0) { /* clean up in case previous sem holder crashed */
133
138
int sem_p (sem * s)
134
139
{
135
140
    int rc;
 
141
    
 
142
    if (s)
 
143
        logprintfl (EUCADEBUG2, "sem_p() %s\n", (s->name)?(s->name):(""));
 
144
    
136
145
    if (s && s->usemutex) {
137
146
        rc = pthread_mutex_lock(&(s->mutex));
138
147
        s->mutwaiters++;
160
169
int sem_v (sem * s)
161
170
{
162
171
    int rc;
 
172
 
 
173
    if (s)
 
174
        logprintfl (EUCADEBUG2, "sem_v() %s\n", (s->name)?(s->name):(""));
 
175
    
163
176
    if (s && s->usemutex) {
164
177
        rc = pthread_mutex_lock(&(s->mutex));
165
178
        if (s->mutwaiters > 0) {
166
 
          rc = pthread_cond_signal(&(s->cond));
167
 
        }
168
 
        s->mutcount++;
 
179
            rc = pthread_cond_signal(&(s->cond));
 
180
        }
 
181
        s->mutcount++;
169
182
        rc = pthread_mutex_unlock(&(s->mutex));
170
 
        return(rc);
 
183
        return(rc);
171
184
    }
172
 
 
 
185
    
173
186
    if (s && s->posix) {
174
187
        return sem_post (s->posix);
175
188
    }
198
211
        semctl (s->sysv, 0, IPC_RMID, arg); /* TODO: check return */
199
212
    }
200
213
    
201
 
    free (s);
 
214
    if (s)
 
215
        free (s);
202
216
}