static int mm_do_lock(mm_mutex *lock, int num) {
struct sembuf op;
int rc;
op.sem_num = (unsigned short)num;
op.sem_op = -1;
op.sem_flg = SEM_UNDO;
do {
rc = semop(lock->semid, &op, 1);
} while (rc < 0 && errno == EINTR);
return (rc == 0);
}
static int mm_do_unlock(mm_mutex *lock, int num) {
struct sembuf op;
op.sem_num = (unsigned short)num;
op.sem_op = 1;
op.sem_flg = SEM_UNDO;
if (!semop(lock->semid, &op, 1))
return 1;
return 0;
}
static void mm_destroy_lock(mm_mutex *lock) {
int n = MM_SEM_NUM;
while (n--) {
semctl(lock->semid, n, IPC_RMID, 0);
}
}
int _mm_lock(MM *mm, int num) {
return mm_do_lock(mm->lock, num);
}
int _mm_unlock(MM *mm, int num) {
return mm_do_unlock(mm->lock, num);
时间:2009-06-13 20:29
来源:chinaunix
作者:hightman
原文链接