00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- 00002 00003 // "comm.cpp" 00004 00005 // (c) OPAC Team, LIFL, August 2005 00006 00007 /* This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Lesser General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public 00018 License along with this library; if not, write to the Free Software 00019 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 00021 Contact: paradiseo-help@lists.gforge.inria.fr 00022 */ 00023 00024 #include <vector> 00025 #include <map> 00026 #include <cassert> 00027 00028 #include "communicable.h" 00029 00030 static std :: vector <Communicable *> key_to_comm (1); /* Vector of registered cooperators */ 00031 00032 static std :: map <const Communicable *, unsigned> comm_to_key; /* Map of registered cooperators */ 00033 00034 unsigned Communicable :: num_comm = 0; 00035 00036 Communicable :: Communicable () { 00037 00038 comm_to_key [this] = key = ++ num_comm; 00039 key_to_comm.push_back (this); 00040 sem_init (& sem_lock, 0, 1); 00041 sem_init (& sem_stop, 0, 0); 00042 } 00043 00044 Communicable :: ~ Communicable () { 00045 00046 } 00047 00048 COMM_ID Communicable :: getKey () { 00049 00050 return key; 00051 } 00052 00053 Communicable * getCommunicable (COMM_ID __key) { 00054 00055 assert (__key < key_to_comm.size ()); 00056 return key_to_comm [__key]; 00057 } 00058 00059 COMM_ID getKey (const Communicable * __comm) { 00060 00061 return comm_to_key [__comm]; 00062 } 00063 00064 void Communicable :: lock () { 00065 00066 sem_wait (& sem_lock); 00067 } 00068 00069 void Communicable :: unlock () { 00070 00071 sem_post (& sem_lock); 00072 } 00073 00074 void Communicable :: stop () { 00075 00076 sem_wait (& sem_stop); 00077 } 00078 00079 void Communicable :: resume () { 00080 00081 sem_post (& sem_stop); 00082 } 00083 00084 00085
1.4.6