- // Fabrique un identifiant unique pour les threads puisque le thread_id n'est pas unique
- // au cours du temps (il peut etre reutilise lorsqu'un thread se termine)
- // ATTENTION : cette methode est uniquement protegee par la section critique de l'association
- // Thread_id / Id (_thread_id_id_association_mutex)
- BatchManager_Local::Id BatchManager_Local::nextId()
- {
- static Id id = 0;
- Id nextId = id++;
- //UNDER_LOCK( cout << "BatchManager_Local::Id BatchManager_Local::nextId() : Id = " << nextId << endl );
- return nextId;
- }
-
-
- // Retourne l'Id enregistre dans l'association Thread_id / Id et le detruit immediatement
- BatchManager_Local::Id BatchManager_Local::getIdByThread_id(pthread_t thread_id)
- {
- // @@@ --------> SECTION CRITIQUE <-------- @@@
- pthread_mutex_lock(&_thread_id_id_association_mutex);
- bool threadIdFound = false;
- std::list<struct ThreadIdIdAssociation>::iterator it;
- while (!threadIdFound) {
- for (it = _thread_id_id_association.begin() ;
- it != _thread_id_id_association.end() && !pthread_equal(it->threadId, thread_id) ;
- it++);
- if (it == _thread_id_id_association.end())
- pthread_cond_wait(&_thread_id_id_association_cond, &_thread_id_id_association_mutex);
- else
- threadIdFound = true;
- }
-
- Id id = it->id;
- _thread_id_id_association.erase(it);
-
- pthread_mutex_unlock(&_thread_id_id_association_mutex);
- // @@@ --------> SECTION CRITIQUE <-------- @@@
-
- //UNDER_LOCK( cout << "BatchManager_Local::Id BatchManager_Local::getIdByThread_id(pthread_t thread_id) : Id = " << id << " - thread_id = " << thread_id << endl );
- return id;
- }
-
-
- // Associe un Thread_id a un Id nouvellement cree
- BatchManager_Local::Id BatchManager_Local::registerThread_id(pthread_t thread_id)
- {
- Id id = -1;
-
- // @@@ --------> SECTION CRITIQUE <-------- @@@
- pthread_mutex_lock(&_thread_id_id_association_mutex);
- std::list<struct ThreadIdIdAssociation>::iterator it;
- for (it = _thread_id_id_association.begin() ;
- it != _thread_id_id_association.end() && !pthread_equal(it->threadId, thread_id) ;
- it++);
- if (it == _thread_id_id_association.end()) {
- struct ThreadIdIdAssociation newAssociation;
- id = newAssociation.id = nextId();
- newAssociation.threadId = thread_id;
- _thread_id_id_association.push_back(newAssociation);
- pthread_cond_signal(&_thread_id_id_association_cond);
-
- } else {
- UNDER_LOCK( cerr << "ERROR : Pthread Inconstency. Two threads own the same thread_id." << endl );
- }
- pthread_mutex_unlock(&_thread_id_id_association_mutex);
- // @@@ --------> SECTION CRITIQUE <-------- @@@
-
- //UNDER_LOCK( cout << "BatchManager_Local::Id BatchManager_Local::registerThread_id(pthread_t thread_id) : Id = " << id << " - thread_id = " << thread_id << endl );
- return id;
- }
-