⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sem.c

📁 CFL是Unix下的通用抽象库,以简化Unix下的系统软件开发,CFL库中包含几个分组的函数和宏
💻 C
字号:
/* ----------------------------------------------------------------------------   CFL - A C Foundation Library   Copyright (C) 1994-2003  Mark A Lindner   This file is part of CFL.      This library is free software; you can redistribute it and/or   modify it under the terms of the GNU Library General Public   License as published by the Free Software Foundation; either   version 2 of the License, or (at your option) any later version.      This library is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   Library General Public License for more details.      You should have received a copy of the GNU Library General Public   License along with this library; if not, write to the Free   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   ----------------------------------------------------------------------------   $Log: sem.c,v $   Revision 1.2  2003/03/14 09:02:30  markl   Code cleanup & bug fixes.   Revision 1.1  2003/03/03 08:20:08  markl   Initial checkin (clean compile on Solaris & OS X)   ----------------------------------------------------------------------------*//* Feature test switches */#include "config.h"/* System headers */#include <fcntl.h>#include <semaphore.h>/* Local headers */#include "cfl/defs.h"#include "cfl/ipc.h"#include "cfl/system.h"#include "cfl/util.h"/* Functions */c_sem_t *C_sem_create(const char *name, mode_t mode, uint_t value)  {  c_sem_t *sem;  sem_t *s;  if((value < 1) || (value > C_SEM_MAX_VALUE))    return(NULL);    if((s = sem_open(name, (O_CREAT | O_RDWR), mode, value)) == SEM_FAILED)    return(NULL);    sem = C_new(c_sem_t);  sem->sem = s;  sem->name = C_string_dup(name);  sem->initial_value = value;  return(sem);  }/* */void C_sem_destroy(c_sem_t *sem)  {  if(!sem)    return;  sem_close(sem->sem);  sem_unlink(sem->name);  C_free(sem->name);  C_free(sem);  }/* */c_bool_t C_sem_wait(c_sem_t *sem)  {  if(!sem || !(sem->sem))    return(FALSE);  return(sem_wait(sem->sem) == 0);  }/* */c_bool_t C_sem_trywait(c_sem_t *sem)  {  if(!sem || !(sem->sem))    return(FALSE);  return(sem_trywait(sem->sem) == 0);  }/* */c_bool_t C_sem_post(c_sem_t *sem)  {  if(!sem || !(sem->sem))    return(FALSE);  return(sem_post(sem->sem) == 0);  }/* */int C_sem_value(c_sem_t *sem)  {  int val, r;    if(!sem || !(sem->sem))    return(0);  r = sem_getvalue(sem->sem, &val);  return((errno == 0) ? val : -1);  }/* end of source file */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -