<commit_before>//
// Copyright (C) 2004-2006 SIPfoundry Inc.
// Licensed by SIPfoundry under the LGPL license.
//
// Copyright (C) 2004-2006 Pingtel Corp.  All rights reserved.
// Licensed to SIPfoundry under a Contributor Agreement.
//
// Copyright (C) 2007 <NAME>
// Licensed under the LGPL license.
// $$
///////////////////////////////////////////////////////////////////////////////

// SYSTEM INCLUDES
// APPLICATION INCLUDES
#include <os/OsSharedServerTaskMgr.h>
#include <os/OsSharedServerTask.h>
#include <os/OsSharedTaskInfo.h>
#include <os/OsLock.h>
#include <os/OsPtrLock.h>
#include <os/OsSharedServerTaskMsg.h>
#include <os/OsSharedServerTaskWorker.h>
#include <utl/UtlInt.h>
#include <utl/UtlHashBagIterator.h>
#include <utl/UtlSListIterator.h>

// DEFINES
// EXTERNAL FUNCTIONS
// EXTERNAL VARIABLES
// CONSTANTS
// STATIC VARIABLE INITIALIZATIONS
// MACROS
// GLOBAL VARIABLES
// GLOBAL FUNCTIONS

/* //////////////////////////// PUBLIC //////////////////////////////////// */

/* ============================ CREATORS ================================== */

OsSharedServerTaskMgr::OsSharedServerTaskMgr(unsigned int iNumberOfThreads /*= SHARED_TASK_DEFAULT_THREADS*/,
                                             const int maxRequestQMsgs)
: m_msgQ(maxRequestQMsgs, OsMsgQ::DEF_MAX_MSG_LEN, OsMsgQ::Q_PRIORITY)
, m_state(SHARED_TASK_MGR_UNINITIALIZED)
{
   unsigned int realNumberOfThreads = iNumberOfThreads > 0 ? iNumberOfThreads : 1;

   for (unsigned int i = 0; i < realNumberOfThreads; i++)
   {
      OsSharedServerTaskWorker* pWorker = new OsSharedServerTaskWorker("OsSharedServerTaskMgr-%d", i, NULL, &m_msgQ);
      m_threadList.insert(pWorker);
      // do not start workers
   }
}

OsSharedServerTaskMgr::~OsSharedServerTaskMgr()
{
   shutdownAllWorkers(); // stop all threads
   releaseAllSharedTasks(); // unmanage all shared tasks
   flushMessages(); // delete all messages from queue
}

/* ============================ MANIPULATORS ============================== */

void OsSharedServerTaskMgr::start()
{
   OsLock lock(m_memberMutex);
   UtlSListIterator itor(m_threadList);
   while (itor())
   {
      OsSharedServerTaskWorker* pWorker = dynamic_cast<OsSharedServerTaskWorker*>(itor.item());
      if (pWorker)
      {
         pWorker->start();
      }
   }
   m_state = SHARED_TASK_MGR_STARTED;
}

void OsSharedServerTaskMgr::shutdown()
{
   shutdownAllWorkers();
}

