<commit_before>/******************************************************************
 *
 * Round for C
 *
 * Copyright (C) <NAME> 2015
 *
 * This is licensed under BSD-style license, see file COPYING.
 *
 ******************************************************************/

#include <round/message.h>

/****************************************
* round_message_manager_new
****************************************/

RoundMessageManager *round_message_manager_new(void)
{
  RoundMessageManager *mgr;

  mgr = (RoundMessageManager *)malloc(sizeof(RoundMessageManager));

  if (!mgr)
    return NULL;

  mgr->queue = round_queue_new();
  mgr->sem = round_semaphore_new(0);
  mgr->mutex = round_mutex_new();
  
  return mgr;
}

/****************************************
* round_message_manager_delete
****************************************/

bool round_message_manager_delete(RoundMessageManager *mgr)
{
  if (!mgr)
    return false;
  
  round_message_manager_clear(mgr);

  round_queue_delete(mgr->queue);
  round_semaphore_delete(mgr->sem);
  round_mutex_delete(mgr->mutex);
  
  free(mgr);
  
  return true;
}

/****************************************
 * round_message_manager_delete
 ****************************************/

bool round_message_manager_clear(RoundMessageManager *mgr)
{
  if (!mgr)
    return false;

  round_semaphore_reset(mgr->sem);

  return true;
}

bool round_message_manager_pushmessage(RoundMessageManager *mgr, RoundMessage *msg)
{
  if (!mgr)
    return false;
  
  round_mutex_lock(mgr->mutex);
  round_queue_push(mgr->queue, (RoundQueueObject *)msg);
  round_mutex_unlock(mgr->mutex);

  round_semaphore_post(mgr->sem);
  
  return true;
  
}

bool round_message_manager_waitmessage(RoundMessageManager *mgr, RoundMessage **msg)
{
  bool isSuccess;
  
  if (!mgr)
    return false;
  
  *msg = NULL;
  if  (!round_semaphore_wait(mgr->sem))
    return false;
  
  round_mutex_lock(mgr->mutex);
  isSuccess = round_queue_pop(mgr->queue, (RoundQueueObject **)msg);
  round_mutex_unlock(mgr->mutex);

  return isSuccess;
}<commit_msg>* Updated round_message_manager_clear() to clear the message queue.<commit_after>/******************************************************************
 *
 * Round for C
 *
 * Copyright (C) <NAME> 2015
 *
 * This is licensed under BSD-style license, see file COPYING.
 *
 ******************************************************************/

#include <round/message.h>

/****************************************
* round_message_manager_new
****************************************/

RoundMessageManager *round_message_manager_new(void)
{
  RoundMessageManager *mgr;

  mgr = (RoundMessageManager *)malloc(sizeof(RoundMessageManager));

  if (!mgr)
    return NULL;

  mgr->queue = round_queue_new();
  mgr->sem = round_semaphore_new(0);
  mgr->mutex = round_mutex_new();
  
  return mgr;
}

/****************************************
* round_message_manager_delete
****************************************/

bool round_message_manager_delete(RoundMessageManager *mgr)
{
  if (!mgr)
    return false;
  
  round_message_manager_clear(mgr);

  round_queue_delete(mgr->queue);
  round_semaphore_delete(mgr->sem);
  round_mutex_delete(mgr->mutex);
  
  free(mgr);
  
  return true;
}

/****************************************
 * round_message_manager_clear
 ****************************************/

bool round_message_manager_clear(RoundMessageManager *mgr)
{
  RoundMessage *msg;
  
  if (!mgr)
    return false;

  // TODO : Serialize none executed message
  while (round_queue_pop(mgr->queue, (RoundQueueObject **)&msg)) {
    if (!msg)
      continue;
    round_message_delete(msg);
  }

  round_semaphore_reset(mgr->sem);

  return true;
}

/****************************************
 * round_message_manager_pushmessage
 ****************************************/

bool round_message_manager_pushmessage(RoundMessageManager *mgr, RoundMessage *msg)
{
  if (!mgr)
    return false;
  
  round_mutex_lock(mgr->mutex);
  round_queue_push(mgr->queue, (RoundQueueObject *)msg);
  round_mutex_unlock(mgr->mutex);

  round_semaphore_post(mgr->sem);
  
  return true;
  
}

/****************************************
 * round_message_manager_waitmessage
 ****************************************/

bool round_message_manager_waitmessage(RoundMessageManager *mgr, RoundMessage **msg)
{
  bool isSuccess;
  
  if (!mgr)
    return false;
  
  *msg = NULL;
  if  (!round_semaphore_wait(mgr->sem))
    return false;
  
  round_mutex_lock(mgr->mutex);
  isSuccess = round_queue_pop(mgr->queue, (RoundQueueObject **)msg);
  round_mutex_unlock(mgr->mutex);

  return isSuccess;
}
{'fraction_non_alphanumeric': 0.2899177845088706, 'fraction_numerical': 0.0021635655560363477, 'mean_word_length': 5.651798561151079, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3073480', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1409, 'n_words': 238}