00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "rcs_defs.hh"
00010
00011 extern "C"
00012 {
00013 #ifndef NO_STDIO
00014 #include <stdio.h>
00015 #endif
00016 #include "_sem.h"
00017 }
00018 #include "sem.hh"
00019 #include "rcs_prnt.hh"
00020 #include "timer.hh"
00021
00022
00023 RCS_SEMAPHORE::RCS_SEMAPHORE (unsigned long int _id, int _oflag, double _time,
00024 int _mode, int _state)
00025 {
00026
00027 id = _id;
00028 oflag = _oflag;
00029 mode = _mode;
00030 state = _state;
00031 timeout = _time;
00032
00033 if (oflag & RCS_SEMAPHORE_CREATE)
00034 {
00035 sem = rcs_sem_create (id, mode, state);
00036 }
00037 else
00038 {
00039 sem = rcs_sem_open ((char *) id, 0);
00040 }
00041
00042 if (sem == NULL)
00043 {
00044 rcs_print_error
00045 ("can't create semaphore (id = %d, oflag = %d, timeout = %f, mode = 0x%X, state = %d)\n",
00046 id, oflag, timeout, mode, state);
00047 }
00048 }
00049
00050 int
00051 RCS_SEMAPHORE::valid ()
00052 {
00053 return (sem != NULL);
00054 }
00055
00056 RCS_SEMAPHORE::~RCS_SEMAPHORE ()
00057 {
00058 if (sem == NULL)
00059 return;
00060
00061
00062 if (oflag & RCS_SEMAPHORE_CREATE)
00063 {
00064
00065 #ifdef LYNX
00066 char filename[32];
00067 sprintf (filename, "/tmp/sem%d", id);
00068 unlink (filename);
00069 #else
00070 rcs_sem_destroy (sem);
00071 #endif
00072 }
00073 rcs_sem_close (sem);
00074 sem = NULL;
00075 }
00076
00077 int
00078 RCS_SEMAPHORE::wait ()
00079 {
00080 int retval;
00081 #ifdef DEBUG
00082 rcs_print_debug (PRINT_SEMAPHORE_ACTIVITY,
00083 "semaphore wait started at %lf\n", etime ());
00084 #endif
00085 if (sem == NULL)
00086 return -1;
00087 retval = rcs_sem_wait (sem, timeout);
00088 #ifdef DEBUG
00089 rcs_print_debug (PRINT_SEMAPHORE_ACTIVITY,
00090 "semaphore wait finished at %lf\n", etime ());
00091 #endif
00092 return retval;
00093 }
00094
00095 int
00096 RCS_SEMAPHORE::trywait ()
00097 {
00098 if (sem == NULL)
00099 return -1;
00100 return rcs_sem_trywait (sem);
00101 }
00102
00103 int
00104 RCS_SEMAPHORE::post ()
00105 {
00106 #ifdef DEBUG
00107 rcs_print_debug (PRINT_SEMAPHORE_ACTIVITY, "semaphore posted at %lf\n",
00108 etime ());
00109 #endif
00110 if (sem == NULL)
00111 return -1;
00112 return rcs_sem_post (sem);
00113 }
00114
00115 int
00116 RCS_SEMAPHORE::flush ()
00117 {
00118 #ifdef DEBUG
00119 rcs_print_debug (PRINT_SEMAPHORE_ACTIVITY, "semaphore flushed at %lf\n",
00120 etime ());
00121 #endif
00122 if (sem == NULL)
00123 return -1;
00124 return rcs_sem_flush (sem);
00125 }
00126
00127 int
00128 RCS_SEMAPHORE::getvalue ()
00129 {
00130 if (sem == NULL)
00131 return -1;
00132 return rcs_sem_getvalue (sem, &sval);
00133 }
00134
00135 int
00136 RCS_SEMAPHORE::setflag (int _oflag)
00137 {
00138 oflag = _oflag;
00139
00140 return (0);
00141 }
00142
00143 int
00144 RCS_SEMAPHORE::clear ()
00145 {
00146 return rcs_sem_clear (sem);
00147 }
00148
00149
00150
00151
00152
00153 RCS_SEMAPHORE::RCS_SEMAPHORE (RCS_SEMAPHORE & sem)
00154 {
00155 }