102int qsem_init(
const char *keyfile,
int keyid,
int nsems,
bool recreate) {
107 if (keyfile != NULL) {
108 semkey = ftok(keyfile, keyid);
112 semkey = IPC_PRIVATE;
116 if ((semid = semget(semkey, nsems, IPC_CREAT | IPC_EXCL | 0666)) == -1) {
117 if (recreate ==
false)
121 if ((semid =
qsem_getid(keyfile, keyid)) >= 0)
123 if ((semid = semget(semkey, nsems, IPC_CREAT | IPC_EXCL | 0666)) == -1)
129 for (i = 0; i < nsems; i++) {
138 if (semop(semid, &sbuf, 1) != 0) {
159 key_t semkey = ftok(keyfile, keyid);
164 if ((semid = semget(semkey, 0, 0)) == -1)
184 sbuf.sem_num = semno;
186 sbuf.sem_flg = SEM_UNDO;
189 if (semop(semid, &sbuf, 1) != 0)
206 sbuf.sem_num = semno;
208 sbuf.sem_flg = SEM_UNDO | IPC_NOWAIT;
211 if (semop(semid, &sbuf, 1) != 0)
234 for (wait = 0; wait < maxwaitms; wait += 10) {
236 if (forceflag != NULL)
243 DEBUG(
"force to unlock semaphore %d-%d", semid, semno);
250 if (forceflag != NULL)
267 sbuf.sem_num = semno;
269 sbuf.sem_flg = SEM_UNDO;
272 if (semop(semid, &sbuf, 1) != 0)
286 if (semctl(semid, semno, GETVAL, 0) == 0)
301 if (semctl(semid, 0, IPC_RMID, 0) != 0)
bool qsem_enter_force(int semid, int semno, int maxwaitms, bool *forceflag)
Force to turn on the flag of semaphore.
bool qsem_leave(int semid, int semno)
Turn off the flag of semaphore then leaving critical section.
bool qsem_enter_nowait(int semid, int semno)
Try to turn on the flag of semaphore.
int qsem_getid(const char *keyfile, int keyid)
Get semaphore identifier by keyfile and keyid for the existing semaphore.
bool qsem_free(int semid)
Release semaphore to system.
bool qsem_check(int semid, int semno)
Get the status of semaphore.
int qsem_init(const char *keyfile, int keyid, int nsems, bool recreate)
Initialize semaphore.
bool qsem_enter(int semid, int semno)
Turn on the flag of semaphore then entering critical section.