Пример #1
0
/* Internal function: _al_register_destructor
 *  Register OBJECT to be destroyed by FUNC during Allegro shutdown.
 *  This would be done in the object's constructor function.
 *
 *  [thread-safe]
 */
void _al_register_destructor(_AL_DTOR_LIST *dtors, char const *name,
   void *object, void (*func)(void*))
{
   int *dtor_owner_count;
   ASSERT(object);
   ASSERT(func);

   dtor_owner_count = _al_tls_get_dtor_owner_count();
   if (*dtor_owner_count > 0)
      return;

   _al_mutex_lock(&dtors->mutex);
   {
#ifdef DEBUGMODE
      /* make sure the object is not registered twice */
      {
         unsigned int i;

         for (i = 0; i < _al_vector_size(&dtors->dtors); i++) {
            DTOR *dtor = _al_vector_ref(&dtors->dtors, i);
            ASSERT(dtor->object != object);
         }
      }
#endif /* DEBUGMODE */

      /* add the destructor to the list */
      {
         DTOR *new_dtor = _al_vector_alloc_back(&dtors->dtors);
         if (new_dtor) {
            new_dtor->object = object;
            new_dtor->func = func;
            new_dtor->name = name;
            ALLEGRO_DEBUG("added dtor for %s %p, func %p\n", name,
               object, func);
         }
         else {
            ALLEGRO_WARN("failed to add dtor for %s %p\n", name,
               object);
         }
      }
   }
   _al_mutex_unlock(&dtors->mutex);
}
/* _al_push_destructor_owner:
 *  Decrease the owner count for the current thread.
 */
void _al_pop_destructor_owner(void)
{
   int *dtor_owner_count = _al_tls_get_dtor_owner_count();
   (*dtor_owner_count)--;
   ASSERT(*dtor_owner_count >= 0);
}
/* _al_push_destructor_owner:
 *  Increase the owner count for the current thread.  When it is greater than
 *  zero, _al_register_destructor will do nothing.
 *
 *  This is required if an object to-be-registered (B) should be "owned" by an
 *  object (A), which is responsible for destroying (B).  (B) should not be
 *  destroyed independently of (A).
 */
void _al_push_destructor_owner(void)
{
   int *dtor_owner_count = _al_tls_get_dtor_owner_count();
   (*dtor_owner_count)++;
}