linux-lock.c

来自「wm PNE 3.3 source code, running at more 」· C语言 代码 · 共 112 行

C
112
字号
/* -------------------------------------------------------------------------- *//*                                                                            *//* Filename: linux-lock.c                                                     *//*                                                                            *//* Author:   Icon Laboratories, Inc.                                          *//*           (888) 235-3443                                                   *//*           http://www.icon-labs.com                                         *//*                                                                            *//* Notice:   Copyright 2001, Icon Laboratories, Inc.  All rights reserved.    *//*                                                                            *//* Description: This file contains common functions for MIB-2 Agent           *//*              based on WindRiver's Envoy/Emissary tools.                    *//*                                                                            *//* Modification History:                                                      *//*                                                                            *//* Jim Jones   v1.2    07-20-01    Removed explicit include for envoy.h       *//* Jim Jones   v1.1    04-23-01    Recompiled for Envoy v9.1                  *//*                                                                            *//* -------------------------------------------------------------------------- *//* this is the lock implementation for Linux.  It uses two pthread * mutexes to implement the classic solution to the reader- * writers problem.  It favors readers over writers.  There are  * other solutions that favor writers over readers.  See, for  * example:  *  http://www.coe.neu.edu/~signoril/op_sys/readwriters.html *  http://www.cs.bc.edu/~mc362/readwriters.html *        or, for more general info: *  http://rtlab.kaist.ac.kr/~sikang/sim/rw.html */#include <wrn/wm/snmp/engine/asn1conf.h>#include <wrn/wm/snmp/engine/asn1.h>#include <wrn/wm/snmp/engine/buffer.h>#include <wrn/wm/snmp/engine/mib.h>#include <wrn/wm/snmp/engine/localio.h>#include <wrn/wm/snmp/engine/snmpdefs.h>#include <wrn/wm/snmp/engine/snmp.h>#include <wrn/wm/snmp/engine/auxfuncs.h>#include <stdio.h>#include <errno.h>#if (INSTALL_ENVOY_SNMP_LOCK)int reader_lock(ENVOY_LOCK_T *lock){  /* lock the reader mutex */  if ( pthread_mutex_lock( &(lock->reader) ) )    return -1;  /* increment the readers count */  lock->nreaders++;    /* if readers count == 1, meaning this is the first reader,   * lock the writers lock */  if ( lock->nreaders == 1 )  {    if ( pthread_mutex_lock( &(lock->writer) ) )      return -1;  }  /* release the reader lock */  if ( pthread_mutex_unlock( &(lock->reader) ) )    return -1;  return 0;}int reader_unlock(ENVOY_LOCK_T *lock){  /* lock the reader mutex */  if ( pthread_mutex_lock( &(lock->reader) ) )    return -1;  /* decrement the readers count */  lock->nreaders--;  /* if readers count == 0, meaning this is the last reader,   * unlock the writers lock */  if ( lock->nreaders == 0 )  {    if ( pthread_mutex_unlock( &(lock->writer) ) )      return -1;  }  /* release the reader lock */  if ( pthread_mutex_unlock( &(lock->reader) ) )    return -1;  return 0;}int writer_lock(ENVOY_LOCK_T *lock){  if ( pthread_mutex_lock( &(lock->writer) ) )    return -1;  return 0;}int writer_unlock(ENVOY_LOCK_T *lock){  if ( pthread_mutex_unlock( &(lock->writer) ) )    return -1;      return 0;}#endif

⌨️ 快捷键说明

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