00001 /* 00002 Simple Semaphore Interface - POSIX implementation 00003 AUP2, Sec. 7.10.1 00004 00005 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00006 May be copied only for purposes and under conditions described 00007 on the Web page www.basepath.com/aup/copyright.htm. 00008 00009 The Example Files are provided "as is," without any warranty; 00010 without even the implied warranty of merchantability or fitness 00011 for a particular purpose. The author and his publisher are not 00012 responsible for any damages, direct or incidental, resulting 00013 from the use or non-use of these Example Files. 00014 00015 The Example Files may contain defects, and some contain deliberate 00016 coding mistakes that were included for educational reasons. 00017 You are responsible for determining if and how the Example Files 00018 are to be used. 00019 00020 */ 00021 #include "defs.h" 00022 00023 /* 00024 There may exist a POSIX semaphore implementation for these systems, 00025 but I haven't located it yet. Change next line if you find one. 00026 and then please email me at aup@basepath.com. 00027 */ 00028 #if !defined(FREEBSD) && !defined(LINUX) 00029 00030 #include "SimpleSem.h" 00031 #include <semaphore.h> 00032 00033 /*[SSPosix]*/ 00034 struct SimpleSem *SimpleSemOpen(const char *name) 00035 { 00036 struct SimpleSem *sem = NULL; 00037 00038 ec_null( sem = malloc(sizeof(struct SimpleSem)) ) 00039 if ((sem->sm.sm_sem = sem_open(name, O_CREAT, PERM_FILE, 0)) == 00040 SEM_FAILED) 00041 EC_FAIL 00042 return sem; 00043 00044 EC_CLEANUP_BGN 00045 free(sem); 00046 return NULL; 00047 EC_CLEANUP_END 00048 } 00049 00050 bool SimpleSemPost(struct SimpleSem *sem) 00051 { 00052 ec_neg1( sem_post(sem->sm.sm_sem) ) 00053 return true; 00054 00055 EC_CLEANUP_BGN 00056 return false; 00057 EC_CLEANUP_END 00058 } 00059 00060 bool SimpleSemWait(struct SimpleSem *sem) 00061 { 00062 ec_neg1( sem_wait(sem->sm.sm_sem) ) 00063 return true; 00064 00065 EC_CLEANUP_BGN 00066 return false; 00067 EC_CLEANUP_END 00068 } 00069 00070 bool SimpleSemClose(struct SimpleSem *sem) 00071 { 00072 ec_neg1( sem_close(sem->sm.sm_sem) ) 00073 free(sem); 00074 return true; 00075 00076 EC_CLEANUP_BGN 00077 return false; 00078 EC_CLEANUP_END 00079 } 00080 00081 bool SimpleSemRemove(const char *name) 00082 { 00083 if (unlink(name) == -1 && errno != ENOENT) 00084 EC_FAIL 00085 return true; 00086 00087 EC_CLEANUP_BGN 00088 return false; 00089 EC_CLEANUP_END 00090 } 00091 /*[]*/ 00092 00093 #else /* FREEBSD or LINUX */ 00094 00095 struct SimpleSem *SimpleSemOpen(const char *name) 00096 { 00097 errno = ENOSYS; 00098 return NULL; 00099 } 00100 00101 bool SimpleSemPost(struct SimpleSem *sem) 00102 { 00103 errno = ENOSYS; 00104 return false; 00105 } 00106 00107 bool SimpleSemWait(struct SimpleSem *sem) 00108 { 00109 errno = ENOSYS; 00110 return false; 00111 } 00112 00113 bool SimpleSemClose(struct SimpleSem *sem) 00114 { 00115 errno = ENOSYS; 00116 return false; 00117 } 00118 00119 bool SimpleSemRemove(const char *name) 00120 { 00121 errno = ENOSYS; 00122 return false; 00123 } 00124 00125 #endif /* FREEBSD or LINUX */