diff options
author | nw1 <nw1@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1997-11-03 20:18:41 +0000 |
---|---|---|
committer | nw1 <nw1@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1997-11-03 20:18:41 +0000 |
commit | aeaade2fa3c91df605f391522fa3d49e2c6982f5 (patch) | |
tree | 5911f2b7481ab6da2e10a1187adb0948bebc7df4 /ace/SV_Semaphore_Complex.cpp | |
parent | 8cc64913985e73f3f822159930cb21136a261224 (diff) | |
download | ATCD-aeaade2fa3c91df605f391522fa3d49e2c6982f5.tar.gz |
(close): Changed to return error only when this->key_ == -1 instead of when this->key_ <= -1.
Diffstat (limited to 'ace/SV_Semaphore_Complex.cpp')
-rw-r--r-- | ace/SV_Semaphore_Complex.cpp | 152 |
1 files changed, 76 insertions, 76 deletions
diff --git a/ace/SV_Semaphore_Complex.cpp b/ace/SV_Semaphore_Complex.cpp index db857edde00..5ab615707b3 100644 --- a/ace/SV_Semaphore_Complex.cpp +++ b/ace/SV_Semaphore_Complex.cpp @@ -14,51 +14,51 @@ ACE_SV_Semaphore_Complex::dump (void) const ACE_TRACE ("ACE_SV_Semaphore_Complex::dump"); } -// initial value of process +// initial value of process const int ACE_SV_Semaphore_Complex::BIGCOUNT_ = 10000; -// Define the ACE_SV_Semaphore operation arrays for the semop() calls. -sembuf ACE_SV_Semaphore_Complex::op_lock_[2] = +// Define the ACE_SV_Semaphore operation arrays for the semop() calls. +sembuf ACE_SV_Semaphore_Complex::op_lock_[2] = { - {0, 0, 0}, // Wait for [0] (lock) to equal 0 - {0, 1, SEM_UNDO}, // then increment [0] to 1 - this locks it. - // UNDO to release the lock if processes exit - // before explicitly unlocking. + {0, 0, 0}, // Wait for [0] (lock) to equal 0 + {0, 1, SEM_UNDO}, // then increment [0] to 1 - this locks it. + // UNDO to release the lock if processes exit + // before explicitly unlocking. }; -sembuf ACE_SV_Semaphore_Complex::op_endcreate_[2] = +sembuf ACE_SV_Semaphore_Complex::op_endcreate_[2] = { - {1, -1, SEM_UNDO}, // Decrement [1] (proc counter) with undo on - // exit, UNDO to adjust proc counter if - // process exits before explicitly calling close() - {0, -1, SEM_UNDO}, // the decrement [0] (lock) back to 0 + {1, -1, SEM_UNDO}, // Decrement [1] (proc counter) with undo on + // exit, UNDO to adjust proc counter if + // process exits before explicitly calling close() + {0, -1, SEM_UNDO}, // the decrement [0] (lock) back to 0 }; -sembuf ACE_SV_Semaphore_Complex::op_open_[1] = +sembuf ACE_SV_Semaphore_Complex::op_open_[1] = { - {1, -1, SEM_UNDO}, // Decrement [1] (proc counter) with undo on - // exit. + {1, -1, SEM_UNDO}, // Decrement [1] (proc counter) with undo on + // exit. }; -sembuf ACE_SV_Semaphore_Complex::op_close_[3] = +sembuf ACE_SV_Semaphore_Complex::op_close_[3] = { - {0, 0, 0}, // Wait for [0] (lock) to equal 0 - {0, 1, SEM_UNDO}, // then increment [0] to 1 - this lock it - {1, 1, SEM_UNDO}, // then increment [1] (proc counter) + {0, 0, 0}, // Wait for [0] (lock) to equal 0 + {0, 1, SEM_UNDO}, // then increment [0] to 1 - this lock it + {1, 1, SEM_UNDO}, // then increment [1] (proc counter) }; -sembuf ACE_SV_Semaphore_Complex::op_unlock_[1] = +sembuf ACE_SV_Semaphore_Complex::op_unlock_[1] = { - {0, -1, SEM_UNDO}, // Decrement [0] (lock) back to 0 + {0, -1, SEM_UNDO}, // Decrement [0] (lock) back to 0 }; -// Open or create an array of SV_Semaphores. We return 0 if all is OK, else -1. +// Open or create an array of SV_Semaphores. We return 0 if all is OK, else -1. -int -ACE_SV_Semaphore_Complex::open (key_t k, - int create, - int initial_value, - u_short nsems, +int +ACE_SV_Semaphore_Complex::open (key_t k, + int create, + int initial_value, + u_short nsems, int perms) { ACE_TRACE ("ACE_SV_Semaphore_Complex::open"); @@ -66,24 +66,24 @@ ACE_SV_Semaphore_Complex::open (key_t k, return -1; this->key_ = k; - + if (create == ACE_SV_Semaphore_Complex::ACE_CREATE) { int result; do { - this->internal_id_ = ACE_OS::semget + this->internal_id_ = ACE_OS::semget (this->key_, (u_short) 2 + nsems, perms | ACE_SV_Semaphore_Complex::ACE_CREATE); if (this->internal_id_ == -1) - return -1; // permission problem or tables full - + return -1; // permission problem or tables full + // When the ACE_SV_Semaphore is created, we know that the // value of all 3 members is 0. Get a lock on the // ACE_SV_Semaphore by waiting for [0] to equal 0, then // increment it. - + // There is a race condition here. There is the possibility // that between the semget() above and the semop() below, // another process can call out close() function which can @@ -91,8 +91,8 @@ ACE_SV_Semaphore_Complex::open (key_t k, // one using it. Therefor we handle the error condition of // an invalid ACE_SV_Semaphore ID specifically below, and if // it does happen, we just go back and create it again. - result = ACE_OS::semop (this->internal_id_, - &ACE_SV_Semaphore_Complex::op_lock_[0], + result = ACE_OS::semop (this->internal_id_, + &ACE_SV_Semaphore_Complex::op_lock_[0], 2); } while (result == -1 && (errno == EINVAL || errno == EIDRM)); @@ -102,9 +102,9 @@ ACE_SV_Semaphore_Complex::open (key_t k, // Get the value of the process counter. If it equals 0, then no // one has initialized the ACE_SV_Semaphore yet. - + int semval = ACE_SV_Semaphore_Simple::control (GETVAL, 0, 1); - + if (semval == -1) return this->init (); else if (semval == 0) @@ -113,45 +113,45 @@ ACE_SV_Semaphore_Complex::open (key_t k, // clear the adjust value that we set when we locked the // ACE_SV_Semaphore above. Instead we do system calls to // initialize [1], as well as all the nsems SV_Semaphores. - - if (ACE_SV_Semaphore_Simple::control (SETVAL, - ACE_SV_Semaphore_Complex::BIGCOUNT_, + + if (ACE_SV_Semaphore_Simple::control (SETVAL, + ACE_SV_Semaphore_Complex::BIGCOUNT_, 1) == -1) return -1; else for (int i = 0; i < nsems; i++) if (this->control (SETVAL, initial_value, i) == -1) return -1; - } - - // Decrement the process counter and then release the lock. - return ACE_OS::semop (this->internal_id_, - &ACE_SV_Semaphore_Complex::op_endcreate_[0], + } + + // Decrement the process counter and then release the lock. + return ACE_OS::semop (this->internal_id_, + &ACE_SV_Semaphore_Complex::op_endcreate_[0], 2); } - else + else { this->internal_id_ = ACE_OS::semget (this->key_, 2 + nsems, 0); if (this->internal_id_ == -1) - return -1; // doesn't exist or tables full - - // Decrement the process counter. We don't need a lock to do this. - if (ACE_OS::semop (this->internal_id_, + return -1; // doesn't exist or tables full + + // Decrement the process counter. We don't need a lock to do this. + if (ACE_OS::semop (this->internal_id_, &ACE_SV_Semaphore_Complex::op_open_[0], 1) < 0) return this->init (); return 0; } } -int -ACE_SV_Semaphore_Complex::open (const char *name, - int flags, - int initial_value, - u_short nsems, +int +ACE_SV_Semaphore_Complex::open (const char *name, + int flags, + int initial_value, + u_short nsems, int perms) { ACE_TRACE ("ACE_SV_Semaphore_Complex::open"); - return this->open (ACE_SV_Semaphore_Simple::name_2_key (name), + return this->open (ACE_SV_Semaphore_Simple::name_2_key (name), flags, initial_value, nsems, perms); } @@ -161,47 +161,47 @@ ACE_SV_Semaphore_Complex::open (const char *name, // the ACE_SV_Semaphore, and if this was the last one, we can remove // the ACE_SV_Semaphore. -int +int ACE_SV_Semaphore_Complex::close (void) { ACE_TRACE ("ACE_SV_Semaphore_Complex::close"); int semval; - - if (this->key_ <= (key_t) - 1 || this->internal_id_ == -1) + + if (this->key_ == (key_t) - 1 || this->internal_id_ == -1) return -1; - + // The following semop() first gets a lock on the ACE_SV_Semaphore, // then increments [1] - the process number. - - if (ACE_OS::semop (this->internal_id_, - &ACE_SV_Semaphore_Complex::op_close_[0], + + if (ACE_OS::semop (this->internal_id_, + &ACE_SV_Semaphore_Complex::op_close_[0], 3) == -1) return -1; - + // Now that we have a lock, read the value of the process counter to // see if this is the last reference to the ACE_SV_Semaphore. There // is a race condition here - see the comments in create (). - + if ((semval = ACE_SV_Semaphore_Simple::control (GETVAL, 0, 1)) == -1) return -1; - + if (semval > ACE_SV_Semaphore_Complex::BIGCOUNT_) return -1; else if (semval == ACE_SV_Semaphore_Complex::BIGCOUNT_) return this->remove (); else { - int result = ACE_OS::semop (this->internal_id_, + int result = ACE_OS::semop (this->internal_id_, &ACE_SV_Semaphore_Complex::op_unlock_[0], 1); this->init (); return result; } } -ACE_SV_Semaphore_Complex::ACE_SV_Semaphore_Complex (key_t k, - int flags, - int initial_value, - u_short nsems, +ACE_SV_Semaphore_Complex::ACE_SV_Semaphore_Complex (key_t k, + int flags, + int initial_value, + u_short nsems, int perms) { ACE_TRACE ("ACE_SV_Semaphore_Complex::ACE_SV_Semaphore_Complex"); @@ -209,19 +209,19 @@ ACE_SV_Semaphore_Complex::ACE_SV_Semaphore_Complex (key_t k, ACE_ERROR ((LM_ERROR, "%p\n", "ACE_SV_Semaphore_Complex")); } -ACE_SV_Semaphore_Complex::ACE_SV_Semaphore_Complex (const char *name, - int flags, - int initial_value, - u_short nsems, +ACE_SV_Semaphore_Complex::ACE_SV_Semaphore_Complex (const char *name, + int flags, + int initial_value, + u_short nsems, int perms) { ACE_TRACE ("ACE_SV_Semaphore_Complex::ACE_SV_Semaphore_Complex"); key_t key; - + if (name == 0) key = ACE_DEFAULT_SEM_KEY; - else + else key = this->name_2_key (name); if (this->open (key, flags, initial_value, nsems, perms) == -1) |