Implement new database locking system in pkg.

We define three stages locking model:

- PKGDB_LOCK_READONLY - a process wants to read datatabase and cannot
  modify it
- PKGDB_LOCK_ADVISORY - a process needs a *persustent* state of a
  database meaning that reading operations are permitted but no other
  advisory or exclusive locks could be obtained. However, due to sqlite
  peculiarities it is not recommended to modify database while held this
  lock. For modify operations it is possible to upgrade advisory lock to
  exclusive one.
- PKGDB_LOCK_EXCLUSIVE - a process wants to update database intensively.
  No read or advisory locks are allowed this time.

Some notes for using advisory and exclusive lock. For example `pkg
upgrade` wants to download gigabytes of packages from the Internet.
However, it wants to preserve the state of database preventing, for
example, `pkg delete` to delete some packages (or even try to do so).
Therefore upgrade process obtains advisory lock, solve upgrade problem,
fetch packages and then upgrade advisory lock to exclusive to modify
database. After database is modified a caller needs to release both
exclusive and advisory locks.

Note: lock upgrade is performed by pkg_jobs itself in libpkg, so a
caller should not bother at least about this type of upgrade.

TODO: handle timeouts and attempts more carefully.