00001 /* 00002 * Promise.cc 00003 * 00004 * Smalltalk like class library for C++ 00005 * Thread wrapper returning value. 00006 * 00007 * Copyright (c) 2003-05 Milan Cermak 00008 */ 00009 /* 00010 * This library is free software; you can redistribute it and/or 00011 * modify it under the terms of the GNU Lesser General Public 00012 * License as published by the Free Software Foundation; either 00013 * version 2.1 of the License, or (at your option) any later version. 00014 * 00015 * This library is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 * Lesser General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU Lesser General Public 00021 * License along with this library; if not, write to the Free Software 00022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 */ 00024 #include <stlib/os/Promise.h> 00025 00026 #include <stlib/Object.h> 00027 #include <stlib/Array.h> 00028 #include <stlib/GenericException.h> 00029 00030 #include <stlib/os/Semaphore.h> 00031 #include <stlib/os/Thread.h> 00032 00033 using namespace OS; 00034 00035 Promise::Promise(Callback<Object *> *receiver) 00036 { 00037 Callback<void> *callback; 00038 00039 _receiver = receiver; 00040 _arguments = nil; 00041 sync = new Semaphore; 00042 callback = new Callback0<Promise, void>(this, &Promise::executePromise); 00043 thread = new Thread(callback); 00044 has_value = false; 00045 _value = nil; 00046 _exception = nil; 00047 } 00048 00049 Promise::~Promise(void) 00050 { 00051 delete thread; 00052 thread = nil; 00053 _receiver = nil; 00054 delete sync; 00055 } 00056 00057 /* Class-accessing protocol */ 00058 String *Promise::className(void) const 00059 { 00060 return new String("Promise"); 00061 } 00062 00063 /* Instance creation protocol */ 00064 Promise *Promise::perform(Callback<Object *> *receiver, Array *args) 00065 { 00066 Promise *promise; 00067 promise = new Promise(receiver); 00068 promise->arguments(args); 00069 promise->run(); 00070 return promise; 00071 } 00072 00073 void Promise::run(void) 00074 { 00075 thread->run(); 00076 } 00077 00078 /* Accessing-child protocol */ 00079 void Promise::exception(GenericException *ex) 00080 { 00081 /* Garbage collecor must be stopped here 'cause data are tranfered across 00082 two processes. If I don't stop it, it may reclaim the exception and 00083 thus cause segfault. */ 00084 // GC_disable(); 00085 _exception = ex; 00086 has_value = true; 00087 sync->signal(); 00088 // GC_enable(); 00089 } 00090 00091 void Promise::value(Object *object) 00092 { 00093 /* Garbage collecor must be stopped here 'cause data are tranfered across 00094 two processes. If I don't stop it, it may reclaim the value and 00095 thus cause segfault. */ 00096 // GC_disable(); 00097 _value = object; 00098 has_value = true; 00099 sync->signal(); 00100 // GC_enable(); 00101 } 00102 00103 /* Accessing-parent protocol */ 00104 void Promise::arguments(Array *args) 00105 { 00106 _arguments = args; 00107 } 00108 00109 bool Promise::hasValue(void) 00110 { 00111 return has_value; 00112 } 00113 00114 Object *Promise::value(void) 00115 { 00116 sync->wait(); 00117 sync->signal(); 00118 if (_exception != nil) { 00119 _exception->pass(); 00120 } 00121 return _value; 00122 } 00123 00124 /* Private protocol */ 00125 void Promise::executePromise(void) 00126 { 00127 try { 00128 value(_receiver->executeWith(_arguments)); 00129 } 00130 catch (GenericException *ex) { 00131 exception(ex); 00132 } 00133 }