Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

SV_Semaphore_Simple.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 #include "ace/SV_Semaphore_Simple.h"
00003 #include "ace/Log_Msg.h"
00004 #include "ace/ACE.h"
00005 
00006 #if defined (ACE_LACKS_INLINE_FUNCTIONS)
00007 #include "ace/SV_Semaphore_Simple.i"
00008 #endif
00009 
00010 ACE_RCSID (ace,
00011            SV_Semaphore_Simple,
00012            "$Id: SV_Semaphore_Simple.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:22 chad Exp $")
00013 
00014 ACE_ALLOC_HOOK_DEFINE (ACE_SV_Semaphore_Simple)
00015 
00016 
00017 void
00018 ACE_SV_Semaphore_Simple::dump (void) const
00019 {
00020   ACE_TRACE ("ACE_SV_Semaphore_Simple::dump");
00021 }
00022 
00023 int
00024 ACE_SV_Semaphore_Simple::control (int cmd,
00025                                   int value,
00026                                   u_short semnum) const
00027 {
00028   ACE_TRACE ("ACE_SV_Semaphore_Simple::control");
00029   if (this->internal_id_ == -1)
00030     return -1;
00031   else
00032     {
00033       semun semctl_arg;
00034 
00035       semctl_arg.val = value;
00036       return ACE_OS::semctl (this->internal_id_,
00037                              semnum,
00038                              cmd,
00039                              semctl_arg);
00040     }
00041 }
00042 
00043 int
00044 ACE_SV_Semaphore_Simple::init (key_t k, int i)
00045 {
00046   ACE_TRACE ("ACE_SV_Semaphore_Simple::init");
00047   this->key_ = k;
00048   this->internal_id_ = i;
00049   return 0;
00050 }
00051 
00052 // General ACE_SV_Semaphore operation. Increment or decrement by a
00053 // specific amount (positive or negative; amount can`t be zero).
00054 
00055 int
00056 ACE_SV_Semaphore_Simple::op (int val, u_short n, int flags) const
00057 {
00058   ACE_TRACE ("ACE_SV_Semaphore_Simple::op");
00059   sembuf op_op;
00060 
00061   op_op.sem_num = n;
00062   op_op.sem_flg = flags;
00063 
00064   if (this->internal_id_ == -1)
00065     return -1;
00066   else if ((op_op.sem_op = val) == 0)
00067     return -1;
00068   else
00069     return ACE_OS::semop (this->internal_id_, &op_op, 1);
00070 }
00071 
00072 // Open or create one or more SV_Semaphores.  We return 0 if all is
00073 // OK, else -1.
00074 
00075 int
00076 ACE_SV_Semaphore_Simple::open (key_t k,
00077                                int flags,
00078                                int initial_value,
00079                                u_short n,
00080                                int perms)
00081 {
00082   ACE_TRACE ("ACE_SV_Semaphore_Simple::open");
00083   union semun ivalue;
00084 
00085   if (k == IPC_PRIVATE || k == ACE_static_cast (key_t, ACE_INVALID_SEM_KEY))
00086     return -1;
00087 
00088   ivalue.val = initial_value;
00089   this->key_ = k;
00090   this->sem_number_ = n;
00091 
00092   this->internal_id_ = ACE_OS::semget (this->key_, n, perms | flags);
00093 
00094   if (this->internal_id_ == -1)
00095     return -1;
00096 
00097   if (ACE_BIT_ENABLED (flags, IPC_CREAT))
00098     for (int i = 0; i < n; i++)
00099       if (ACE_OS::semctl (this->internal_id_, i, SETVAL, ivalue) == -1)
00100         return -1;
00101 
00102   return 0;
00103 }
00104 
00105 ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple (key_t k,
00106                                                   int flags,
00107                                                   int initial_value,
00108                                                   u_short n,
00109                                                   int perms)
00110   : key_ (k)
00111 {
00112   ACE_TRACE ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple");
00113   if (this->open (k, flags, initial_value, n, perms) == -1)
00114     ACE_ERROR ((LM_ERROR,  ACE_LIB_TEXT ("%p\n"),  ACE_LIB_TEXT ("ACE_SV_Semaphore::ACE_SV_Semaphore")));
00115 }
00116 
00117 // Convert name to key.  This function is used internally to create keys
00118 // for the semaphores.
00119 //
00120 // The method for generating names is a 32 bit CRC, but still we
00121 // measured close to collition ratio of nearly 0.1% for
00122 // ACE::unique_name()-like strings.
00123 
00124 key_t
00125 ACE_SV_Semaphore_Simple::name_2_key (const char *name)
00126 {
00127   ACE_TRACE ("ACE_SV_Semaphore_Simple::name_2_key");
00128 
00129   if (name == 0)
00130     {
00131       errno = EINVAL;
00132       return ACE_static_cast (key_t, ACE_INVALID_SEM_KEY);
00133     }
00134 
00135   // Basically "hash" the values in the <name>.  This won't
00136   // necessarily guarantee uniqueness of all keys.
00137   // But (IMHO) CRC32 is good enough for most purposes (Carlos)
00138 #if defined (ACE_WIN64)
00139   // The cast below is legit...
00140 #  pragma warning(push)
00141 #  pragma warning(disable : 4312)
00142 #endif /* ACE_WIN64 */
00143   return (key_t) ACE::crc32 (name);
00144 #if defined (ACE_WIN64)
00145 #  pragma warning(pop)
00146 #endif /* ACE_WIN64 */
00147 }
00148 
00149 // Open or create a ACE_SV_Semaphore.  We return 1 if all is OK, else
00150 // 0.
00151 
00152 int
00153 ACE_SV_Semaphore_Simple::open (const char *name,
00154                                int flags,
00155                                int initial_value,
00156                                u_short n,
00157                                int perms)
00158 {
00159   ACE_TRACE ("ACE_SV_Semaphore_Simple::open");
00160 
00161   key_t key;
00162 
00163   if (name == 0)
00164     key = ACE_DEFAULT_SEM_KEY;
00165   else
00166     key = this->name_2_key (name);
00167 
00168   return this->open (key, flags, initial_value, n, perms);
00169 }
00170 
00171 ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple (const char *name,
00172                                                   int flags,
00173                                                   int initial_value,
00174                                                   u_short n,
00175                                                   int perms)
00176 {
00177   ACE_TRACE ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple");
00178   if (this->open (name,
00179                   flags,
00180                   initial_value,
00181                   n,
00182                   perms) == -1)
00183     ACE_ERROR ((LM_ERROR,
00184                 ACE_LIB_TEXT ("%p\n"),
00185                 ACE_LIB_TEXT ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple")));
00186 }
00187 
00188 ACE_SV_Semaphore_Simple::~ACE_SV_Semaphore_Simple (void)
00189 {
00190   ACE_TRACE ("ACE_SV_Semaphore_Simple::~ACE_SV_Semaphore_Simple");
00191   this->close ();
00192 }
00193 
00194 ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple (void)
00195 {
00196   ACE_TRACE ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple");
00197   this->init ();
00198 }
00199 
00200 // Remove all SV_Semaphores associated with a particular key.  This
00201 // call is intended to be called from a server, for example, when it
00202 // is being shut down, as we do an IPC_RMID on the ACE_SV_Semaphore,
00203 // regardless of whether other processes may be using it or not.  Most
00204 // other processes should use close() below.
00205 
00206 int
00207 ACE_SV_Semaphore_Simple::remove (void) const
00208 {
00209   ACE_TRACE ("ACE_SV_Semaphore_Simple::remove");
00210   int result = this->control (IPC_RMID);
00211   ((ACE_SV_Semaphore_Simple *) this)->init ();
00212   return result;
00213 }

Generated on Mon Jun 16 11:21:33 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002