<commit_before>/**
 * rr_impl.c
 *
 * Implementation of functionality specific to round-robin scheduling.
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>

#include "rr_impl.h"
#include "list.h"
#include "sched_impl.h"

void rr_wait(sched_queue_t *queue) {
  list_elem_t *queue_elem;
  thread_info_t *new_head_info;
  if(!pthread_mutex_lock(queue->access_mutex)) {
    /* Current worker is moved to the back of the line */
    queue_elem = list_get_head(queue->list);

    /* If the queue is empty, the head elem will be NULL */
    if(queue_elem) {
      list_remove_elem(queue->list, queue_elem);
      list_insert_tail(queue->list, queue_elem);

      /* re-use queue_elem for the new head */

      queue_elem = list_get_head(queue->list);

      /* If the queue is empty, the head elem will be NULL */
      if(queue_elem) {
        new_head_info = (thread_info_t *) queue_elem->datum;

        if(pthread_mutex_unlock(queue->access_mutex)) {
          /* Handle mutex unlock failure */
          perror("round robin wait queue mutex unlock");
        }

        if(pthread_mutex_lock(new_head_info->yield_cpu)) {
          /* Handle mutex lock failure */
          perror("round robin wait cpu yield mutex lock");
        }
      } else {
        /* Where did it go in the last milliseconds?!? (Shouldn't happen) */
        perror("round robin wait has worker");
      }

    } else {
      /* Uh, where'd our worker go? */
      perror("round robin wait has worker");
    }
  } else {
    /* Handle mutex lock failure */
    perror("round robin wait queue mutex lock");
  }
}
<commit_msg>Changed order of operations in rr_wait<commit_after>/**
 * rr_impl.c
 *
 * Implementation of functionality specific to round-robin scheduling.
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>

#include "rr_impl.h"
#include "list.h"
#include "sched_impl.h"

void rr_wait(sched_queue_t *queue) {
  list_elem_t *head;
  thread_info_t *worker;

  /* Get head of queue */
  if(!pthread_mutex_lock(queue->access_mutex)) {
    head = list_get_head(queue->list);
    if(head) {
      worker = (thread_info_t*) head->datum;
      list_remove_elem(queue->list, head);
      list_insert_tail(queue->list, head);
    } else {
      /* Queue is empty. Just leave then. */
      pthread_mutex_unlock(queue->access_mutex);
      return;
    }
    pthread_mutex_unlock(queue->access_mutex);
  } else {
    /* Handle queue access lock failure */
  }

  /* Block until worker has finished */
  if(!pthread_mutex_lock(worker->yield_cpu)) {
    /* Error handling */
  }
}

{'fraction_non_alphanumeric': 0.12571646923958732, 'fraction_numerical': 0.0, 'mean_word_length': 2.9427710843373496, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 27, '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': '6011477', 'n_tokens_mistral': 889, 'n_tokens_neox': 865, 'n_words': 217}