📄 linux-lock.c
字号:
/* -------------------------------------------------------------------------- *//* *//* 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -