a66c8409f51a94436193e0b72e6880c04e769103
2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file ipc.c interprocess communication and shared memory helpers */
27 /** abort if semget() failed that many times */
28 #define MAX_SEMOP_RETRIES 500
33 * \return the identifier for the new mutex on success, -E_SEM_GET
40 int ret
= semget(IPC_PRIVATE
, 1, IPC_CREAT
| 0666);
41 return ret
< 0? -E_SEM_GET
: ret
;
47 * \para, id the identifier of the mutex to be destroyed
49 * \returns Positive on success, -E_SEM_REMOVE on errors.
53 int mutex_destroy(int id
)
55 int ret
= semctl(id
, 0, IPC_RMID
);
56 return ret
< 0? -E_SEM_REMOVE
: 1;
59 static void para_semop(int id
, struct sembuf
*sops
, int num
)
63 for (i
= 0; i
< MAX_SEMOP_RETRIES
; i
++)
64 if (semop(id
, sops
, num
) >= 0)
66 PARA_EMERG_LOG("semop failed %d times, aborting\n", MAX_SEMOP_RETRIES
);
71 * lock the given mutex
73 * This function either succeeds or aborts.
75 * \sa semop(2), struct misc_meta_data
77 void mutex_lock(int id
)
79 struct sembuf sops
[2] = {
91 para_semop(id
, sops
, 2);
97 * \param id the identifier of the mutex
99 * This function either succeeds or aborts.
101 * \sa semop(2), struct misc_meta_data
103 void mutex_unlock(int id
)
105 struct sembuf sops
[1] = {
112 para_semop(id
, sops
, 1);
116 * create a new shared memory area of given size
120 int shm_new(size_t size
)
122 int ret
= shmget(IPC_PRIVATE
, size
, IPC_CREAT
| IPC_EXCL
| 0600);
123 return ret
< 0 ? -E_SHM_GET
: ret
;
127 * destroy the given shared memory area
130 int shm_destroy(int id
)
132 struct shmid_ds shm_desc
;
133 int ret
= shmctl(id
, IPC_RMID
, &shm_desc
);
134 return ret
< 0? -E_SHM_DESTROY
: ret
;
138 * attach a shared memory segment
140 * \param id the identifier of the shared memory segment to attach
141 * \param mode either ATTACH_RO (read only) or ATTACH_RW (read/write)
142 * \param result points to the attached arer which to the
144 * \returns positive on success, -E_SHM_ATTACH on errrors.
148 int shm_attach(int id
, enum shm_attach_mode mode
, void **result
)
150 if (mode
== ATTACH_RW
) {
151 *result
= shmat(id
, NULL
, 0);
152 return *result
? 1 : -E_SHM_ATTACH
;
154 *result
= shmat(id
, NULL
, SHM_RDONLY
);
155 return *result
? 1 : -E_SHM_ATTACH
;
159 * detach a shared memory segment
161 * \param addr the address of the attached segment
163 * \returns positive on success, -E_SHM_DETACH on errors.
167 int shm_detach(void *addr
)
169 int ret
= shmdt(addr
);
170 return ret
< 0? -E_SHM_DETACH
: 1;