<commit_before>/*
 * Optimized Ruby Mutex implementation, loosely based on thread.rb by
 * <NAME> <<EMAIL>>
 *
 *  Copyright 2006  MenTaLguY <<EMAIL>>
 *
 * This file is made available under the same terms as Ruby.
 */

#include <ruby.h>
#include <intern.h>
#include <rubysig.h>

static VALUE rb_cMutex;
static VALUE rb_cConditionVariable;
static VALUE rb_eThreadError;
static VALUE rb_cQueue;
static VALUE rb_cSizedQueue;

static VALUE
return_value(value)
  VALUE value;
{
  return value;
}

typedef struct _Entry {
  VALUE value;
  struct _Entry *next;
} Entry;

typedef struct _List {
  Entry *entries;
  Entry *last_entry;
  Entry *entry_pool;
  unsigned long size;
} List;

static void
init_list(list)
  List *list;
{
  list->entries = NULL;
  list->last_entry = NULL;
  list->entry_pool = NULL;
  list->size = 0;
}

static void
mark_list(list)
  List *list;
{
  Entry *entry;
  for ( entry = list->entries ; entry ; entry = entry->next ) {
    rb_gc_mark(entry->value);
  }
}

static void
free_entries(first)
  Entry *first;
{
  Entry *next;
  while (first) {
    next = first->next;
    free(first);
    first = next;
  }
}

static void
finalize_list(list)
  List *list;
{
  free_entries(list->entries);
  free_entries(list->entry_pool);
}

static void
push_list(list, value)
  List *list;
  VALUE value;
{
  Entry *entry;

  if (list->entry_pool) {
    entry = list->entry_pool;
    list->entry_pool = entry->next;
  } else {
    entry = (Entry *)malloc(sizeof(Entry));
  }

  entry->value = value;
  entry->next = NULL;

  if (list->last_entry) {
    list->last_entry->next = entry;
  } else {
    list->entries = entry;
  }
  list->last_entry = entry;

  ++list->size;
}

static VALUE
shift_list(list)
  List *list;
{
  Entry *entry;

  entry = list->entries;
  if (!entry) return Qundef;

  list->entries = entry->next;
  if ( entry == list->last_entry ) {
    list->last_entry = NULL;
  }
  entry->next = list->entry_pool;
  list->entry_pool = entry;

  --list->size;

  return entry->value;
}

static void
clear_list(list)
  List *list;
{
  if (list->last_entry) {
    list->last_entry->next = list->entry_pool;
    list->entry_pool = list->entries;
    list->entries = NULL;
    list->last_entry = NULL;
    list->size = 0;
  }
}

static VALUE
wake_one(list)
  List *list;<commit_msg>make use of memory pools configurable<commit_after>/*
 * Optimized Ruby Mutex implementation, loosely based on thread.rb by
 * <NAME> <<EMAIL>>
 *
 *  Copyright 2006  MenTaLguY <<EMAIL>>
 *
 * This file is made available under the same terms as Ruby.
 */

#include <ruby.h>
#include <intern.h>
#include <rubysig.h>

#ifndef USE_MEM_POOLS
#define USE_MEM_POOLS 1
#endif

static VALUE rb_cMutex;
static VALUE rb_cConditionVariable;
static VALUE rb_eThreadError;
static VALUE rb_cQueue;
static VALUE rb_cSizedQueue;

static VALUE
return_value(value)
  VALUE value;
{
  return value;
}

typedef struct _Entry {
  VALUE value;
  struct _Entry *next;
} Entry;

typedef struct _List {
  Entry *entries;
  Entry *last_entry;
  Entry *entry_pool;
  unsigned long size;
} List;

static void
init_list(list)
  List *list;
{
  list->entries = NULL;
  list->last_entry = NULL;
  list->entry_pool = NULL;
  list->size = 0;
}

static void
mark_list(list)
  List *list;
{
  Entry *entry;
  for ( entry = list->entries ; entry ; entry = entry->next ) {
    rb_gc_mark(entry->value);
  }
}

static void
free_entries(first)
  Entry *first;
{
  Entry *next;
  while (first) {
    next = first->next;
    free(first);
    first = next;
  }
}

static void
finalize_list(list)
  List *list;
{
  free_entries(list->entries);
  free_entries(list->entry_pool);
}

static void
push_list(list, value)
  List *list;
  VALUE value;
{
  Entry *entry;

  if (list->entry_pool) {
    entry = list->entry_pool;
    list->entry_pool = entry->next;
  } else {
    entry = (Entry *)malloc(sizeof(Entry));
  }

  entry->value = value;
  entry->next = NULL;

  if (list->last_entry) {
    list->last_entry->next = entry;
  } else {
    list->entries = entry;
  }
  list->last_entry = entry;

  ++list->size;
}

static VALUE
shift_list(list)
  List *list;
{
  Entry *entry;
  VALUE value;

  entry = list->entries;
  if (!entry) return Qundef;

  list->entries = entry->next;
  if ( entry == list->last_entry ) {
    list->last_entry = NULL;
  }

  --list->size;

  value = entry->value;
  if (USE_MEM_POOLS) {
    entry->next = list->entry_pool;
    list->entry_pool = entry;
  } else {
    free(entry);
  }

  return value;
}

static void
clear_list(list)
  List *list;
{
  if (list->last_entry) {
    if (USE_MEM_POOLS) {
      list->last_entry->next = list->entry_pool;
      list->entry_pool = list->entries;
    } else {
      free_entries(list->entries);
    }
    list->entries = NULL;
    list->last_entry = NULL;
    list->size = 0;
  }
}

static VALUE
wake_one(list)
  List *list;
{'fraction_non_alphanumeric': 0.13528562590224788, 'fraction_numerical': 0.002680965147453083, 'mean_word_length': 3.2920353982300883, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 102, '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': '1782067', 'n_tokens_mistral': 1833, 'n_tokens_neox': 1781, 'n_words': 438}