Contains functions for dealing with mutexes and shared memory.
server_cmdline_objs="server.cmdline"
server_errlist_objs="server mp3 afs command net string signal random_dbtool time daemon stat
- crypt http_send db close_on_fork"
+ crypt http_send db close_on_fork ipc"
server_ldflags=""
########################################################################### ssl
SS_STRING, SS_DAEMON, SS_STAT, SS_TIME, SS_GRAB_CLIENT, SS_HTTP_RECV,
SS_RECV_COMMON, SS_FILTER_CHAIN, SS_WAV, SS_COMPRESS, SS_OGGDEC, SS_FILTER,
SS_COMMAND, SS_RANDOM_DBTOOL, SS_CRYPT, SS_HTTP_SEND, SS_ORTP_SEND, SS_DB, SS_OGG,
- SS_MP3, SS_MP3DEC, SS_SERVER, SS_AFS, SS_MYSQL, SS_RINGBUFFER};
+ SS_MP3, SS_MP3DEC, SS_SERVER, SS_AFS, SS_MYSQL, SS_IPC, SS_RINGBUFFER};
#define NUM_SS (SS_RINGBUFFER + 1)
extern const char **para_errlist[];
/** \endcond */
PARA_ERROR(LOCK, "lock error"), \
PARA_ERROR(SENDER_CMD, "command not supported by this sender"), \
+#define IPC_ERRORS \
+ PARA_ERROR(SEM_GET, "failed to create semaphore"), \
+ PARA_ERROR(SEM_REMOVE, "can not remove semaphore"), \
+ PARA_ERROR(SHM_GET, "failed to allocate shared memory area"), \
+ PARA_ERROR(SHM_DESTROY, "failed to destroy shared memory area"), \
+ PARA_ERROR(SHM_DETACH, "can not detach shared memory area"), \
/* these do not need error handling (yet) */
#define SERVER_ERRORS
SS_ENUM(ORTP_SEND);
SS_ENUM(DB);
SS_ENUM(MYSQL);
+SS_ENUM(IPC);
SS_ENUM(RINGBUFFER);
/** \endcond */
#undef PARA_ERROR
--- /dev/null
+#include "para.h"
+#include "error.h"
+#include "ipc.h"
+#include <sys/ipc.h>
+#include <sys/shm.h>
+
+
+int mutex_new(void)
+{
+ int ret = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666);
+ return ret < 0? -E_SEM_GET : ret;
+}
+
+int mutex_remove(int id)
+{
+ int ret = semctl(id, 0, IPC_RMID);
+ return ret < 0? -E_SEM_REMOVE : 1;
+}
+
+static void para_semop(int id, struct sembuf *sops, int num)
+{
+ if (semop(id, sops, num) >= 0)
+ return;
+ PARA_WARNING_LOG("semop failed (%s), retrying\n", strerror(errno));
+ while (semop(id, sops, num) < 0)
+ ; /* nothing */
+}
+
+/**
+ * lock the given mutex
+ *
+ * \sa semop(2), struct misc_meta_data
+ */
+void mutex_lock(int id)
+{
+ struct sembuf sops[2] = {
+ {
+ .sem_num = 0,
+ .sem_op = 0,
+ .sem_flg = SEM_UNDO
+ },
+ {
+ .sem_num = 0,
+ .sem_op = 1,
+ .sem_flg = SEM_UNDO
+ }
+ };
+ para_semop(id, sops, 2);
+}
+
+/**
+ * unlock a mutex
+ *
+ * \sa semop(2), struct misc_meta_data
+ */
+void mutex_unlock(int id)
+{
+ struct sembuf sops[1] = {
+ {
+ .sem_num = 0,
+ .sem_op = -1,
+ .sem_flg = SEM_UNDO
+ },
+ };
+ para_semop(id, sops, 1);
+}
+
+/**
+ * create a new shared memory area of given size
+ *
+ * \sa shmget(2)
+ */
+int shm_new(size_t size)
+{
+ int ret = shmget(IPC_PRIVATE, size, IPC_CREAT | IPC_EXCL | 0600);
+ return ret < 0 ? -E_SHM_GET : ret;
+}
+
+/**
+ * destroy the given shared memory area
+ * \sa shmctl(2)
+ **/
+int shm_destroy(int id)
+{
+ struct shmid_ds shm_desc;
+ int ret = shmctl(id, IPC_RMID, &shm_desc);
+ return ret < 0? -E_SHM_DESTROY : ret;
+}
+
+/**
+ * attach a shared memory area
+ *
+ * \sa semop(2)
+ */
+void *shm_attach(int id, enum shm_attach_mode mode)
+{
+ if (mode == ATTACH_RW)
+ return shmat(id, NULL, 0);
+ return shmat(id, NULL, SHM_RDONLY);
+}
+int shm_detach(void *addr)
+{
+ int ret = shmdt(addr);
+ return ret < 0? -E_SHM_DETACH : 1;
+}
--- /dev/null
+/** \file ipc.h inter process communication and shared memory routines */
+
+enum shm_attach_mode {ATTACH_RO, ATTACH_RW};
+
+int mutex_new(void);
+void mutex_lock(int id);
+void mutex_unlock(int id);