Fresco/Prague/src/Sys Signal.cc,1.9,1.10 Thread.cc,1.13,1.14 Timer.cc,1.10,1.11

Stefan Seefeld <[email protected]> Wed, 07 May 2003 22:21:14 -0500
Newsgroups gmane.comp.video.fresco.cvs
Message-ID <[email protected]>
Update of /cvs/fresco/Fresco/Prague/src/Sys
In directory purcel:/tmp/cvs-serv22129/src/Sys

Modified Files:
	Signal.cc Thread.cc Timer.cc 
Log Message:
some API clarifications for Threads, and minor updates to enhance coding standard conformity

Index: Signal.cc
===================================================================
RCS file: /cvs/fresco/Fresco/Prague/src/Sys/Signal.cc,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- Signal.cc	18 Oct 2001 14:37:20 -0000	1.9
+++ Signal.cc	8 May 2003 03:21:11 -0000	1.10
@@ -1,8 +1,8 @@
 /*$Id$
  *
- * This source file is a part of the Berlin Project.
- * Copyright (C) 1999 Stefan Seefeld <[email protected]> 
- * http://www.berlin-consortium.org
+ * This source file is a part of the Fresco Project.
+ * Copyright (C) 1999 Stefan Seefeld <[email protected]> 
+ * http://www.fresco.org
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -53,7 +53,7 @@
 
 bool Signal::set (int signum, Signal::Notifier *notifier)
 {
-  if (server.state() != Thread::running) server.start();
+  if (server.state() == Thread::READY) server.start();
   if (!notifiers[signum].size())
     {
       struct sigaction sa;

Index: Thread.cc
===================================================================
RCS file: /cvs/fresco/Fresco/Prague/src/Sys/Thread.cc,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- Thread.cc	6 Feb 2001 19:46:17 -0000	1.13
+++ Thread.cc	8 May 2003 03:21:11 -0000	1.14
@@ -1,8 +1,8 @@
 /*$Id$
  *
- * This source file is a part of the Berlin Project.
- * Copyright (C) 1999 Stefan Seefeld <[email protected]> 
- * http://www.berlin-consortium.org
+ * This source file is a part of the Fresco Project.
+ * Copyright (C) 1999 Stefan Seefeld <[email protected]> 
+ * http://www.fresco.org
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -24,6 +24,7 @@
 
 using namespace Prague;
 
+const void   *Thread::CANCELED = PTHREAD_CANCELED;
 pthread_key_t Thread::self_key;
 pthread_key_t Thread::id_key;
 Mutex         Thread::id_mutex;
@@ -55,73 +56,82 @@
 
 Thread::Guard::~Guard()
 {
-  Thread::main->_state = canceled;
   delete Thread::main;
 }
 
 Thread::Thread(proc pp, void *a, priority_t prio)
-  : p(pp), arg(a), _priority(prio), _state(ready), detached(false)
+  : my_proc(pp),
+    my_arg(a),
+    my_priority(prio),
+    my_state(READY),
+    my_detached(false)
+{
+}
+
+Thread::Thread(pthread_t pt)
+  : my_proc(0),
+    my_arg(0),
+    my_thread(pt),
+    my_priority(NORMAL),
+    my_state(RUNNING),
+    my_detached(false)
 {
 }
 
 Thread::~Thread()
 {
-  if (this != self())
-    {
-      cancel();
-      if (!detached) join(0);
-    }
+  if (this != self() && state() != READY)
+  {
+    cancel();
+    if (!my_detached) join(0);
+  }
 }
 
 void Thread::start() throw (Exception)
 {
-  Prague::Guard<Mutex> guard(mutex);
-  if ((_state) != ready) throw Exception("thread already running");
-  if (pthread_create(&thread, 0, &start, this) != 0) throw Exception("can't create thread");
-  else _state = running;
+  Prague::Guard<Mutex> guard(my_mutex);
+  if ((my_state) != READY) throw Exception("thread already running");
+  if (pthread_create(&my_thread, 0, &start, this) != 0) throw Exception("can't create thread");
+  else my_state = RUNNING;
 }
 
 void Thread::join(void **status) throw (Exception)
 {
   {
-    Prague::Guard<Mutex> guard(mutex);
-    if (_state == joined || _state == canceled || _state == ready) return;
+    Prague::Guard<Mutex> guard(my_mutex);
+    if (my_state == READY) throw Exception("can't join ready thread");
     if (this == self()) throw Exception("can't join thread 'self'");
-    if (detached) throw Exception("can't join detached thread");
-    _state = joined;
+    if (my_detached) throw Exception("can't join detached thread");
   }
   void *s;
-  pthread_join(thread, &s);
-  if (s == PTHREAD_CANCELED)
-    {
-      Prague::Guard<Mutex> guard(mutex);
-      _state = canceled;
-    }
+  pthread_join(my_thread, &s);
+  Prague::Guard<Mutex> guard(my_mutex);
+  my_state = READY;
   if (status) *status = s;
 }
 
 void Thread::cancel()
 {
-  if (this != self() && state() == running)
-    pthread_cancel(thread);
+  if (this != self() && state() == RUNNING)
+    pthread_cancel(my_thread);
 }
 
 void Thread::detach()
 {
-  mutex.lock();
-  detached = true;
-  mutex.unlock();
-  pthread_detach(thread);
+  my_mutex.lock();
+  my_detached = true;
+  my_mutex.unlock();
+  pthread_detach(my_thread);
 }
 
 void Thread::exit(void *r)
 {
   Thread *me = self();
   if (me)
-    {
-      Prague::Guard<Mutex> guard(me->mutex);  
-      me->_state = terminated;
-    }
+  {
+    Prague::Guard<Mutex> guard(me->my_mutex);  
+    me->my_state = TERMINATED;
+  }
   pthread_exit(r);
 }
 
@@ -132,8 +142,7 @@
   id_mutex.lock();
   pthread_setspecific(id_key, new unsigned long (counter++));
   id_mutex.unlock();
-  void *ret = thread->p(thread->arg);
-  return ret;
+  return thread->my_proc(thread->my_arg);
 }
 
 bool Thread::delay(const Time &time)

Index: Timer.cc
===================================================================
RCS file: /cvs/fresco/Fresco/Prague/src/Sys/Timer.cc,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- Timer.cc	21 Mar 2001 06:28:55 -0000	1.10
+++ Timer.cc	8 May 2003 03:21:11 -0000	1.11
@@ -1,8 +1,8 @@
 /*$Id$
  *
- * This source file is a part of the Berlin Project.
- * Copyright (C) 1999 Stefan Seefeld <[email protected]> 
- * http://www.berlin-consortium.org
+ * This source file is a part of the Fresco Project.
+ * Copyright (C) 1999 Stefan Seefeld <[email protected]> 
+ * http://www.fresco.org
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -77,7 +77,7 @@
 
 void Timer::schedule(Timer *timer)
 {
-  if (server.state() != Thread::running) server.start();
+  if (server.state() == Thread::READY) server.start();
   Prague::Guard<Mutex> guard(mutex);
   timers.push_back(timer);
   push_heap(timers.begin(), timers.end(), comp());