2 * Copyright (C) 2006-2007 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 */
28 /** abort if semget() failed that many times */
29 #define MAX_SEMOP_RETRIES 500
34 * \return the identifier for the new mutex on success, \a -E_SEM_GET
41 int ret
= semget(IPC_PRIVATE
, 1, IPC_CREAT
| 0666);
42 return ret
< 0? -E_SEM_GET
: ret
;
48 * \param id the identifier of the mutex to be destroyed
50 * \return Positive on success, \a -E_SEM_REMOVE on errors.
54 int mutex_destroy(int id
)
56 int ret
= semctl(id
, 0, IPC_RMID
);
57 return ret
< 0? -E_SEM_REMOVE
: 1;
60 static void para_semop(int id
, struct sembuf
*sops
, unsigned num
)
64 for (i
= 0; i
< MAX_SEMOP_RETRIES
; i
++)
65 if (semop(id
, sops
, num
) >= 0)
67 PARA_EMERG_LOG("semop failed %d times, aborting\n", MAX_SEMOP_RETRIES
);
72 * lock the given mutex
74 * \param id of the shared memory area to lock
76 * This function either succeeds or aborts.
78 * \sa semop(2), struct misc_meta_data
80 void mutex_lock(int id
)
82 struct sembuf sops
[2] = {
94 para_semop(id
, sops
, 2);
100 * \param id the identifier of the mutex
102 * This function either succeeds or aborts.
104 * \sa semop(2), struct misc_meta_data
106 void mutex_unlock(int id
)
108 struct sembuf sops
[1] = {
115 para_semop(id
, sops
, 1);
119 * create a new shared memory area of given size
121 * \param size the size of the shared memory area to create
123 * \return The id of the shared memory areay on success, \a -E_SHM_GET on errors.
127 int shm_new(size_t size
)
129 int ret
= shmget(IPC_PRIVATE
, size
, IPC_CREAT
| IPC_EXCL
| 0600);
130 return ret
< 0 ? -E_SHM_GET
: ret
;
134 * destroy the given shared memory area
136 * \param id the shared memory id
138 * \return The return value of the underlying shmctl() call on success,
139 * \a -E_SHM_DESTROY on errors.
143 int shm_destroy(int id
)
145 struct shmid_ds shm_desc
;
146 int ret
= shmctl(id
, IPC_RMID
, &shm_desc
);
147 return ret
< 0? -E_SHM_DESTROY
: ret
;
151 * attach a shared memory segment
153 * \param id the identifier of the shared memory segment to attach
154 * \param mode either ATTACH_RO (read only) or ATTACH_RW (read/write)
155 * \param result points to the attached area just attached
157 * \return positive on success, \a -E_SHM_ATTACH on errors.
161 int shm_attach(int id
, enum shm_attach_mode mode
, void **result
)
163 if (mode
== ATTACH_RW
) {
164 *result
= shmat(id
, NULL
, 0);
165 return *result
? 1 : -E_SHM_ATTACH
;
167 *result
= shmat(id
, NULL
, SHM_RDONLY
);
168 return *result
? 1 : -E_SHM_ATTACH
;
172 * detach a shared memory segment
174 * \param addr the address of the attached segment
176 * \return positive on success, \a -E_SHM_DETACH on errors.
180 int shm_detach(void *addr
)
182 int ret
= shmdt(addr
);
183 return ret
< 0? -E_SHM_DETACH
: 1;