~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/lock.cc

  • Committer: Brian Aker
  • Date: 2009-08-11 03:03:58 UTC
  • mfrom: (1113.1.2 merge)
  • Revision ID: brian@gaz-20090811030358-dn0mzaj7n94zrheg
lcov dead code removal (merge)

Show diffs side-by-side

added added

removed removed

Lines of Context:
162
162
 
163
163
 
164
164
DRIZZLE_LOCK *mysql_lock_tables(Session *session, Table **tables, uint32_t count,
165
 
                              uint32_t flags, bool *need_reopen)
 
165
                                uint32_t flags, bool *need_reopen)
166
166
{
167
167
  DRIZZLE_LOCK *sql_lock;
168
168
  Table *write_lock_used;
332
332
  This will work even if get_lock_data fails (next unlock will free all)
333
333
*/
334
334
 
335
 
void mysql_unlock_some_tables(Session *session, Table **table,uint32_t count)
 
335
void mysql_unlock_some_tables(Session *session, Table **table, uint32_t count)
336
336
{
337
337
  DRIZZLE_LOCK *sql_lock;
338
338
  Table *write_lock_used;
422
422
                          effect is desired.
423
423
*/
424
424
 
425
 
void mysql_lock_remove(Session *session, DRIZZLE_LOCK *locked,Table *table,
426
 
                       bool always_unlock)
 
425
void mysql_lock_remove(Session *session, Table *table)
427
426
{
428
 
  if (always_unlock == true)
429
 
    mysql_unlock_some_tables(session, &table, /* table count */ 1);
430
 
  if (locked)
431
 
  {
432
 
    register uint32_t i;
433
 
    for (i=0; i < locked->table_count; i++)
434
 
    {
435
 
      if (locked->table[i] == table)
436
 
      {
437
 
        uint32_t  j, removed_locks, old_tables;
438
 
        Table *tbl;
439
 
        uint32_t lock_data_end;
440
 
 
441
 
        assert(table->lock_position == i);
442
 
 
443
 
        /* Unlock if not yet unlocked */
444
 
        if (always_unlock == false)
445
 
          mysql_unlock_some_tables(session, &table, /* table count */ 1);
446
 
 
447
 
        /* Decrement table_count in advance, making below expressions easier */
448
 
        old_tables= --locked->table_count;
449
 
 
450
 
        /* The table has 'removed_locks' lock data elements in locked->locks */
451
 
        removed_locks= table->lock_count;
452
 
 
453
 
        /* Move down all table pointers above 'i'. */
454
 
        memmove((locked->table+i), (locked->table+i+1),
455
 
                (old_tables - i) * sizeof(Table*));
456
 
 
457
 
        lock_data_end= table->lock_data_start + table->lock_count;
458
 
        /* Move down all lock data pointers above 'table->lock_data_end-1' */
459
 
        memmove((locked->locks + table->lock_data_start),
460
 
                (locked->locks + lock_data_end),
461
 
                (locked->lock_count - lock_data_end) *
462
 
                sizeof(THR_LOCK_DATA*));
463
 
 
464
 
        /*
465
 
          Fix moved table elements.
466
 
          lock_position is the index in the 'locked->table' array,
467
 
          it must be fixed by one.
468
 
          table->lock_data_start is pointer to the lock data for this table
469
 
          in the 'locked->locks' array, they must be fixed by 'removed_locks',
470
 
          the lock data count of the removed table.
471
 
        */
472
 
        for (j= i ; j < old_tables; j++)
473
 
        {
474
 
          tbl= locked->table[j];
475
 
          tbl->lock_position--;
476
 
          assert(tbl->lock_position == j);
477
 
          tbl->lock_data_start-= removed_locks;
478
 
        }
479
 
 
480
 
        /* Finally adjust lock_count. */
481
 
        locked->lock_count-= removed_locks;
482
 
        break;
483
 
      }
484
 
    }
485
 
  }
 
427
  mysql_unlock_some_tables(session, &table, /* table count */ 1);
486
428
}
487
429
 
488
430
 
489
431
/** Abort all other threads waiting to get lock in table. */
490
432
 
491
 
void mysql_lock_abort(Session *session, Table *table, bool upgrade_lock)
 
433
void mysql_lock_abort(Session *session, Table *table)
492
434
{
493
435
  DRIZZLE_LOCK *locked;
494
436
  Table *write_lock_used;
496
438
  if ((locked= get_lock_data(session, &table, 1, false,
497
439
                             &write_lock_used)))
498
440
  {
499
 
    for (uint32_t i=0; i < locked->lock_count; i++)
500
 
      thr_abort_locks(locked->locks[i]->lock, upgrade_lock);
 
441
    for (uint32_t x= 0; x < locked->lock_count; x++)
 
442
      thr_abort_locks(locked->locks[x]->lock);
501
443
    free((unsigned char*) locked);
502
444
  }
503
445
}
536
478
}
537
479
 
538
480
 
539
 
DRIZZLE_LOCK *mysql_lock_merge(DRIZZLE_LOCK *a, DRIZZLE_LOCK *b)
540
 
{
541
 
  DRIZZLE_LOCK *sql_lock;
542
 
  Table **table, **end_table;
543
 
 
544
 
  if (!(sql_lock= (DRIZZLE_LOCK*)
545
 
        malloc(sizeof(*sql_lock)+
546
 
               sizeof(THR_LOCK_DATA*)*(a->lock_count+b->lock_count)+
547
 
               sizeof(Table*)*(a->table_count+b->table_count))))
548
 
    return NULL;                                // Fatal error
549
 
  sql_lock->lock_count=a->lock_count+b->lock_count;
550
 
  sql_lock->table_count=a->table_count+b->table_count;
551
 
  sql_lock->locks=(THR_LOCK_DATA**) (sql_lock+1);
552
 
  sql_lock->table=(Table**) (sql_lock->locks+sql_lock->lock_count);
553
 
  memcpy(sql_lock->locks,a->locks,a->lock_count*sizeof(*a->locks));
554
 
  memcpy(sql_lock->locks+a->lock_count,b->locks,
555
 
         b->lock_count*sizeof(*b->locks));
556
 
  memcpy(sql_lock->table,a->table,a->table_count*sizeof(*a->table));
557
 
  memcpy(sql_lock->table+a->table_count,b->table,
558
 
         b->table_count*sizeof(*b->table));
559
 
 
560
 
  /*
561
 
    Now adjust lock_position and lock_data_start for all objects that was
562
 
    moved in 'b' (as there is now all objects in 'a' before these).
563
 
  */
564
 
  for (table= sql_lock->table + a->table_count,
565
 
         end_table= table + b->table_count;
566
 
       table < end_table;
567
 
       table++)
568
 
  {
569
 
    (*table)->lock_position+=   a->table_count;
570
 
    (*table)->lock_data_start+= a->lock_count;
571
 
  }
572
 
 
573
 
  /* Delete old, not needed locks */
574
 
  free((unsigned char*) a);
575
 
  free((unsigned char*) b);
576
 
 
577
 
  return sql_lock;
578
 
}
579
 
 
580
 
 
581
481
/**
582
482
  Find duplicate lock in tables.
583
483
 
800
700
}
801
701
 
802
702
 
803
 
/*****************************************************************************
804
 
  Lock table based on the name.
805
 
  This is used when we need total access to a closed, not open table
806
 
*****************************************************************************/
807
 
 
808
 
/**
809
 
  Lock and wait for the named lock.
810
 
 
811
 
  @param session                        Thread handler
812
 
  @param table_list             Lock first table in this list
813
 
 
814
 
 
815
 
  @note
816
 
    Works together with global read lock.
817
 
 
818
 
  @retval
819
 
    0   ok
820
 
  @retval
821
 
    1   error
822
 
*/
823
 
 
824
 
int lock_and_wait_for_table_name(Session *session, TableList *table_list)
825
 
{
826
 
  int lock_retcode;
827
 
  int error= -1;
828
 
 
829
 
  if (wait_if_global_read_lock(session, 0, 1))
830
 
    return 1;
831
 
  pthread_mutex_lock(&LOCK_open); /* lock and wait for table when we need total access to table */
832
 
  if ((lock_retcode = lock_table_name(session, table_list, true)) < 0)
833
 
    goto end;
834
 
  if (lock_retcode && wait_for_locked_table_names(session, table_list))
835
 
  {
836
 
    unlock_table_name(table_list);
837
 
    goto end;
838
 
  }
839
 
  error=0;
840
 
 
841
 
end:
842
 
  pthread_mutex_unlock(&LOCK_open);
843
 
  start_waiting_global_read_lock(session);
844
 
  return error;
845
 
}
846
 
 
847
 
 
848
703
/**
849
704
  Put a not open table with an old refresh version in the table cache.
850
705
 
857
712
 
858
713
  @warning
859
714
    If you are going to update the table, you should use
860
 
    lock_and_wait_for_table_name instead of this function as this works
 
715
    lock_and_wait_for_table_name(removed) instead of this function as this works
861
716
    together with 'FLUSH TABLES WITH READ LOCK'
862
717
 
863
718
  @note
977
832
 
978
833
  @param table_list             Names of tables to lock
979
834
 
980
 
  @note
981
 
    If you are just locking one table, you should use
982
 
    lock_and_wait_for_table_name().
983
 
 
984
835
  @retval
985
836
    0   ok
986
837
  @retval