Commit 1b7418b9 authored by Eric Cano's avatar Eric Cano
Browse files

Added missing implementation of AgentReference.

Fixed race condition detected by helgrind in ArchiveMount.
parent 390ccab7
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "AgentReference.hpp"
#include "common/exception/Errnum.hpp"
#include <sstream>
#include <unistd.h>
#include <sys/syscall.h>
#include <iomanip>
namespace cta { namespace objectstore {
AgentReference::AgentReference(const std::string & clientType) {
m_nextId=0;
std::stringstream aid;
// Get time
time_t now = time(0);
struct tm localNow;
localtime_r(&now, &localNow);
// Get hostname
char host[200];
cta::exception::Errnum::throwOnMinusOne(::gethostname(host, sizeof(host)),
"In AgentId::AgentId: failed to gethostname");
// gettid is a safe system call (never fails)
aid << clientType << "-" << host << "-" << syscall(SYS_gettid) << "-"
<< 1900 + localNow.tm_year
<< std::setfill('0') << std::setw(2)
<< 1 + localNow.tm_mon
<< std::setw(2) << localNow.tm_mday << "-"
<< std::setw(2) << localNow.tm_hour << ":"
<< std::setw(2) << localNow.tm_min << ":"
<< std::setw(2) << localNow.tm_sec;
m_agentAddress = aid.str();
}
std::string AgentReference::getAgentAddress() {
return m_agentAddress;
}
std::string AgentReference::nextId(const std::string& childType) {
std::stringstream id;
id << childType << "-" << m_agentAddress << "-" << m_nextId++;
return id.str();
}
}}
\ No newline at end of file
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "common/threading/Mutex.hpp"
#include <atomic>
#include <string>
namespace cta { namespace objectstore {
/**
* A class allowing the passing of the address of an Agent object, plus a thread safe
* object name generator, that will allow unique name generation by several threads.
* This object should be created once and for all per session (as the corresponding
* Agent object in the object store).
* A process
*/
class AgentReference {
public:
/**
* Constructor will implicitly generate the address of the Agent object.
* @param clientType is an indicative string used to generate the agent object's name.
*/
AgentReference(const std::string &clientType);
/**
* Generates a unique address for a newly created child object. This function is thread
* safe.
* @param childType the name of the child object type.
* @return a unique address for the child object, derived from the agent's address.
*/
std::string nextId(const std::string & childType);
/**
* Gets the address of the Agent object generated on construction.
* @return the agent object address.
*/
std::string getAgentAddress();
private:
std::atomic<uint64_t> m_nextId;
std::string m_agentAddress;
};
}}
......@@ -25,6 +25,7 @@
#include "catalogue/Catalogue.hpp"
#include <memory>
#include <atomic>
namespace cta {
/**
......@@ -143,7 +144,7 @@ namespace cta {
/**
* Internal tracking of the session completion
*/
bool m_sessionRunning;
std::atomic<bool> m_sessionRunning;
}; // class ArchiveMount
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment