Example #1
0
// open the database, read-only or read/write as specified by the flag. if the
// database is missing: create in write mode, return 0 in read mode.
static _Bool db_open(_Bool write) {
    char *name;
    int err, flags;

    name = dbfilename(NULL);

    if (write)
        flags = SQLITE_OPEN_READWRITE;
    else
        flags = SQLITE_OPEN_READONLY;

    err = sqlite3_open_v2(name, &db, flags, 0);

    if (err != SQLITE_OK && errno == ENOENT) {
        if (!write) return 0;
        flags |= SQLITE_OPEN_CREATE;
        err = sqlite3_open_v2(name, &db, flags, 0);
        if (err == SQLITE_OK)
            db_init();
    }

    if (err != SQLITE_OK) {
        fatal4("cannot open database `", name, "': ", sqlite3_errmsg(db));
        sqlite3_close(db);
    }

    sqlite3_extended_result_codes(db, 1);

    db_check_version();
    return 1;
}
Example #2
0
void Mutex::set_owner_implementation(Thread *new_owner) {  
  // This function is solely responsible for maintaining
  // and checking the invariant that threads and locks
  // are in a 1/N relation, with some some locks unowned.
  // It uses the Mutex::_owner, Mutex::_next, and
  // Thread::_owned_locks fields, and no other function
  // changes those fields.
  // It is illegal to set the mutex from one non-NULL
  // owner to another--it must be owned by NULL as an
  // intermediate state.

  if (new_owner != INVALID_THREAD) {
    // the thread is acquiring this lock
 
    assert(new_owner == Thread::current(), "Should I be doing this?");
    assert(_owner == INVALID_THREAD, "setting the owner thread of an already owned mutex");
    _owner = new_owner; // set the owner

    // link "this" into the owned locks list

    #ifdef ASSERT  // Thread::_owned_locks is under the same ifdef
      Mutex* locks = get_least_ranked_lock(new_owner->owned_locks());
                    // Mutex::set_owner_implementation is a friend of Thread

      assert(this->rank() >= 0, "bad lock rank");

      if (LogMultipleMutexLocking && locks != NULL && this != Event_lock) {
        Events::log("thread %#x locks %s, already owns %s", new_owner, name(), locks->name());
      }

      // Deadlock avoidance rules require us to acquire Mutexes only in
      // a global total order (if m1 has rank less than m2 then
      // m2 cannot be acquired while holding m1). The rank Mutex::native
      // is an exception in that it is not subject to the verification rules.
      // Here are some further notes relating to mutex acquisition anomalies:
      // . under Solaris, the interrupt lock gets acquired when doing
      //   profiling, so any lock could be held.
      // . it is also ok to acquire Safepoint_lock at the very end while we
      //   already hold Terminator_lock - may happen because of periodic safepoints
      if (this->rank() != Mutex::native &&
          this->rank() != Mutex::suspend_resume &&
          locks != NULL && locks->rank() <= this->rank() &&
	  !SafepointSynchronize::is_at_safepoint() &&
          this != Interrupt_lock && this != ProfileVM_lock &&
          this != SuspendChecker_lock &&
	  !(this == Safepoint_lock && contains(locks, Terminator_lock) &&
            SafepointSynchronize::is_synchronizing())) { 
        new_owner->print_owned_locks();
        fatal4("acquiring lock %s/%d out of order with lock %s/%d -- possible deadlock", 
	       this->name(), this->rank(), locks->name(), locks->rank());
      }

      this->_next = new_owner->_owned_locks;
      new_owner->_owned_locks = this;
    #endif

  } else {
    // the thread is releasing this lock

    Thread* old_owner = _owner;
    debug_only(_last_owner = old_owner);
    
    assert(old_owner != INVALID_THREAD, "removing the owner thread of an unowned mutex");
    assert(old_owner == Thread::current(), "removing the owner thread of an unowned mutex");

    _owner = INVALID_THREAD; // set the owner

    #ifdef ASSERT
      Mutex *locks = old_owner->owned_locks();

      if (LogMultipleMutexLocking && locks != this && this != Event_lock) {
        Events::log("thread %#x unlocks %s, still owns %s", old_owner, this->name(), locks->name());
      }
  
      // remove "this" from the owned locks list
  
      Mutex *prev = NULL;
      bool found = false;
      for (; locks != NULL; prev = locks, locks = locks->next()) {
        if (locks == this) {
	  found = true;
	  break;
        }
      }
      assert(found, "Removing a lock not owned");
      if (prev == NULL) {
        old_owner->_owned_locks = _next;
      } else {
        prev->_next = _next;
      }
      _next = NULL;
    #endif
  }
}