在开辟好的共享内存内进行alloc/free的函数集(5)

来源:chinaunix 作者:hightman
  }

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 原文链接

好文,顶一下
(1)
50%
文章真差,踩一下
(1)
50%
------分隔线----------------------------


把开源带在你的身边-精美linux小纪念品
无觅相关文章插件,快速提升流量