<commit_before><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  uses(
    'lang.IllegalThreadStateException',
    'lang.SystemException'
  );

  /**
   * Thread
   *
   * <code>
   *   uses('lang.Thread');
   *   
   *   class TimerThread extends Thread {
   *     var
   *       $ticks    = 0,
   *       $timeout  = 0;
   *       
   *     function __construct($timeout) {
   *       $this->timeout= $timeout;
   *       parent::__construct('timer.'.$timeout);
   *     }
   *       
   *     function run() {
   *       while ($this->ticks < $this->timeout) {
   *         Thread::sleep(1000);
   *         $this->ticks++;
   *         printf("<%s> tick\n", $this->name);
   *       }
   *       printf("<%s> time's up!\n", $this->name);
   *     }
   *   }
   *   
   *   $t[0]= new TimerThread(5);
   *   $t[0]->start();
   *   $t[1]= new TimerThread(2);
   *   $t[1]->start();
   *   var_dump($t);
   *   for ($i= 0; $i < 3; $i++) {
   *     echo "<main> Waiting...\n";
   *     sleep(1);
   *   }
   *   var_dump($t[0]->join(), $t[1]->join());
   * </code>
   *
   * @ext      pcntl
   * @ext      posix
   * @platform Unix
   * @purpose  Base class
   */
  abstract class Thread extends Object {
    public
      $name     = '',
      $running  = FALSE;
      
    protected
      $_id      = -1,
      $_pid     = -1;
      
    /**
     * Constructor
     *
     * @access  public
     * @param   string name
     */
    public function __construct($name= '') {
      $this->name= $name;
      
    }
    
    /**
     * Returns whether this thread is running
     *
     * @access  public
     * @return  bool
     */
    public function isRunning() {
      return $this->running;
    }
    
    /**
     * Set Name
     *
     * @access  public
     * @param   string name
     */
    public function setName($name) {
      $this->name= $name;
    }

    /**
     * Get Name
     *
     * @access  public
     * @return  string
     */
    public function getName() {
      return $this->name;
    }
    
    /**
     * Causes the currently executing thread to sleep (temporarily cease 
     * execution) for the specified number of milliseconds. 
     *
     * @model   static
     * @access  public
     * @param   int millis
     */
    public static function sleep($millis) {
      usleep($millis * 1000);
    }

    /**
     * Starts thread execution
     *
     * @access  public
     * @throws  lang.IllegalThreadStateException if this thread is already running
     * @throws  lang.SystemException if the thread cannot be started
     */
    public function start() {
      if (self::isRunning()) {
        throw (new IllegalThreadStateException('Already running'));
      }

      $parent= getmypid();
      $pid= pcntl_fork();
      if (-1 == $pid) {     // Cannot fork
        throw (new SystemException('Cannot fork'));
      } elseif ($pid) {     // Parent
        $this->running= TRUE;
        $this->_id= $pid;
        $this->_pid= $parent;
      } else {              // Child
        $this->running= TRUE;
        $this->_id= getmypid();
        $this->_pid= $parent;
        self::run();
        exit();
      }
    }
    
    /**
     * Join this thread. The optional parameter wait may be set to FALSE to
     * return immediately if this thread hasn't terminated yet.
     *
     * @access  public<commit_msg>- Fix "Fatal error: Cannot call abstract method Thread::run()"<commit_after><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  uses(
    'lang.IllegalThreadStateException',
    'lang.SystemException'
  );

  /**
   * Thread
   *
   * <code>
   *   uses('lang.Thread');
   *   
   *   class TimerThread extends Thread {
   *     var
   *       $ticks    = 0,
   *       $timeout  = 0;
   *       
   *     function __construct($timeout) {
   *       $this->timeout= $timeout;
   *       parent::__construct('timer.'.$timeout);
   *     }
   *       
   *     function run() {
   *       while ($this->ticks < $this->timeout) {
   *         Thread::sleep(1000);
   *         $this->ticks++;
   *         printf("<%s> tick\n", $this->name);
   *       }
   *       printf("<%s> time's up!\n", $this->name);
   *     }
   *   }
   *   
   *   $t[0]= new TimerThread(5);
   *   $t[0]->start();
   *   $t[1]= new TimerThread(2);
   *   $t[1]->start();
   *   var_dump($t);
   *   for ($i= 0; $i < 3; $i++) {
   *     echo "<main> Waiting...\n";
   *     sleep(1);
   *   }
   *   var_dump($t[0]->join(), $t[1]->join());
   * </code>
   *
   * @ext      pcntl
   * @ext      posix
   * @platform Unix
   * @purpose  Base class
   */
  abstract class Thread extends Object {
    public
      $name     = '',
      $running  = FALSE;
      
    protected
      $_id      = -1,
      $_pid     = -1;
      
    /**
     * Constructor
     *
     * @access  public
     * @param   string name
     */
    public function __construct($name= '') {
      $this->name= $name;
      
    }
    
    /**
     * Returns whether this thread is running
     *
     * @access  public
     * @return  bool
     */
    public function isRunning() {
      return $this->running;
    }
    
    /**
     * Set Name
     *
     * @access  public
     * @param   string name
     */
    public function setName($name) {
      $this->name= $name;
    }

    /**
     * Get Name
     *
     * @access  public
     * @return  string
     */
    public function getName() {
      return $this->name;
    }
    
    /**
     * Causes the currently executing thread to sleep (temporarily cease 
     * execution) for the specified number of milliseconds. 
     *
     * @model   static
     * @access  public
     * @param   int millis
     */
    public static function sleep($millis) {
      usleep($millis * 1000);
    }

    /**
     * Starts thread execution
     *
     * @access  public
     * @throws  lang.IllegalThreadStateException if this thread is already running
     * @throws  lang.SystemException if the thread cannot be started
     */
    public function start() {
      if (self::isRunning()) {
        throw (new IllegalThreadStateException('Already running'));
      }

      $parent= getmypid();
      $pid= pcntl_fork();
      if (-1 == $pid) {     // Cannot fork
        throw (new SystemException('Cannot fork'));
      } elseif ($pid) {     // Parent
        $this->running= TRUE;
        $this->_id= $pid;
        $this->_pid= $parent;
      } else {              // Child
        $this->running= TRUE;
        $this->_id= getmypid();
        $this->_pid= $parent;
        $this->run();
        exit();
      }
    }
    
    /**
     * Join this thread. The optional parameter wait may be set to FALSE to
     * return immediately if this thread hasn't terminated yet.
     *
     * @access  public
{'fraction_non_alphanumeric': 0.14874446085672083, 'fraction_numerical': 0.0070901033973412115, 'mean_word_length': 1.5892925430210325, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 54, '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': '6122840', 'n_tokens_mistral': 2220, 'n_tokens_neox': 2145, 'n_words': 516}