00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef LIBMUTH_SUSPENDER_H
00020 #define LIBMUTH_SUSPENDER_H
00021
00022 #include <assert.h>
00023 #include "suspender_decls.h"
00024 #include "currentthread.h"
00025
00026 template<class T> T Suspender<T>::suspend() {
00027 this->lock.acquire();
00028 assert(this->microthread == NULL);
00029 if(!this->wakened) {
00030 Microthread *m(this->microthread = &getcurrentthread());
00031 this->lock.release();
00032 m->delayme();
00033 } else
00034 this->lock.release();
00035 assert(this->wakened == true);
00036 return this->retval;
00037 }
00038
00039 template<class T> bool Suspender<T>::wakeup(const T &rv) {
00040 this->lock.acquire();
00041 if(this->wakened) {
00042 this->lock.release();
00043 return false;
00044 }
00045 this->retval = rv;
00046 this->wakened = true;
00047 if(Microthread *m = this->microthread) {
00048 this->lock.release();
00049 m->scheduleme();
00050 } else
00051 this->lock.release();
00052 return true;
00053 }
00054
00055 template<class T> bool Suspender<T>::cooperativewakeup(const T &rv) {
00056 this->lock.acquire();
00057 if(this->wakened) {
00058 this->lock.release();
00059 return false;
00060 }
00061 this->wakened = true;
00062 this->retval = rv;
00063 if(Microthread *m = this->microthread) {
00064 this->lock.release();
00065 Microthread &me(getcurrentthread());
00066 me.scheduleme();
00067 me.swapto(*m);
00068 } else
00069 this->lock.release();
00070 return true;
00071 }
00072
00073 template<class T> inline bool Suspender<T>::issuspended() const {
00074
00075 return this->microthread != NULL && !this->wakened;
00076 }
00077
00078 template<class T> inline bool Suspender<T>::iswakened() const {
00079
00080 return this->wakened;
00081 }
00082
00083 template<class T> inline bool Suspender<T>::iscompleted() const {
00084
00085 return this->microthread != NULL && this->wakened;
00086 }
00087
00088 #endif