00001
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "config.h"
00019 #include "defines.h"
00020
00021 #ifdef CF_SHARED_MEM
00022
00023 #include <sys/types.h>
00024
00025 #include <sys/ipc.h>
00026 #include <sys/shm.h>
00027 #include <sys/sem.h>
00028
00029 #include <unistd.h>
00030
00031 #include "semaphores.h"
00032 #include "shm_locking.h"
00033
00034
00041 int shm_lock_exclusive(int semid) {
00042
00043 if(CF_SEM_UNLOCK(semid,CF_WRITER_SEM) == -1) return -1;
00044
00045
00046 if(CF_SEM_WAITZERO(semid,CF_READER_SEM) == -1) {
00047 CF_SEM_LOCK(semid,CF_WRITER_SEM);
00048 return -1;
00049 }
00050
00051
00052 if(CF_SEM_LOCK(semid,CF_LOCK_SEM) == -1) {
00053 CF_SEM_LOCK(semid,CF_WRITER_SEM);
00054 return -1;
00055 }
00056
00057 return 0;
00058 }
00059
00063 int shm_unlock_exclusive(int semid) {
00064 if(CF_SEM_UNLOCK(semid,0) == -1) return -1;
00065
00066 return CF_SEM_LOCK(semid,2);
00067 }
00068
00075 int shm_lock_shared(int semid) {
00076 if(CF_SEM_WAITZERO(semid,CF_WRITER_SEM) == -1) return -1;
00077
00078 return CF_SEM_UNLOCK(semid,CF_READER_SEM);
00079 }
00080
00084 int shm_unlock_shared(int semid) {
00085 return CF_SEM_LOCK(semid,CF_READER_SEM);
00086 }
00087
00088 #endif
00089
00090