<commit_before>

// Copyright 2010-2012 University of Washington. All Rights Reserved.
// LICENSE_PLACEHOLDER
// This software was created with Government support under DE
// AC05-76RL01830 awarded by the United States Department of
// Energy. The Government has certain rights in the software.


#ifndef __FULLEMPTY_HPP__
#define __FULLEMPTY_HPP__

#include <gflags/gflags.h>
#include <glog/logging.h>

#include "ConditionVariableLocal.hpp"

namespace Grappa {
  
  /// @addtogroup Synchronization
  /// @{
  
  /// Wrapper class to provide full-bit semantics for arbitrary data.
  template< typename T >
  class FullEmpty {
  private:
    enum class State : bool { EMPTY = false, FULL = true };

  public:
    union {
      struct {
        State state_ : 1;
        intptr_t waiters_ : 63; // can't include pointers in bitfield, so use intptr_t
      };
      intptr_t raw_; // unnecessary; just to ensure alignment
    };
    
  private:
    T t_;
    
    void block_until( State desired_state ) {
      while( state_ != desired_state ) {
        DVLOG(5) << "In " << __PRETTY_FUNCTION__ 
                 << ", blocking until " << (desired_state == State::FULL ? "full" : "empty");
	Grappa::wait( this );
      }
    }
    
  public:
    
    FullEmpty( ) : state_( State::EMPTY ), waiters_( 0 ), t_() {}

    FullEmpty( T t ) : state_( State::FULL ), waiters_( 0 ), t_(t) {}

    inline bool full() const { return state_ == State::FULL; }
    inline bool empty() const { return state_ == State::EMPTY; }
    
    inline void reset() {
      CHECK(waiters_ == 0);
      state_ = State::EMPTY;
    }
    
    T writeXF( T t ) {
      t_ = t; 
      state_ = State::FULL; 
      Grappa::broadcast( this );
      return t_; 
    }
      
    T writeEF( T t ) { 
      block_until(State::EMPTY); 
      return writeXF( t );
    }

    T writeFF( T t ) { 
      block_until(State::FULL); 
      return writeXF( t );
    }


    T readXX() {
      return t_; 
    }
      
    T readFF() { 
      block_until(State::FULL); 
      return readXX();
    }

    T readFE() { 
      block_until(State::FULL); 
      state_ = State::EMPTY;
      Grappa::broadcast( this );
      return readXX();
    }

  };
  /// @}

}

#endif
<commit_msg>Rearrange synchronization in condition variable. I think this is kinder to the scheduler.<commit_after>

// Copyright 2010-2012 University of Washington. All Rights Reserved.
// LICENSE_PLACEHOLDER
// This software was created with Government support under DE
// AC05-76RL01830 awarded by the United States Department of
// Energy. The Government has certain rights in the software.


#ifndef __FULLEMPTY_HPP__
#define __FULLEMPTY_HPP__

#include <gflags/gflags.h>
#include <glog/logging.h>

#include "ConditionVariableLocal.hpp"

namespace Grappa {
  
  /// @addtogroup Synchronization
  /// @{
  
  /// Wrapper class to provide full-bit semantics for arbitrary data.
  template< typename T >
  class FullEmpty {
  private:
    enum class State : bool { EMPTY = false, FULL = true };

  public:
    union {
      struct {
        State state_ : 1;
        intptr_t waiters_ : 63; // can't include pointers in bitfield, so use intptr_t
      };
      intptr_t raw_; // unnecessary; just to ensure alignment
    };
    
  private:
    T t_;
    
    void block_until( State desired_state ) {
      while( state_ != desired_state ) {
        DVLOG(5) << "In " << __PRETTY_FUNCTION__ 
                 << ", blocking until " << (desired_state == State::FULL ? "full" : "empty");
	Grappa::wait( this );
      }
    }
    
  public:
    
    FullEmpty( ) : state_( State::EMPTY ), waiters_( 0 ), t_() {}

    FullEmpty( T t ) : state_( State::FULL ), waiters_( 0 ), t_(t) {}

    inline bool full() const { return state_ == State::FULL; }
    inline bool empty() const { return state_ == State::EMPTY; }
    
    inline void reset() {
      CHECK(waiters_ == 0);
      state_ = State::EMPTY;
    }
    
    T writeXF( T t ) {
      t_ = t; 
      state_ = State::FULL; 
      //Grappa::broadcast( this );
      Grappa::signal( this );
      return t_; 
    }
      
    T writeEF( T t ) { 
      block_until(State::EMPTY); 
      T tt = writeXF( t );
      Grappa::signal( this );
      return tt;
    }

    T writeFF( T t ) { 
      block_until(State::FULL); 
      T tt = writeXF( t );
      Grappa::signal( this );
      return tt;
    }


    T readXX() {
      return t_; 
    }
      
    T readFF() { 
      block_until(State::FULL); 
      T tt = readXX();
      Grappa::signal( this );
      return tt;
    }

    T readFE() { 
      block_until(State::FULL); 
      state_ = State::EMPTY;
      Grappa::broadcast( this );
      return readXX();
    }

  };
  /// @}

}

#endif

{'fraction_non_alphanumeric': 0.12253550985796056, 'fraction_numerical': 0.010175959296162815, 'mean_word_length': 2.1813890761968984, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 9, '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': '1030856', 'n_tokens_mistral': 1597, 'n_tokens_neox': 1506, 'n_words': 431}