The DejaVU Framework -- hush 3.0
[.] Papers Tutorials Examples Manuals Interfaces Sources Packages Resources ?

source: thread.c hush-3.0b4/auxiliary/net/thread


[.] - [up] [top] - index README make include source scripts
  // simple thread package, based on solaris threads
  
  // September 1995
  // Bastiaan Sch\"onhage
  
  include <hush/source.h>
  include <hush/handler.h>
  
  include <net/thread/thread.h>
  include <hush/flags.h>
  
  //#include <iostream.h>
  
  ifdef THREADS
  include <thread.h>
  include <signal.h>
  endif
  
  include <string.h>
  
  const char EOS = '\0'; 
  
  void* thread::act = 0;
  
  ifdef THREADS
  static void*         spawn_thread(void* x);
  static void         exit_handler();
  endif
  
  inline static void        skipspaces(char* &ptr);
  
  void skipspaces(char* &ptr)
  {
          while(*ptr==' ') ptr++;
  }
  
  ifndef THREADS
  thread::thread( char * ) : t(0) {
  //cerr << "ERROR: no threads supported" << endl;
  }
  else
  thread::thread( char *options ) : t(0)
  {
          if(!act)
          {
                  act = new struct sigaction;
                  struct sigaction* a = (struct sigaction*) act;
                  a->sa_handler = exit_handler;
                  sigemptyset(&(a->sa_mask));
                  sigaction(SIGTERM, a, NULL);
          }
          opt = THR_NEW_LWP;
          if ( strlen(options)> 0 )
          {
                  while( *options != EOS ) 
                  {
                          skipspaces(options);
                          if ( strcmp(options, "bound")==0 ) {
                                  opt |= THR_BOUND;
                                  options+=5;
                          } else if ( strcmp(options, "detached")==0 ) {
                                  opt |= THR_DETACHED;
                                  options+=8;
                          } else if ( strcmp(options, "daemon")==0 ) {
                                  opt |= THR_DAEMON;
                                  options+=6;
                          }
                  }                
          }
  }
  endif
  
  int thread::main()
  {
      return 0;
  }
  
  int thread::run() 
  {
  ifdef THREADS
      if ( thr_create(NULL, 0, spawn_thread, this, opt, &t) ) 
      {
          error("start/create"); 
          return 0;
      }
  else
     //cerr << "ERROR: no threads supported" << endl;
  endif
      return t;
  }
  
  void thread::kill()
  {
  ifdef THREADS
          thr_kill(t, SIGTERM);
  else
     //cerr << "ERROR: no threads supported" << endl;
  endif
  }
  
  void thread::suspend()
  {
  ifdef THREADS
          thr_suspend(t);
  else
      //cerr << "ERROR: no threads supported" << endl;
  endif
  }
  
  void thread::resume()
  {
  ifdef THREADS
          thr_continue(t);
  else
     //cerr << "ERROR: no threads supported" << endl;
  endif
  }
  
  ifndef THREADS
  void thread::priority(int) {
     //cerr << "ERROR: no threads supported" << endl;
  }
  else
  void thread::priority(int pri)
  {
          thr_setprio(t, pri);
  }
  endif
  
  int thread::priority()
  {
  ifdef THREADS
          int pri;
  
          if( thr_getprio(t, &pri) ) return pri;
          else return 0;
  else
          //cerr << "ERROR: no threads supported" << endl;
          return 0;
  endif
  }
  
  unsigned thread::id()
  {
          return t;
  }
  
  void thread::error(char* s)
  {        
      //cerr << "Thread error: " << s << endl;
  }
  
  ifndef THREADS
  unsigned thread::join(unsigned , void* ) {
     //cerr << "ERROR: no threads supported" << endl;
     return 0;
  }
  else
  unsigned thread::join(unsigned waitfor, void* status) 
  {
      thread_t res;
  
      int result = thr_join( waitfor, &res, &status);
      if (result) //cerr << "Could not join" << endl;
      return result?0:res;
  }
  endif
  
  void thread::yield()
  {
  ifdef THREADS
          thr_yield();
  endif
  }
  
  unsigned thread::self()
  {
  ifdef THREADS
          return thr_self();
  else
          //cerr << "ERROR: no threads supported" << endl;
          return 0;
  endif
  }
  
  ifndef THREADS
  void thread::exit(int ) {
          //cerr << "ERROR: no threads supported" << endl;
          }
  else
  void thread::exit(int status)
  {
          thr_exit((void*) status);
  }
  endif
  
  ifndef THREADS
  void thread::setconcurrency(int ) {
          //cerr << "ERROR: no threads supported" << endl;
          }
  else
  void thread::setconcurrency(int n)
  {
          thr_setconcurrency(n);
  }
  endif
  
  ifdef THREADS
  static void* spawn_thread(void* x) 
  {
      thread* h = (thread*) x;
      return (void*) h->main();
  }
  endif
  
  void exit_handler()
  {
  ifdef THREADS
          thread_t t;
  
          t = thr_self();
          //cerr << "ExitHandler called for tid:" << t << endl;
          thr_exit(0);
  else
          //cerr << "ERROR: no threads supported" << endl;
  endif
  }
  

[.] Papers Tutorials Examples Manuals Interfaces Sources Packages Resources ?
Hush Online Technology
hush@cs.vu.nl
09/09/98