UtlBoolean OsSharedServerTaskMgr::manage(OsSharedServerTask& serverTask)
{
   if (serverTask.isManaged())
   {
      // we cannot manage OsSharedServerTask if its already managed
      return FALSE;
   }

   UtlInt* pKey = new UtlInt(serverTask.getTaskId());
   OsLock lock(m_memberMutex);
   // first try to find if we are already managing it
   OsSharedTaskInfo* pTaskInfo = dynamic_cast<OsSharedTaskInfo*>(m_managedTaskInfo.find(pKey));
   if (!pTaskInfo)
   {
      // add it
      pTaskInfo = new OsSharedTaskInfo(serverTask.getTaskId(), &serverTask);
      m_managedTaskInfo.insert(pTaskInfo);
      serverTask.taskAttached(this);
      return TRUE;
   }
   else
   {
      // we are already managing it
      delete pKey;
      pKey = NULL;
   }
<commit_msg>Applied patch from Saab: - Fixed memory leak: UtlInt was "newed" (UtlInt* pKey = new UtlInt(serverTask.getTaskId());) but never deleted if successful.<commit_after>//
// Copyright (C) 2004-2006 SIPfoundry Inc.
// Licensed by SIPfoundry under the LGPL license.
//
// Copyright (C) 2004-2006 Pingtel Corp.  All rights reserved.
// Licensed to SIPfoundry under a Contributor Agreement.
//
// Copyright (C) 2007 <NAME>
// Licensed under the LGPL license.
// $$
///////////////////////////////////////////////////////////////////////////////

// SYSTEM INCLUDES
// APPLICATION INCLUDES
#include <os/OsSharedServerTaskMgr.h>
#include <os/OsSharedServerTask.h>
#include <os/OsSharedTaskInfo.h>
#include <os/OsLock.h>
#include <os/OsPtrLock.h>
#include <os/OsSharedServerTaskMsg.h>
#include <os/OsSharedServerTaskWorker.h>
#include <utl/UtlInt.h>
#include <utl/UtlHashBagIterator.h>
#include <utl/UtlSListIterator.h>

// DEFINES
// EXTERNAL FUNCTIONS
// EXTERNAL VARIABLES
// CONSTANTS
// STATIC VARIABLE INITIALIZATIONS
// MACROS
// GLOBAL VARIABLES
// GLOBAL FUNCTIONS

/* //////////////////////////// PUBLIC //////////////////////////////////// */

/* ============================ CREATORS ================================== */

OsSharedServerTaskMgr::OsSharedServerTaskMgr(unsigned int iNumberOfThreads /*= SHARED_TASK_DEFAULT_THREADS*/,
                                             const int maxRequestQMsgs)
: m_msgQ(maxRequestQMsgs, OsMsgQ::DEF_MAX_MSG_LEN, OsMsgQ::Q_PRIORITY)
, m_state(SHARED_TASK_MGR_UNINITIALIZED)
{
   unsigned int realNumberOfThreads = iNumberOfThreads > 0 ? iNumberOfThreads : 1;

   for (unsigned int i = 0; i < realNumberOfThreads; i++)
   {
      OsSharedServerTaskWorker* pWorker = new OsSharedServerTaskWorker("OsSharedServerTaskMgr-%d", i, NULL, &m_msgQ);
      m_threadList.insert(pWorker);
      // do not start workers
   }
}

OsSharedServerTaskMgr::~OsSharedServerTaskMgr()
{
   shutdownAllWorkers(); // stop all threads
   releaseAllSharedTasks(); // unmanage all shared tasks
   flushMessages(); // delete all messages from queue
}

/* ============================ MANIPULATORS ============================== */

void OsSharedServerTaskMgr::start()
{
   OsLock lock(m_memberMutex);
   UtlSListIterator itor(m_threadList);
   while (itor())
   {
      OsSharedServerTaskWorker* pWorker = dynamic_cast<OsSharedServerTaskWorker*>(itor.item());
      if (pWorker)
      {
         pWorker->start();
      }
   }
   m_state = SHARED_TASK_MGR_STARTED;
}

void OsSharedServerTaskMgr::shutdown()
{
   shutdownAllWorkers();
}

UtlBoolean OsSharedServerTaskMgr::manage(OsSharedServerTask& serverTask)
{
   if (serverTask.isManaged())
   {
      // we cannot manage OsSharedServerTask if its already managed
      return FALSE;
   }

   UtlInt key(serverTask.getTaskId());
   OsLock lock(m_memberMutex);
   // first try to find if we are already managing it
   OsSharedTaskInfo* pTaskInfo = dynamic_cast<OsSharedTaskInfo*>(m_managedTaskInfo.find(&key));
   if (!pTaskInfo)
   {
      // add it
      pTaskInfo = new OsSharedTaskInfo(serverTask.getTaskId(), &serverTask);
      m_managedTaskInfo.insert(pTaskInfo);
      serverTask.taskAttached(this);
      return TRUE;
   }

{'fraction_non_alphanumeric': 0.18561557788944724, 'fraction_numerical': 0.007223618090452261, 'mean_word_length': 3.1410923276983094, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6021057', 'n_tokens_mistral': 2065, 'n_tokens_neox': 2002, 'n_words': 422}