Skip to content

Replace semaphore with mutex on zos #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/ibmras/monitoring/agent/threads/WorkerThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
* limitations under the License.
*******************************************************************************/

#if defined(_ZOS)
#define _XOPEN_SOURCE_EXTENDED 1
#define _UNIX03_THREADS
#include <pthread.h>
#include <sys/time.h>
#endif

#include "ibmras/monitoring/agent/threads/WorkerThread.h"
#include "ibmras/monitoring/agent/Agent.h"
Expand All @@ -26,12 +32,23 @@ namespace threads {

extern IBMRAS_DECLARE_LOGGER;


#if defined(_ZOS)
WorkerThread::WorkerThread(pullsource* pullSource) : data(threadEntry, cleanUp), countdown(0) {
#else
WorkerThread::WorkerThread(pullsource* pullSource) : semaphore(0, 1, pullSource->header.name), data(threadEntry, cleanUp), countdown(0) {
#endif
source = pullSource;
running = false;
stopped = true;
data.setArgs(this);
#if defined(_ZOS)
lock = new pthread_mutex_t;
pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(lock);
cond = new pthread_cond_t;
pthread_cond_t* condition = reinterpret_cast<pthread_cond_t*>(cond);
pthread_mutex_init(mutex, NULL);
pthread_cond_init(condition, NULL);
#endif
}


Expand All @@ -55,8 +72,12 @@ void WorkerThread::stop() {
// We've already set running=false, so processLoop will finish the
// next chance it gets and only then will set stopped=true.
//stopped = true;

#if defined(_ZOS)
pthread_mutex_destroy(reinterpret_cast<pthread_mutex_t*>(lock));
pthread_cond_destroy(reinterpret_cast<pthread_cond_t*>(cond));
#else
semaphore.inc();
#endif
IBMRAS_DEBUG_1(debug, "Worker thread for %s stopping", source->header.name);
}

Expand All @@ -74,7 +95,11 @@ void* WorkerThread::threadEntry(ibmras::common::port::ThreadData* data) {
void WorkerThread::process(bool immediate) {
IBMRAS_DEBUG_2(finest, "Worker thread process for %s, countdown is %d", source->header.name, countdown);
if ((immediate && countdown > 120) || (countdown == 0)) {
#if defined(_ZOS)
pthread_cond_signal(reinterpret_cast<pthread_cond_t*>(cond));// Notify one waiting thread
#else
semaphore.inc();
#endif
countdown = source->pullInterval;
} else {
countdown--;
Expand All @@ -89,7 +114,19 @@ void* WorkerThread::processLoop() {
IBMRAS_DEBUG_1(finest, "Worker thread started for %s", source->header.name);
Agent* agent = Agent::getInstance();
while (running) {
#if defined(_ZOS)
struct timeval now;
struct timespec timeout;
gettimeofday(&now, NULL);
timeout.tv_sec = now.tv_sec + 1;
timeout.tv_nsec = now.tv_usec * 1000;
pthread_mutex_lock(reinterpret_cast<pthread_mutex_t*>(lock));
int rc = pthread_cond_timedwait(reinterpret_cast<pthread_cond_t*>(cond),reinterpret_cast<pthread_mutex_t*>(lock), &timeout);
pthread_mutex_unlock(reinterpret_cast<pthread_mutex_t*>(lock));
if (0 == rc && running) {
#else
if (semaphore.wait(1) && running) {
#endif
IBMRAS_DEBUG_1(fine, "Pulling data from source %s", source->header.name);
monitordata* data = source->callback();
if (data != NULL) {
Expand Down
5 changes: 5 additions & 0 deletions src/ibmras/monitoring/agent/threads/WorkerThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ class WorkerThread {
void* processLoop();
bool running;
bool stopped;
#if defined(_ZOS)
void* lock;
void* cond;
#else
ibmras::common::port::Semaphore semaphore; /* sempahore to control data processing */
#endif
pullsource* source; /* source to pull data from */
ibmras::common::port::ThreadData data;
int countdown;
Expand Down